[LA3135]node形式的优先队列

n个触发器,每个触发器每period秒就产生一个编号为qnum的事件,求前k个事件。

n<=1000  k<=10000

 

 

node形式的优先队列

主要在于重载小于号,确定优先顺序。

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<vector>
 8 #include<map>
 9 #include<queue>
10 using namespace std;
11 
12 struct node{
13     int qnum,period,time;
14     bool operator < (const node &a) const {
15         return time > a.time || ( time == a.time  && qnum > a.qnum);
16     }
17 };
18 
19 priority_queue<node> q;
20 char s[20];
21 
22 int main()
23 {
24     //freopen("a.in","r",stdin);
25     //freopen("a.out","w",stdout);
26     
27     node x;
28     while(scanf("%s",s) && s[0]!='#')
29     {
30         scanf("%d%d",&x.qnum,&x.period);
31         x.time=x.period;
32         q.push(x);
33     }
34     
35     int k;
36     scanf("%d",&k);
37     while(k--)
38     {
39         x=q.top();q.pop();
40         printf("%d\n",x.qnum);
41         x.time+=x.period;
42         q.push(x);
43     }
44     
45     return 0;
46 }

 

posted @ 2018-08-11 11:08  拦路雨偏似雪花  阅读(322)  评论(0编辑  收藏  举报