2830 蓬莱山辉夜 优先队列的简单应用

在幻想乡中,蓬莱山辉夜是月球公主,居住在永远亭上,二次设定说她成天宅在家里玩电脑,亦称NEET姬
一天,她要她帮忙升级月球的网络服务器,应为注册用户过多(月兔和地球上的巫女都注册了……),所以作为代理管理员(俗称网管)的她,非常蛋疼。
注册用户格式:
TouhouMaiden 2004 200
其中前面的Touhoumaiden是预设,不做更改,第一个数是标识,第二个数是每次接受信息访问的间隔用时。

你要做的事,就是给定一群用户及n,求出这n次信息访问中,访问到了谁?


完整题目连接:http://codevs.cn/problem/2830/

输入描述 Input Description

以题目预设格式输入,另起一行以‘#’结束,在其一行输入n

输出描述 Output Description

n行,每行输出第行次后,信息访问到了谁?若在一个时间有若干少女被访问到,输出字典序最小的那位少女的标识

样例输入 Sample Input
TouhouMaiden 2004 200
TouhouMaiden 2005 300
#
5
样例输出 Sample Output
2004
2005
2004
2004
2005
分析:

题目扯这么多其实就是说,给定一个编号,和时间间隔,问按时间进行若干次对某编号的对象访问,最后的访问序列是什么?

如给出的描述数据,2004第一次访问是因为 它的间隔为200,而2005为300所以后访问。而第一访问完2004后,下一次要在600的时候访问。

所以访问时间再加上200,再丢到优先队列中即可。每次取队头元素,打印,更新后在插入队列,直到n个访问完毕。

代码:

#include <iostream>
#include <queue>
#include <string>
#include <cstdio>
using namespace std;
struct node
{
	int step;
	int curtime;
	int id;
};

struct cmp_node
{
	bool operator()(node &a,node &b)
	{
		if (a.step != b.step)
		{
			return a.step > b.step;
		}
		else if (a.id != b.id)
		{
			return a.id > b.id;
		}
		return false;
	}
};

int n;
priority_queue<node,vector<node>,cmp_node > que;
void input()
{
	string s;
	node o;
	cin >> s;
	while (s != "#")
	{
		cin >> o.id >> o.step >> s;
		o.curtime = o.step;
		que.push(o);
	}
	cin >> n;
}

void cal()
{
	node t;
	while (n > 0)
	{
		t = que.top();
		que.pop();
		printf("%d\n",t.id);
		t.step = t.step + t.curtime;
		que.push(t);
		n = n - 1;
	}

}

int main() {
	
	input();
	cal();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值