3186 queue exercises

Source: Internet
Author: User

3186 queue exercises
3186 queue Exercise 2

 

Time Limit: 1 s space limit: 128000 KB title level: Gold Title Description Description

(This question is changed to two places compared to queue Exercise 1: 1 Enhanced Data 2 does not guarantee that the team will not leave when the team is empty)
Given a queue (initially empty), there are only two types of operations to join and exit, please
Output the final Header element. Operation explanation: 1 indicates entering the team, 2 indicates leaving the team

Input description Input Description

N (number of operations)
N operations (if the operation is to join the queue, there will be another element to join the queue)
For details, see the example (input to ensure that the team will not leave when the team is empty)

Output description Output Description

Element of the final team header. If the final team is empty or the team is empty, an operation is performed to output "impossible !" (Without quotation marks)

Sample Input Sample Input

3
1 2
2
2

Sample output Sample Output

Impossible!

Data range and prompt Data Size & Hint

For 100% of data, elements N ≤ 100000 are positive integers and smaller than or equal to 10 ^ 8

 1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 queue<int>a; 5 int main() 6 { 7     int n; 8     cin>>n; 9     for(int i=1;i<=n;i++)10     {11         int b;12         cin>>b;13         if(b==1)14         {15             int c;16             cin>>c;17             a.push(c);18         }19         else if(b==2)20         {21             if(a.size()!=0)22             a.pop();23             else24             {25                 cout<<"impossible!";26                 return 0;27             }28         }29     }30     if(a.size()==0)31     {32         cout<<"impossible!";33     }34     else35     {36         cout<<a.front();37     }38     return 0;39 }

 

Related Article

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.