Problem I
I Can Guess the Data Structure!
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
Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!
直接STL进行小模拟即可,注意pop的时候判断是否empty。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
using namespace std;
const int MAXN = 1010 + 50;
const int maxw = 100 + 20;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e10-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
stack <int> S;
queue <int> Q;
priority_queue <int> PQ;
int n , oks , okq , okpq , a , b;
int main()
{
//ios::sync_with_stdio(false);
#ifdef Online_Judge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // Online_Judge
while(scanf("%d",&n)!=EOF)
{
while(!S.empty())S.pop();
while(!Q.empty())Q.pop();
while(!PQ.empty())PQ.pop();
oks = okq = okpq = 1;
while(n--)
{
scanf("%d%d" , &a , &b);
if(a == 1)
{
S.push(b);
Q.push(b);
PQ.push(b);
}
else
{
if(oks)
{
if(!S.empty()&&S.top() == b)
{
S.pop();
}
else
{
oks = 0;
}
}
if(okq)
{
if(!Q.empty()&&Q.front() == b)
{
Q.pop();
}
else
{
okq = 0;
}
}
if(okpq)
{
if(!PQ.empty()&&PQ.top() == b)
{
PQ.pop();
}
else
{
okpq = 0;
}
}
}
}
if(oks&&!okq&&!okpq)printf("stack\n");
else if(!oks&&okq&&!okpq)printf("queue\n");
else if(!oks&&!okq&&okpq)printf("priority queue\n");
else if(!oks&&!okq&&!okpq)printf("impossible\n");
else printf("not sure\n");
}
return 0;
}