Codevs Queue Practice Merge Edition

Source: Internet
Author: User

3185 Queue Exercise 1time limit: 1 sspace limit: 128000 KBtitle level: Golden GoldTitle Description Description

Given a queue (initially empty), only two operations are queued and out of the team, now give these operations to output the final team header element. Operation Explanation: 1 means queue, 2 indicates the team

Enter a description Input Description

N (number of operations)
n Operations (there will be a queue element after the queue)
See Example (Input guarantee team will not be out of the team when empty)

Output description Output Description

Final team head element, if the final team is empty, output "impossible!" (without quotation marks)

Sample input Sample Input

3
1 2
1 9
2

Sample output Sample Output

9

Data range and Tips Data Size & Hint

For 100% of data n≤1000 elements are positive integers and less than or equal to 100

1#include <iostream>2 using namespacestd;3 inta[1010],n;4 intMain ()5 {6     intX,y,head=0, tail=0;7Cin>>N;8      for(intI=1; i<=n;i++)9     {TenCin>>y; One         if(y==1) A         { -cin>>x;a[tail]=x; -tail++; the         } -         if(y==2) -         { -head++;  +         } -     } +     if(Head==tail) cout<<"impossible!"; A     Elsecout<<A[head]; at     return 0; -}
3186 Queue Exercise 2

time limit: 1 s space limit: 128000 KB title level: Golden Gold

Title Description Description

(This problem compared to the queue Exercise 1 changed 2:1 strengthened data 2 does not guarantee that the team will not be out of the team when empty)
Given a queue (initially empty), only two operations are queued and the team is out, now give these actions
Outputs the final team head element. Operation Explanation: 1 means queue, 2 indicates the team

Enter a description Input Description

N (number of operations)
n Operations (there will be a queue element after the queue)
See Example (Input guarantee team will not be out of the team when empty)

Output description Output Description

Final team head element, if the final team is empty, or the team is empty, there is a team 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 <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 inta[200000],n;6 intMain ()7 {8     intX,y,head=0, tail=0;9Cin>>N;Ten      for(intI=1; i<=n;i++) One     { Ascanf"%d",&y); -         if(y==1) -         { thescanf"%d",&x); -a[tail]=x; -tail++; -         } +         if(y==2) -         { +head++; A         } at         if(head>tail) { -printf"impossible!\n"); -             return 0; -         } -     } -     if(head>tail) { inprintf"impossible!\n"); -     } to     Else  +printf"%d\n", A[head]); -     return 0; the}
3187 Queue Exercise 3

time limit: 1 s space limit: 128000 KB title level: Diamonds Diamond

Title Description Description

Compared to the first question, the topic added another operation, access to the team head element (number 3, guaranteed access to the team head element or when the team is not empty), now give the n this operation, output results.

Enter a description Input Description

N
n Operations (1 queue 2 out Team 3 Access team head)

Output description Output Description

K Row (k is the number of queries in the input) each time the result

Sample input Sample Input

6
1 7
3
2
1 9
1 7
3

Sample output Sample Output

7
9

Data range and Tips Data Size & Hint

For 50% data n≤1000 the enqueued element ≤200
For 100% data n≤100000 The enqueued element is a positive integer and is less than or equal to 10^4

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 inta[200000],n;6 intMain ()7 {8     intX,y,head=0, tail=0;9Cin>>N;Ten      for(intI=1; i<=n;i++) One     { Ascanf"%d",&y); -         if(y==1) -         { thescanf"%d",&x); -a[tail]=x; -tail++; -         } +         if(y==2) -         { +head++; A         } at         if(y==3) -         { -printf"%d\n", A[head]); -         }  -     } -      in     return 0; -}

Codevs Queue Practice Merge Edition

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.