题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1873
第一次写的优先队列题,准确来说不是写,是从网上找资料再从那里copy过来的。。。。。。。
不过还是要保存下来学习。。。。。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct patient
{
int num;/*编号*/
int imp;/*重要度*/
}w[1001];
struct comp
{
bool operator()(patient &x,patient &y)/*结构体排序*/
{
if(x.imp<y.imp) return true;
if(x.imp==y.imp&&x.num>y.num) return true;
return false;
}
};
int main()
{
int n,i,a,b,count;
char str[101];
while(~scanf("%d",&n))
{
priority_queue<patient,vector<patient>,comp>doc[4];/*优先队列*/
count=1;
while(n--)
{
scanf("%s",str);
if(str[0]=='I')
{
scanf("%d %d",&a,&b);
w[count].num=count;
w[count].imp=b;
doc[a].push(w[count]);/*直接插入一个结构体....*/
count++;
}
else
{
scanf("%d",&a);
if(doc[a].empty())
{
printf("EMPTY\n");
}
else
{
printf("%d\n",doc[a].top().num);
doc[a].pop();
}
}
}
}
return 0;
}

本文介绍了一个使用C++实现的优先队列程序实例,该程序通过处理病人的编号和重要度来模拟医院挂号系统的调度逻辑。文章提供的代码展示了如何定义结构体、比较函数以及如何操作优先队列。
297

被折叠的 条评论
为什么被折叠?



