(priority_queue)自定义优先级
思考與總結(jié):
1.優(yōu)先隊列,先出隊列元素不是先進隊列的元素,而是隊列中優(yōu)先級最高的元素
2.遇到這種題一般把每一個數(shù)據(jù)封裝到一個struct里
3.然后根據(jù)優(yōu)先級判斷依據(jù),我們通過重定向定義優(yōu)先隊列的優(yōu)先級
如果我們寫bool operator <
下面return里a< b,意味著a的值比b小的話,優(yōu)先級小
4.我們把數(shù)據(jù)輸入優(yōu)先隊列里,那隊列里就自動為我們排好序了
輸出的時候,取隊列頂端,就把優(yōu)先級大的取出來了
題目1:
Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message from the message queue.
Input
There’s only one test case in the input. Each line is a command, “GET” or “PUT”, which means getting message or putting message. If the command is “PUT”, there’re one string means the message name and two integer means the parameter and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.
Output
For each “GET” command, output the command getting from the message queue with the name and parameter in one line. If there’s no message in the queue, output “EMPTY QUEUE!”. There’s no output for “PUT” command.
Sample Input
GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET
Sample Output
EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!
ac代碼:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<queue> #include<vector> #include<string> using namespace std;struct node{string a;int b;int c;int num; }; bool operator < (const node &x,const node&y){if(x.c==y.c){return x.num>y.num;}else{return x.c>y.c;} }int main(){char a[5];priority_queue<node>pq;int n=1;while(cin>>a){if(a[0]=='P'){struct node l;cin>>l.a;cin>>l.b;cin>>l.c;l.num=n++;pq.push(l);}if(a[0]=='G'){if(!pq.empty()){struct node ll=pq.top();pq.pop();cout<<ll.a<<' '<<ll.b<<endl;}else{cout<<"EMPTY QUEUE!"<<endl;}}} }題目2:
看病要排隊這個是地球人都知道的常識。
不過經(jīng)過細心的0068的觀察,他發(fā)現(xiàn)了醫(yī)院里排隊還是有講究的。0068所去的醫(yī)院有三個醫(yī)生(汗,這么少)同時看病。而看病的人病情有輕重,所以不能根據(jù)簡單的先來先服務(wù)的原則。所以醫(yī)院對每種病情規(guī)定了10種不同的優(yōu)先級。級別為10的優(yōu)先權(quán)最高,級別為1的優(yōu)先權(quán)最低。醫(yī)生在看病時,則會在他的隊伍里面選擇一個優(yōu)先權(quán)最高的人進行診治。如果遇到兩個優(yōu)先權(quán)一樣的病人的話,則選擇最早來排隊的病人。
現(xiàn)在就請你幫助醫(yī)院模擬這個看病過程。
Input
輸入數(shù)據(jù)包含多組測試,請?zhí)幚淼轿募Y(jié)束。
每組數(shù)據(jù)第一行有一個正整數(shù)N(0< N<2000)表示發(fā)生事件的數(shù)目。
接下來有N行分別表示發(fā)生的事件。
一共有兩種事件:
1:”IN A B”,表示有一個擁有優(yōu)先級B的病人要求醫(yī)生A診治。
(0 < A<=3,0< B< =10)
2:”O(jiān)UT A”,表示醫(yī)生A進行了一次診治,診治完畢后,病人出院。(0< A< =3)
Output
對于每個”O(jiān)UT A”事件,請在一行里面輸出被診治人的編號ID。如果該事件時無病人需要診治,則輸出”EMPTY”。
診治人的編號ID的定義為:在一組測試中,”IN A B”事件發(fā)生第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
ac代碼:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<queue> #include<string> using namespace std;struct node {int x, y; }; bool operator< (const node& a, const node& b) {if(a.x!=b.x){return a.x < b.x;}else{return a.y > b.y;} }int main() {int n;while(scanf("%d",&n)!=EOF){int h = 1;priority_queue<node>q[4];string aa;int a,b;for(int i=0;i<n;i++){cin >> aa;if(aa == "IN"){struct node p;scanf("%d%d",&a,&b);p.x = b;p.y = h;q[a].push(p);h++;}else{scanf("%d",&a);if(!q[a].empty()){struct node tt;tt = q[a].top();q[a].pop();printf("%d\n",tt.y);}else{printf("EMPTY\n");}}}}return 0; }總結(jié)
以上是生活随笔為你收集整理的(priority_queue)自定义优先级的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在安卓手机上下载linux系统,如何在安
- 下一篇: python从数据库取数据 显示字段名_