Codevs 3138-Stack Exercise 2

Source: Internet
Author: User

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

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.