1. 程式人生 > >POJ 3481 Double Queue(set實現)

POJ 3481 Double Queue(set實現)

Double Queue

  The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment 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 positive integer priority P

. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

0 The system needs to stop serving
KP 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 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 identifier K is always less than 106, and a priority P is 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 the 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

解題思路:
  本題要求處理銀行排隊問題,根據每行的輸入處理資料;若輸入為1則繼續輸入兩個整數,以第一個數為權值,第二個數為優先順序(數值越大優先順序越高)加入排隊系統中;若輸入為2 則處理當前優先順序最高的使用者,並輸出使用者的權值;若輸入為3 則處理當前優先順序最低的使用者,並輸出使用者權值;若輸入為0結束執行。若當沒有排隊人數則無論輸入2、3都輸出0。

  利用set自帶紅黑樹排序的性質,可以在集合中直接獲取最高與最低優先順序,用pair記錄權值和優先順序,將pair傳入集合。用迭代器可以獲得集合首位(最大優先順序)和集合末尾(最小優先順序),資料處理完畢後可以通過erase()刪除當前使用者以便後續操作。

樣例分析:
Sample Input

2          //處理高優先順序使用者,當前無人排隊輸出0
1 20 14      //優先順序為14的使用者20開始排隊 
1 30 3       //優先順序為3的使用者30開始排隊
2          //處理高優先順序使用者,當前佇列中有 20 14   30 3 最高優先順序14 輸出20 
1 10 99      //優先順序為99的使用者10開始排隊 
3          //處理低優先順序使用者,當前佇列中有 10 99   30 3 最低優先順序3 輸出30
2          //處理高優先順序使用者,當前佇列中有 10 99 最高優先順序99 輸出10
2          //處理高優先順序使用者, 當前無人排隊輸出0
0          //輸入為0結束

 1 #include <cstdio>
 2 #include <iterator>
 3 #include <set>
 4 //bits/stdc++.h編譯錯誤
 5 using namespace std;
 6 //利用set的排序性質
 7 struct cmp{ //set比較方法以pair的第二個成員為基準,數值越大的在前
 8     bool operator() (const pair<int, int> &a, const pair<int, int> &b)
 9     {
10         return a.second > b.second;
11     }
12 };
13 set<pair<int, int>, cmp> waitingList;  //用set記錄當前佇列
14 int main()
15 {
16     int n;
17     while(scanf("%d", &n) != EOF){  //輸入資料
18         if(n == 0)  //輸入為0結束執行
19             break;
20         if(n == 1){ //輸入為1
21             pair<int, int> temp;
22             scanf("%d%d", &temp.first, &temp.second);   //記錄使用者權值與優先順序
23             waitingList.insert(temp);   //使用者開始排隊
24         }
25         if(n == 2){ //輸入為2
26             if(!waitingList.empty()){
27                 set<pair<int, int>, cmp>::iterator it = waitingList.begin();
28                 //獲取優先順序最高的使用者
29                 printf("%d\n", (*it).first);    //輸出使用者權值
30                 waitingList.erase(it);  //刪除使用者
31             }else{
32                 printf("0\n");  //隊中無人
33             }
34         }
35         if(n == 3){
36             if(!waitingList.empty()){
37                 set<pair<int, int>, cmp>::iterator it = waitingList.end();
38                 it--;
39                 //獲取優先順序最小的使用者
40                 printf("%d\n", (*it).first);    //輸出使用者權值
41                 waitingList.erase(it);  //刪除使用者
42             }else{
43                 printf("0\n");//隊中無人
44             }
45         }
46     }
47     return 0;
48 }