3138 Stack Exercise 2
Submit Address: http://codevs.cn/problem/3138/
time limit: 1 sspace limit: 128000 KB title level: Golden Gold
Title Description
Description
(This problem compared to the stack Exercise 1 is changed 2:1 Enhanced Data 2 does not guarantee that the stack is not stack empty)
Given a stack (initially empty, the element type is an integer and less than or equal to 100), there are only two operations: the stack and the stack. To give these operations first, output the top stack of the final stack. Operation Explanation: 1 means the stack, 2 indicates the stack
Enter a description
Input Description
N (number of operations)
N Operations (if the stack is followed by a stack element)
See the sample (input does not guarantee that the stack is empty)
Output description
Output Description
The final stack top element, if the final stack empty, or stack empty when there is a stack operation, output "impossible!" (without quotation marks)
Sample input
Sample Input
3
1 2
2
2
Sample output
Sample Output
impossible!
Data range and Tips
Data Size & Hint
For 100% of data n≤100000 elements are positive integers and less than or equal to 10^8
1#include <cstdio>2 inta[100001];3 intn,x,y;4 intMain ()5 {6 intp=0;7scanf"%d",&n);8 for(intI=1; i<=n;i++)9 {Tenscanf"%d",&x); One if(x==1) {scanf ("%d", &y); a[p++]=y;} A Else if(p>0) a[--p]=0; - Else{printf ("impossible!");return 0;} - } the if(p<=0) printf ("impossible!"); - Elseprintf"%d", a[--p]); - return 0; -}
Codevs 3138-Stack Exercise 2