您的位置:首页 > 其它

UVA 11995 STL 使用

2016-04-20 20:52 447 查看
There is a bag-like data structure, supporting two operations:
1 x

Throw an element x into the bag.
2

Take out an element from the bag.Given a sequence of operations with return values, you're going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

Input

There are several test cases. Each test case begins with a line containing a single integer n (1<=n<=1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF). The size of input file does not exceed 1MB.

Output

For each test case, output one of the following:
stack

It's definitely a stack.
queue

It's definitely a queue.
priority queue

It's definitely a priority queue.
impossible

It can't be a stack, a queue or a priority queue.
not sure

It can be more than one of the three data structures mentioned above.

Sample Input

6
1 1
1 2
1 3
2 1
2 2
2 3
6
1 1
1 2
1 3
2 3
2 2
2 1
2
1 1
2 2
4
1 2
1 1
2 1
2 2
7
1 2
1 5
1 1
1 3
2 5
1 4
2 4

Output for the Sample Input

queue
not sure
impossible
stack
priority queue

题意:
判断是哪一种数据结构
题解:
STL <queue> <stack> priority_queue
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
using namespace std;
queue<int> q;
stack<int> s;
struct ss{
int f;
friend bool operator < (ss a, ss b)
{return  a.f < b.f;}
};
priority_queue<ss> pq;
int aa,bb;
int n;
int ans,ans1,ans2,ans3;
int cun[1005][2];
int main()
{
while(scanf("%d",&n)!=EOF)
{
ans=0;ans1=0;ans2=0;ans3=0;
while(q.size()>0)
q.pop();
while(s.size()>0)
s.pop();
while(pq.size()>0)
pq.pop();
memset(cun,0,sizeof(cun));
for(int i=1;i<=n;i++)
scanf("%d %d",&cun[i][0],&cun[i][1]);
for(int i=1;i<=n;i++)
{
int aa=cun[i][0];
int bb=cun[i][1];
if(aa==1)
{
q.push(bb);
s.push(bb);
struct ss gg;
gg.f=bb;
pq.push(gg);
}
else
{
ans++;
if(!q.empty()&&bb==q.front())
{ ans1++; q.pop();}
if(!s.empty()&&bb==s.top())
{ans2++;s.pop();}
if(!pq.empty()&&bb==pq.top().f)
{ans3++;pq.pop();}
}
}
//cout<<ans<<" "<<ans1<<" "<<ans2<<" "<<ans3<<" "<<endl;
int qu=0,st=0,pri=0;
if(ans==ans1)
qu=1;
if(ans==ans2)
st=1;
if(ans==ans3)
pri=1;
if (st + qu + pri > 1) printf("not sure\n");
else if (st) printf("stack\n");
else if (qu) printf("queue\n");
else if (pri) printf("priority queue\n");
else printf("impossible\n");

}
return 0;
}
/*
3
1 1
1 2
2 1
6
1 1
1 2
1 3
2 1
2 2
2 3
6
1 1
1 2
1 3
2 3
2 2
2 1
*/

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: