Double Queue
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:4628 |
|
Accepted:2130 |
Description
The
New founded Balkan Investment Group Bank (big-bank) opened a new office
In Bucharest, equipped with a modern computing environment provided
IBM Romania, and using modern information technologies. As usual, each
Client of the bank is identified by a positive integerKAnd, upon arriving to the bank for some services, he or she has es a positive integer priorityP.
One of the limitations of the young manager of the bank shocked
Software Engineer of the serving system. They proposed to break
Tradition by sometimes calling the serving desk withLowestPriority instead of that with the highest priority. Thus, the system will receive the following types of request:
0 |
The system needs to stop serving |
1K P |
Add ClientKTo the Waiting List with priorityP |
2 |
Serve the client with the highest priority and drop him or her from the waiting list |
3 |
Serve the client with the lowest priority and drop him or her from the waiting list |
Your task is to help the software engineer of the Bank by writing a program to implement the requested serving policy.
Input
Each
Line of the input contains one of the possible requests; only the last
Line contains the stop-Request (code 0). You may assume that when there
Is a request to include a new client in the list (Code 1), there is no
Other request in the list of the same client or with the same priority.
An identifierKIs always less than 106, and a priorityPIs less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.
Output
For
Each request with Code 2 or 3, the program has to print, in a separate
Line of the standard output, the identifier of the served client. If
Request arrives when the waiting list is empty, then the program prints
Zero (0) to the output.
Sample Input
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
Sample output
0
20
30
10
0
It is a priority queue that can be deleted at both ends. Here we use map and set for simulation.
The Code is as follows:
// This is written using map.
# Include <cstdio> # include <iostream> # include <map> # include <algorithm> using namespace STD; int main () {int op; Map <int, int> MP; while (scanf ("% d", & OP), Op! = 0) {int CLI, PRI; Switch (OP) {Case 1: {scanf ("% d", & CLI, & PRI ); {MP [pri] = CLI;} break;} Case 2: {If (MP. empty () {printf ("0 \ n"); break;} Map <int, int >:: iterator it; it = -- MP. end (); printf ("% d \ n", It-> second); MP. erase (IT-> first); break;} Case 3: {If (MP. empty () {printf ("0 \ n"); break;} Map <int, int >:: iterator it; it = MP. begin (); printf ("% d \ n", It-> second); MP. erase (IT-> first) ;}} return 0 ;}
// Set is used here
# Include <cstdio> # include <iostream> # include <algorithm> # include <set> using namespace STD; struct E {int CLI, PRI; bool operator <(const E & T) const {return pri <t. PRI ;}; int main () {int op; set <E> st; set <e >:: iterator it; while (scanf ("% d ", & OP), Op! = 0) {e; Switch (OP) {Case 1: {scanf ("% d", & E. CLI, & E. PRI); ST. insert (E); break;} Case 2: {If (St. empty () {puts ("0"); break;} It = -- st. end (); printf ("% d \ n", It-> cli); ST. erase (it); break;} Case 3: {If (St. empty () {puts ("0"); break;} It = ST. begin (); printf ("% d \ n", It-> cli); ST. erase (IT) ;}} return 0 ;}