天天看點

HDU - 看病要排隊(優先隊列)

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1873

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

看病要排隊這個是地球人都知道的常識。

不過經過細心的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

2

Sample Output

Problem solving report:

//鍊式
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4;
struct edge {
    int id, num;
};
typedef edge ElemType;
typedef struct ListPtr {
    ElemType data;
    ListPtr *next;
}*lists;
int len[MAXN];
lists head[MAXN], tail[MAXN];
void Alloc(lists &p) {
    p = (ListPtr *)malloc(sizeof(ListPtr));
    p -> next = NULL;
}
void link() {
    for (int i = 0; i < MAXN; i++) {
        len[i] = 0;
        Alloc(head[i]);
        tail[i] = head[i];
    }
}
void push(int k, ElemType e) {
    lists p, r = head[k];
    Alloc(p);
    p -> data = e;
    while (r -> next != NULL && e.num <= r -> next -> data.num)//優先級高的在前面
        r = r -> next;
    p -> next = r -> next;
    r -> next = p;
    len[k]++;
}
void pop(int k) {
    len[k]--;
    lists p = head[k] -> next;
    head[k] -> next = p -> next;
    if (p == tail[k])
        tail[k] = head[k];
    free(p);
}
ElemType front(int k) {//隊頭
    return head[k] -> next -> data;
}
int main() {
    char opt[5];
    int n, apt, bpt;
    while (~scanf("%d", &n)) {
        link();
        int top = 1;
        for (int i = 0; i < n; i++) {
            scanf("%s%d", opt, &apt);
            if (opt[0] != 'O') {
                scanf("%d", &bpt);
                push(apt, (edge){top++, bpt});
            }
            else {
                if (len[apt]) {
                    printf("%d\n", front(apt).id);
                    pop(apt);
                }
                else printf("EMPTY\n");
            }
        }
    }
    return 0;
}           
//STL
#include <bits/stdc++.h>
using namespace std;
struct edge {
    int id, num;
    bool operator < (const edge & s) const {
        if (s.num != num)
            return s.num > num;
        return s.id < id;
    }
};
int main() {
    char opt[5];
    int n, apt, bpt;
    while (~scanf("%d", &n)) {
        int top = 1;
        priority_queue <edge> Q[5];
        for (int i = 0; i < n; i++) {
            scanf("%s%d", opt, &apt);
            if (opt[0] != 'O') {
                scanf("%d", &bpt);
                Q[apt].push((edge){top++, bpt});
            }
            else {
                if (!Q[apt].empty()) {
                    printf("%d\n", Q[apt].top().id);
                    Q[apt].pop();
                }
                else printf("EMPTY\n");
            }
        }
    }
    return 0;
}           

繼續閱讀