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 }