UVA 1203 Argus(优先队列)

UVA 1203 Argus(优先队列)

题意:

        给你多个命令,每个命令如Register 2004 200 对应于编号为2004的事件,每隔200秒发生一次(首次发生是在200秒).然后在给你一个K,要你输出前K个发生事件的编号.如果几个事件同时发生,输出事件编号小的.

分析:

        刘汝佳训练指南P188例题.

        本题只需要维护一个优先队列即可,每次从队列中取出时间最小且编号最小的事件,输出其编号,然后将其发生时间加上一个周期再从新放入优先队列即可.

AC代码(新):

#include<queue>
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;

struct Node
{
    int id;
    int time;
    int period;

    Node(int id,int time,int period):id(id),time(time),period(period){}

    bool operator<(const Node &rhs)const
    {
        return time>rhs.time || ( time==rhs.time && id>rhs.id );
    }
};

int main()
{
    string com;//Register
    priority_queue<Node> Q;

    while(cin>>com)
    {
        if(com=="#") break;

        int id,time;
        cin>>id>>time;

        Q.push(Node(id,time,time));
    }

    int k;
    scanf("%d",&k);

    while(k--)
    {
        Node tmp = Q.top(); Q.pop();
        cout<<tmp.id<<endl;

        tmp.time += tmp.period;
        Q.push(tmp);
    }

    return 0;
}

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int num;
    int time;
    int period;
    bool operator<(const node&b)const
    {
        return time>b.time ||(time==b.time&&num>b.num);
    }
};
int main()
{
    priority_queue<node> pq;
    char s[1000];
    int k;
    while(scanf("%s",s)==1&&s[0]!='#')
    {
        node r;
        scanf("%d%d",&r.num,&r.period);
        r.time=r.period;
        pq.push(r);
    }
    scanf("%d",&k);
    while(k--)
    {
        node r=pq.top();pq.pop();
        printf("%d\n",r.num);
        r.time+=r.period;
        pq.push(r);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值