hdu 1873 看病要排队

本文介绍两种使用C++实现自定义优先级队列的方法。一种是通过重载比较运算符来定义节点的优先级,另一种是通过自定义比较函数对象实现。这两种方法均应用于解决任务调度问题,涉及优先队列的插入与删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

两种自定义优先级写法

View Code
 1 #include<iostream>
 2 #include<queue>
 3 #include<string>
 4 using namespace std;
 5 struct Node
 6 {
 7     int time;
 8     int import;
 9     bool operator<(const Node &a) const
10     {
11         if(a.import>import) return true;
12         else if(a.import==import&&a.time<time) return true;
13         else return false;
14     }
15 }p[2002];
16 int main()
17 {
18     
19     
20     
21     int n;
22     string str1;
23     string str2;
24     string str;
25     int d,i;
26     Node node;
27     int count;
28     while(~scanf("%d",&n))
29     {
30         count=1;
31         priority_queue<Node>doc[4];
32         while(n--)
33         {
34             
35             
36             cin>>str;
37             if(str[0]=='I')
38             {
39                 
40                 
41                 scanf("%d%d",&d,&i);
42                 p[count].time=count;
43                 p[count].import=i;
44                 doc[d].push(p[count]);
45                 count++;
46                 
47             }
48             else
49             {
50                
51                
52                
53                scanf("%d",&d);
54                if(doc[d].empty()==true)
55                {
56                      printf("EMPTY\n");
57                      
58                   }
59                   
60                 else 
61                 {
62                     
63                     printf("%d\n",doc[d].top().time);
64                     doc[d].pop();
65                 }    
66             }
67         }
68         
69     }
70 }
View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6 struct patient
 7 {
 8     int num;/*编号*/
 9     int imp;/*重要度*/
10 }w[1001];
11 struct comp
12 {
13     bool operator()(patient &x,patient &y)/*结构体排序*/
14     {
15         if(x.imp<y.imp) return true;
16         if(x.imp==y.imp&&x.num>y.num) return true;
17         return false;
18     }
19 };
20 int main()
21 {
22     int n,i,a,b,count;
23     char str[101];
24     while(~scanf("%d",&n))
25     {
26         priority_queue<patient,vector<patient>,comp>doc[4];/*优先队列*/
27         count=1;
28         while(n--)
29         {
30             scanf("%s",str);
31             if(str[0]=='I')
32             {
33                 scanf("%d %d",&a,&b);
34                 w[count].num=count;
35                 w[count].imp=b;
36                 doc[a].push(w[count]);/*直接插入一个结构体....*/
37                 count++;
38             }
39             else
40             {       
41                 scanf("%d",&a);
42                 if(doc[a].empty())
43                 {
44                     printf("EMPTY\n");
45                 }
46                 else
47                 {
48                     printf("%d\n",doc[a].top().num);
49                     doc[a].pop();
50                 }
51             }
52         }
53     }
54     return 0;
55 }

 

 

转载于:https://www.cnblogs.com/1114250779boke/archive/2012/08/07/2626596.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值