(priority_queue)⾃定义优先级
思考与总结:
1.优先队列,先出队列元素不是先进队列的元素,⽽是队列中优先级最⾼的元素
2.遇到这种题⼀般把每⼀个数据封装到⼀个struct⾥
3.然后根据优先级判断依据,我们通过重定向定义优先队列的优先级烟酒茶
如果我们写bool operator <
下⾯return⾥a< b,意味着a的值⽐b⼩的话,优先级⼩
4.我们把数据输⼊优先队列⾥,那队列⾥就⾃动为我们排好序了
输出的时候,取队列顶端,就把优先级⼤的取出来了
题⽬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 mou 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 ca 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 procesd 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 out
put 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;
}
el{
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 p();
pq.pop();
cout<<ll.a<<' '<<ll.b<<endl;
}
el{
cout<<"EMPTY QUEUE!"<<endl;
}
}
}
}
题⽬2:
看病要排队这个是地球⼈都知道的常识。
不过经过细⼼的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
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;
}
el
{
return a.y > b.y;
}
}
int main()
{
int n;
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++;
}
el
{
scanf("%d",&a);
if(!q[a].empty())
{
struct node tt;
tt = q[a].top();
q[a].pop();
printf("%d\n",tt.y); }
el
{
printf("EMPTY\n"); }
}
}
}
return0;
}