学优先队列 时,看到了一个莫名的函数, bool operator <(const node &x)const ;当时没注意,今天比赛就 sad..........了。
题目链接:点击打开链接
题意:比较简单,模拟计算机处理数据, PUT msg1 10 5, 其中PUT表示输出信息,msg1表示信息的编号,10表示信息的内容,5表示优先级(数字越小优先级越高)。GET:表示输出信息,优先级高的先输出。例如本题中 : 输入两条信息后 PUT msg1 10 5
PUT msg2 10 4
如果执行 两个GET命令,则会先输出msg2 10 ,然后在输出msg1 10。
思路:优先队列,需要编一个根据 “优先级 ”进行输出的重载运算函数。
其中用到了重载概念,链接:点击打开链接
代码如下:
#include<stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct node
{
char name[10];
int m, n;
bool operator<(const node &x)const
{
return x.n < n;
}
}p;
int main ()
{
priority_queue <node>q;
char s[20];
while (~scanf("%s",s))
{
if(strcmp(s, "GET") == 0)
{
if (q.empty ()==1)
puts ("EMPTY QUEUE!");
else{
struct node t = q.top ();
printf("%s %d\n", t.name, t.m);
q.pop();
}
}
if(strcmp(s, "PUT")==0)
{
scanf("%s %d %d", p.name, &p.m, &p.n);
q.push (p);
}
}
return 0;
}