HDU 1509 Windows Message Queue
明显的优先队列
用了STL,但是不全懂,自己写个堆估计悬... 有工具咱就用!
#include <iostream>
#include <queue>
#include <vector>
#include <stdio.h>
using namespace std;
const int MAXN = 60001;
struct message
{
char name[20];
int para,pri;
int order;
bool operator < (const message& m1)const
{
if(pri == m1.pri)
return order > m1.order;
return pri < m1.pri;
}
};
struct cmp
{
bool operator()(const message &m1, const message &m2){
if(m1.pri == m2.pri)
return m1.order > m2.order;
return m1.pri > m2.pri;
}
};
//以上全不懂,全是试出来的,C++ stl 没时间看啊!
priority_queue<message,vector<message>,cmp> q;
int main(){
char str[5],name[20];
int para,pri,cnt = 1;
while(scanf("%s",str) == 1){
switch (str[0])
{
case 'G':
if(q.empty())
printf("EMPTY QUEUE!\n");
else {
message mout = q.top();
q.pop();
printf("%s %d\n",mout.name,mout.para);
}
break;
case 'P':
scanf("%s%d%d",name,¶,&pri);
message m ;
strcpy(m.name,name);
m.para = para;
m.pri = pri;
m.order = cnt++;
q.push(m);
break;
}
}
return 0;
}