[Ultraviolet A] 11995-I can guess the data structure! [STL application]

Source: Internet
Author: User

11995-I can guess the data structure! Time Limit: 1.000 secondsProblem II 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 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

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!

 

Question: Use of queue, stack, and priority_queue in STL.

Code:

 

 1 #include<cstdio> 2 #include<cstring> 3 #include<stdbool.h> 4 #include<cmath> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8  9 10 #define rep(i,a,b)      for(i=(a);i<=(b);i++)11 #define red(i,a,b)      for(i=(a);i>=(b);i--)12 #define clr(x,y)        memset(x,y,sizeof(x))13 #define sqr(x)          ((x)*(x))14 15 using namespace std;16 17 int i,j,x,y,pri,qui,sti;18     19 void check()20 {21     if(qui==1 && pri==0 && sti==0) printf("queue\n");22     else23     if(qui==0 && pri==1 && sti==0) printf("priority queue\n");24     else25     if(qui==0 && pri==0 && sti==1) printf("stack\n");26     else27     if(qui==0 && pri==0 && sti==0) printf("impossible\n");28     else29     printf("not sure\n");   30 }31 32 int main()33 {34     int T;35     36    37     38     while(scanf("%d",&T)!=EOF) {39          pri=qui=sti=1;40         41          queue <int> Q;42          stack <int> S;43          priority_queue <int> prQ;44         45         while(T--) {46             scanf("%d%d",&x,&y);47             if(x==1) {48                 Q.push(y);49                 S.push(y);50                 prQ.push(y);51             } 52               else {53                54                   if(qui==1) {55                     if(Q.empty() || Q.front()!=y) qui=0;56                     if(!Q.empty()) Q.pop();57                   }58                    59                   if(sti==1) {60                     if(S.empty() || S.top()!=y) sti=0;61                     if(!S.empty()) S.pop();62                   }   63       64                   if(pri==1) {65                     if(prQ.empty() || prQ.top()!=y) pri=0;66                     if(!prQ.empty()) prQ.pop();67                   }          68              }    69         }70            71             check();72     }73     74     return 0;75 }

 

[Ultraviolet A] 11995-I can guess the data structure! [STL application]

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.