您的位置:首页 > 其它

优先队列结构体重载的二级排序

2016-08-22 20:17 309 查看
优先结构体的重载的二级排序

struct node
{
int bh, jb;
friend bool operator < (node a, node b){
if(a.jb==b.jb)
{
return a.bh>b.bh; //以bh小的优先
}
return a.jb<b.jb; //以级别大的优先;
}
};


这就是重载二级排序;

看例题hdu1873;

题目大意就是;根据病人的优先级以及先后顺序二级排序;

1:N A B”,表示有一个拥有优先级B的病人要求医生A诊治

2OUT A”,表示医生A进行了一次诊治,诊治完毕后,病人出院。

对于每个”OUT A”事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出”EMPTY”。

诊治人的编号ID的定义为:在一组测试中,”IN A B”事件发生第K次时,进来的病人ID即为K。从1开始编号。

http://acm.split.hdu.edu.cn/showproblem.php?pid=1873

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node { int bh, jb; friend bool operator < (node a, node b){ if(a.jb==b.jb) { return a.bh>b.bh; //以bh小的优先 } return a.jb<b.jb; //以级别大的优先; } };
int main()
{
int t, i, j, a, b;
char ch[100];
while(~scanf("%d",&t))
{
node now, next;
priority_queue<node> q1,q2,q3;
int l = 1;
for(i = 1; i <= t; i++)
{
scanf("%s",ch);
if(strcmp(ch,"IN")==0)
{
scanf("%d %d",&a,&b);
if(a == 1)
{
now.bh = l;
l++;
now.jb = b;
q1.push(now);
}
else if(a == 2)
{
now.bh = l;
l++;
now.jb = b;
q2.push(now);
}
else if(a == 3)
{
now.bh = l;
l++;
now.jb = b;
q3.push(now);
}
}
else
{
scanf("%d",&a);
if(a == 1)
{
if(q1.size() == 0)
{
printf("EMPTY\n");
}
else
{
printf("%d\n",q1.top());
q1.pop();
}
}
else if(a == 2)
{
if(q2.size() == 0)
{
printf("EMPTY\n");
}
else
{
printf("%d\n",q2.top());
q2.pop();
}
}
else if(a == 3)
{
if(q3.size() == 0)
{
printf("EMPTY\n");
}
else
{
printf("%d\n",q3.top());
q3.pop();
}
}
}
}
}

return 0;
}


hdu1509;同样是重载中的二级排序;

题目连接;http://acm.split.hdu.edu.cn/showproblem.php?pid=1509

注意题目;f two messages have the same priority, the one comes first will be processed first.;

也与先后顺序有关系的;之前没注意一直wa;

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
//看清题目;if two messages have the same priority,
// the one comes first will be processed first
//则要二级排序;
struct node
{
char name[55];
int cs;
int jb;
int num;
friend bool operator < (node a, node b){
if(a.jb == b.jb)
return a.num > b.num;
else return a.jb > b.jb;
}
};
int main()
{
priority_queue<node> q;
node now;
char ch[10];
int k =0;
while(~scanf("%s",ch))
{
if(strcmp(ch,"GET")==0)
{
if(q.empty())printf("EMPTY QUEUE!\n");
else{
now = q.top();
q.pop();
printf("%s %d\n",now.name, now.cs);
}
}
else
{
scanf("%s %d %d",now.name, &now.cs, &now.jb);
now.num = k++;
q.push(now);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息