POJ 2051 Argus STL 优先队列

POJ 2051

题意为:给出一系列值和其出现的时间周期,这些数值都将以其周期为规则不断地出现,问从零时刻开始前k个出现的数值,依次输出。若某时刻出现的数值不只一个,则按照从小到大的顺序输出。

显然这是优先队列的应用。

优先队列,priority_queue,可以直接借助STL实现,是一个在队列基础上给队列元素增加了优先级的数据结构,一般形式为:

priority_queue<type,container,fuction>q

其中type是定义队列元素的类型,可以是基础的int,double等,也可以是自定义的类型(struct)

container是容器,默认为vector;

function在此为比较函数,默认为operator< ,如果有需要可以重载运算符或者自己写一个新的判断方式;

在使用priotity_queue时,上面三个参数中必须指明元素类型,后两个参数缺省时保持默认的设定

这个题目的想法并不是很复杂,每次输出一个条目,就向队列中压入同样值的一个条目,其出现时间要比已经输出的条目多一个周期,为此定义结构体node,对每一个条目存储值,时间和周期。

code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<cmath>
#define maxn 100010
using namespace std;
struct node
{
    int value,time,period;
};
bool operator<(node a,node b)
{
    if(a.time==b.time)
        return a.value>b.value;
    return a.time>b.time;
}
priority_queue<node>q;
char op[20];
int k;
int main()
{
    while(cin>>op)
    {
        if(op[0]=='#')break;
        node tmp;
        scanf("%d%d",&tmp.value,&tmp.time);
        tmp.period=tmp.time;
        q.push(tmp);
    }
    scanf("%d",&k);
    while(k--)
    {
        node tmp=q.top();
        q.pop();
        printf("%d\n",tmp.value);
        node t=tmp;
        t.time+=t.period;
        q.push(t);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值