Ultraviolet A 11995

Source: Internet
Author: User
I can guess the data structure!

Time Limit: 1.000 seconds

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 xWithout 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 1 MB.

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
61 11 21 32 12 22 361 11 21 32 32 22 121 12 241 21 12 12 271 21 51 11 32 51 42 4
Output for the sample input
queuenot sureimpossiblestackpriority queue

Measure the test taker's knowledge about the definition of the basic data structure.

/** @author  Panoss*/#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<vector>#include<ctime>#include<stack>#include<queue>#include<list>using namespace std;#define DBG 0#define fori(i,a,b) for(int i = (a); i < (b); i++)#define forie(i,a,b) for(int i = (a); i <= (b); i++)#define ford(i,a,b) for(int i = (a); i > (b); i--)#define forde(i,a,b) for(int i = (a); i >= (b); i--)#define forls(i,a,b,n) for(int i = (a); i != (b); i = n[i])#define mset(a,v) memset(a, v, sizeof(a))#define mcpy(a,b) memcpy(a, b, sizeof(a))#define dout  DBG && cerr << __LINE__ << " >>| "#define checkv(x) dout << #x"=" << (x) << " | "<<endl#define checka(array,a,b) if(DBG) { \    dout << #array"[] | " << endl;     forie(i, a, b) cerr << "[" << i << "]=" << array[i] << " |" << ((i - (a)+1) % 5 ? " " : "\n"); if (((b)-(a)+1) % 5) cerr << endl; }#define redata(T, x) T x; cin >> x#define MIN_LD -2147483648#define MAX_LD  2147483647#define MIN_LLD -9223372036854775808#define MAX_LLD  9223372036854775807#define MAX_INF 18446744073709551615inline int  reint() { int d; scanf("%d", &d); return d; }inline long relong() { long l; scanf("%ld", &l); return l; }inline char rechar() { scanf(" "); return getchar(); }inline double redouble() { double d; scanf("%lf", &d); return d; }inline string restring() { string s; cin >> s; return s; }struct Cmd{    int cmd;    int x;};vector<Cmd> C;bool is_stack(){    stack<int> S;    fori(i,0,C.size())    {        if(C[i].cmd == 1)            S.push(C[i].x);        else        {            if(S.empty()) return false;            int value = S.top();            S.pop();            if(value != C[i].x) return false;        }    }    return true;}bool is_queue(){    queue<int> Q;    fori(i,0,C.size())    {        if(C[i].cmd == 1)            Q.push(C[i].x);        else        {            if(Q.empty()) return false;            int value = Q.front();            Q.pop();            if(value != C[i].x) return false;        }    }    return true;}bool is_pri_queue(){    priority_queue<int> Q;    fori(i,0,C.size())    {        if(C[i].cmd == 1)            Q.push(C[i].x);        else        {            if(Q.empty()) return false;            int value = Q.top();            Q.pop();            if(value != C[i].x) return false;        }    }    return true;}int main(){    int n;    while(scanf("%d",&n)==1)    {        C.clear();        forie(i,1,n)        {            Cmd command;            scanf("%d%d",&command.cmd,&command.x);            C.push_back(command);        }        bool is_st = is_stack();        bool is_qu = is_queue();        bool is_pri_qu = is_pri_queue();        if(!is_st&&!is_qu&&!is_pri_qu) puts("impossible");        else if(is_st&&is_qu&&is_pri_qu) puts("not sure");        else if((is_st&&is_qu&&!is_pri_qu)||(is_st&&!is_qu&&is_pri_qu)||(!is_st&&is_qu&&is_pri_qu)) puts("not sure");        else if(is_st&&!is_qu&&!is_pri_qu) puts("stack");        else if(!is_st&&is_qu&&!is_pri_qu) puts("queue");        else if(!is_st&&!is_qu&&is_pri_qu) puts("priority queue");    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.