本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1509
本题运用优先队列来解决,并运用了结构体的一些知识,AC代码如下:
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
struct node{
char name[100];
int x,y;
int num;
friend bool operator < (node a,node b)
{
if(a.y!=b.y)
return a.y>b.y;
else return a.num>b.num;
}
};
int main()
{
priority_queue <node> q;
char a[100];
int count=1;
node Q;
while(scanf("%s",a)!=EOF)
{
if(!strcmp(a,"GET"))
{
if(q.empty())
printf("EMPTY QUEUE!\n");
else
{
printf("%s %d\n",q.top().name,q.top().x);
q.pop();
}
}
else if(!strcmp(a,"PUT"))
{
scanf("%s %d %d",Q.name,&Q.x,&Q.y);
Q.num=count;
count++;
q.push(Q);
}
}
return 0;
}