您的位置:首页 > 其它

Poj 2051 Argus

2016-04-05 18:58 369 查看
题意理解:
(1)每个注册用户均有一个注册ID和一个时间间隔
(2)针对每隔用户,每隔一个自己的时间间隔该ID打印一次
(3)#说明输入到结尾处,没有用户注册了
(4)最后一行的数字为打印的次数
使用了一个优先权队列,把所有的注册用户放入该队列中,队列的排序按照要出现的时间从小到大排序,如果时间有冲突就按照id升序。每次这个用户id输出后,先把该用户从队列里pop出来,然后把该用户的出现时间加上他的时间间隔,然后再push到优先权队列中,由于这个队列是按照要求排序的,所以每次在队头的那个元素就是要打印的ID

1 #include <iostream>
2 #include <queue>
3 #include <string>
4 using namespace std;
5 struct node{
6     int IntIdNum;//用户注册的ID
7     int IntPeroid;//间隔时间
8     int IntNextTime;//下次出现的时间
9     friend bool operator <(node a,node b){//运算符重载
10         if(a.IntNextTime != b.IntNextTime)
11             return (a.IntNextTime > b.IntNextTime);//时间不等按时间输出
12         else return (a.IntIdNum > b.IntIdNum);//时间相等按id输出
13     }
14 };
15 int Id,peroid,OutNum;
16 int main()
17 {
18     //freopen("D:\\t.txt","r",stdin);
19
20     string str;
21     priority_queue <node> SaveQue;//创建优先队列,'<'对Push进去的数据进行处理
22     while(cin>>str && str != "#"){
23         cin>>Id>>peroid;//输入及初始化
24         node Input;
25         Input.IntIdNum = Id;
26         Input.IntPeroid = peroid;
27         Input.IntNextTime = peroid;
28         SaveQue.push(Input);
29         }
30         cin >> OutNum;
31         for(int i = 0;i < OutNum;i++){
32             node Output;
33             Output = SaveQue.top();
34             SaveQue.pop();
35             cout<< Output.IntIdNum<<endl;
36             Output.IntNextTime += Output.IntPeroid;//
37             SaveQue.push(Output);
38     }
39
40     return 0;
41 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: