1. 程式人生 > >HDU-1873-看病要排隊 (STL-queue)

HDU-1873-看病要排隊 (STL-queue)

原題連結:
http://acm.hdu.edu.cn/showproblem.php?pid=1873
看病要排隊這個是地球人都知道的常識。
不過經過細心的0068的觀察,他發現了醫院裡排隊還是有講究的。0068所去的醫院有三個醫生(汗,這麼少)同時看病。而看病的人病情有輕重,所以不能根據簡單的先來先服務的原則。所以醫院對每種病情規定了10種不同的優先順序。級別為10的優先權最高,級別為1的優先權最低。醫生在看病時,則會在他的隊伍裡面選擇一個優先權最高的人進行診治。如果遇到兩個優先權一樣的病人的話,則選擇最早來排隊的病人。

現在就請你幫助醫院模擬這個看病過程。
Input
輸入資料包含多組測試,請處理到檔案結束。
每組資料第一行有一個正整數N(0<N<2000)表示發生事件的數目。
接下來有N行分別表示發生的事件。
一共有兩種事件:
1:“IN A B”,表示有一個擁有優先順序B的病人要求醫生A診治。(0<A<=3,0<B<=10)
2:“OUT A”,表示醫生A進行了一次診治,診治完畢後,病人出院。(0<A<=3)
Output
對於每個"OUT A"事件,請在一行裡面輸出被診治人的編號ID。如果該事件時無病人需要診治,則輸出"EMPTY"。
診治人的編號ID的定義為:在一組測試中,"IN A B"事件發生第K次時,進來的病人ID即為K。從1開始編號。
Sample Input
7
IN 1 1
IN 1 2
OUT 1
OUT 2
IN 2 1
OUT 2
OUT 1
2
IN 1 1
OUT 1
Sample Output
2
EMPTY
3
1
1
題意:
中文題
題解:
剛好今天看了一下stl的容器, 拿來練個手,這道題目用優先佇列去做就好了,具體看程式碼。
附上AC程式碼:

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
using namespace std;
int n;
struct cmp//重定義排序方式
{
        template<typename T, typename U>
        bool operator()(T const& left, U const &right) {
            if (left.first < right.first) return true;
            else if(left.first==right.first&&left.second>right.second) return true;
            return false;
        }
};
int main()
{
    char op[4];
    int dran,pran;
    while(scanf("%d",&n)!=EOF)
    {
        priority_queue<pair<int,int>,vector<pair<int,int> >,cmp> doc1;
        priority_queue<pair<int,int>,vector<pair<int,int> >,cmp> doc2;
        priority_queue<pair<int,int>,vector<pair<int,int> >,cmp> doc3;
        int id=1;
        while(n--)
        {
            scanf("%s",op);
            if(op[0]=='I')//插入新病人
            {
                scanf("%d%d",&dran,&pran);
                pair<int,int> tmp(pran,id++);
                if(dran==1)
                    doc1.push(tmp);
                else if(dran==2)
                    doc2.push(tmp);
                else if(dran==3)
                    doc3.push(tmp);
            }
            else if(op[0]=='O')//診斷好一個病人
            {
                scanf("%d",&dran);
                if(dran==1)
                {
                    if(doc1.empty())
                        printf("EMPTY\n");
                    else
                    {
                        printf("%d\n",doc1.top().second);
                        doc1.pop();
                    }
                }
                else if(dran==2)
                {
                    if(doc2.empty())
                    printf("EMPTY\n");
                    else
                    {
                        printf("%d\n",doc2.top().second);
                        doc2.pop();
                    }
                }
                else if(dran==3)
                {
                    if(doc3.empty())
                    printf("EMPTY\n");
                    else
                    {
                        printf("%d\n",doc3.top().second);
                        doc3.pop();
                    }
                }
            }
        }
    }
    return 0;
}

歡迎評論!