LA - 3135 - Argus

题意:一个ID对应一个周期,不同的ID对应不同的周期,总共可能有3000个ID,输出先返回的K个ID。(当时刻相同时先返回ID小的)

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=14&page=show_problem&problem=1136

——>>这题即zoj - 2212(当时的思路:http://blog.csdn.net/scnu_jiechao/article/details/8273149),在书上汝佳说重载运算符 < 的时候,const必不可少,于是今天就加了这个const进去,虽说不加这个const也AC得了,但我还是不太明白,不加这个const会出现什么样的后果呢?望各位赐教。

#include <iostream>
#include <queue>

using namespace std;

struct node     //结点类型
{
    int Q_num;      //ID
    int base;       //基底,处理过程中要维护这个基底
    int Period;     //间隔
};

bool operator < (const node e1, const node e2)      //定义优先队列的比较规则,时刻不同,先返回早的,时刻相同,先返回ID小的
{
    return ((e1.Period > e2.Period) || (e1.Period == e2.Period && e1.Q_num > e2.Q_num));
}

int main()
{
    int K;
    string s;       //输入的第一个参数
    node newnode;
    priority_queue<node> qu;        //利用优先队列来处理
    while(cin>>s)
    {
        if(s == "#") break;
        cin>>newnode.Q_num>>newnode.Period;
        newnode.base = newnode.Period;      //基底不可改,需要维护
        qu.push(newnode);       //入列
    }
    cin>>K;
    int cnt = 1;        //计数器
    while(cnt <= K)
    {
        cout<<qu.top().Q_num<<endl;
        newnode.Q_num = qu.top().Q_num;     //输出之后,把这点取出来,下次到来的时刻就是目前时刻加上基底
        newnode.Period = qu.top().Period + qu.top().base;
        newnode.base = qu.top().base;
        qu.pop();
        qu.push(newnode);       //处理后入列
        cnt++;
    }
    return 0;
}



posted @ 2013-01-03 12:43  xiaodanding  阅读(99)  评论(0编辑  收藏  举报