POJ 3481 Double Queue

来源:互联网 发布:林志玲人品如何 知乎 编辑:程序博客网 时间:2024/04/30 00:48

Description

The new founded Balkan Investment Group Bank(BIG-Bank) opened a new office in Bucharest, equipped with a modern computingenvironment provided by IBM Romania, and using modern information technologies.As usual, each client of the bank is identified by a positive integer Kand, upon arriving to the bank for some services, he or she receives a positiveinteger priorityP. One of the inventions of the young managers of thebank shocked the software engineer of the serving system. They proposed tobreak the tradition by sometimes calling the serving desk with thelowestpriority instead of that with the highest priority. Thus, the system willreceive the following types of request:

0

The system needs to stop serving

1 K P

Add client K to the waiting list with priority P

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 bankby writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possiblerequests; only the last line contains the stop-request (code 0). You may assumethat 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 samepriority. An identifier K is always less than 106, and apriorityP is less than 107. The client may arrive for beingserved multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has toprint, in a separate line of the standard output, the identifier of the servedclient. If the request arrives when the waiting list is empty, then the programprints 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

 

题目简介:对数据进行一些列的操作。0、1、2、3为不同的指令。输入1,表示输入两个数,第二个是第一个数所在位置。输入2,表示输出位置最高位上的数,同时删除。输入3,表示输出位置最低位上的数,同时删除。0表示停止操作。

方法:map

从后面删除时必须用L.erase(L.rbegin()->first从前面删除时可以L.erase(L.begin()->first);也可以L.erase(L.rbegin())。不知为何,表示菜鸟还没学数据结构不知道。。。。。。。。求大神教育

 

#include<iostream>#include<cstdio>#include<map>using namespace std;int main(){int index, m, k;map<int,int>L;while(scanf("%d",&index),index){if(index==1){scanf("%d%d",&m, &k);L[k] = m;}else if(index==2){if(L.empty()){printf("0\n");}else{printf("%d\n",L.rbegin()->second);L.erase(L.rbegin()->first);}}else{printf("%d\n",L.begin()->second);L.erase(L.begin()->first);}}return 0;}


 

原创粉丝点击