(算法竞赛入门经典 优先队列)LA 3135(前K条指令)

A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logs and telephone call records. Likewise, queries over streams run continuously over a period of time and incrementally return new results as new data arrives. For example, a temperature detection system of a factory warehouse may run queries like the following.

Query-1: ?Every five minutes, retrieve the maximum temperature over the past five minutes.? Query-2: ?Return the average temperature measured on each floor over the past 10 minutes.?

We have developed a Data Stream Management System called Argus, which processes the queries over the data streams. Users can register queries to the Argus. Argus will keep the queries running over the changing data and return the results to the corresponding user with the desired frequency.

For the Argus, we use the following instruction to register a query:

Register Q_num Period

Q_num (0 < Q_num ≤ 3000) is query ID-number, and Period (0 < Period ≤ 3000) is the interval between two consecutive returns of the result. After Period seconds of register, the result will be returned for the first time, and after that, the result will be returned every Period seconds.

Here we have several different queries registered in Argus at once. It is confirmed that all the queries have different Q_num. Your task is to tell the first K queries to return the results. If two or more queries are to return the results at the same time, they will return the results one by one in the ascending order of Q_num.

Input

The first part of the input are the register instructions to Argus, one instruction per line. You can assume the number of the instructions will not exceed 1000, and all these instructions are executed at the same time. This part is ended with a line of ?#?.

The second part is your task. This part contains only one line, which is one positive integer K (≤ 10000).

Output

You should output the Q_num of the first K queries to return the results, one number per line.

Sample Input

Register 2004 200
Register 2005 300
#
5

Sample output

2004
2005
2004
2004
2005

代码如下:

/*
 * LA_3135.cpp
 *
 *  Created on: 2014年8月1日
 *      Author: pc
 */

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

struct Item{
	int num;//指令的序号
	int period;//指令的执行周期
	int time;//指令的下一次执行时间

	bool operator<(const Item& b)const{
		if(time != b.time){
			return time > b.time;
		}

		return num > b.num;
	}
};

int main(){
	char s[20];

	priority_queue<Item> pq;
	while(scanf("%s",&s),s[0] != ‘#‘){
		Item item;
		scanf("%d%d",&item.num,&item.period);
		item.time = item.period;
		pq.push(item);
	}

	int k;
	scanf("%d",&k);
	while(k--){//核心逻辑
		Item r = pq.top();//不断的取出来然后不断的插进去
		pq.pop();

		printf("%d\n",r.num);
		r.time += r.period;

		pq.push(r);
	}

	return 0;
}

(算法竞赛入门经典 优先队列)LA 3135(前K条指令)

时间: 2024-10-18 13:01:07

(算法竞赛入门经典 优先队列)LA 3135(前K条指令)的相关文章

(DS 《算法竞赛入门经典》)LA 3644 X-Plosives(并查集)

解题思路: 并查集 A secret service developed a new kind of explosive that attain its volatile property only when a specificassociation of products occurs. Each product is a mix of two different simple compounds, to which wecall a binding pair. If N > 2, then

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意: 查询某一个节点到根节点之间的距离 解题思路: 加权并查集问题.之前做的题目是"查看两个或多个节点是否在同一个集合下",现在的题目是"查询某个节点到 根节点之间的距离".之前只需要使用到father[x]这个数组,用来表示x的父亲节点是谁.现在引入dist[x]数组,用来记录 x节点到根节点的距离 1)在并查集中,根节点不懂,其他节点都可以动. A very big corporation is developing its corporative net

《算法竞赛入门经典(第二版)》pdf

下载地址:网盘下载 内容简介  · · · · · · <算法竞赛入门经典(第2版)>是一本算法竞赛的入门与提高教材,把C/C++语言.算法和解题有机地结合在一起,淡化理论,注重学习方法和实践技巧.全书内容分为12 章,包括程序设计入门.循环结构程序设计.数组和字符串.函数和递归.C++与STL入门.数据结构基础.暴力求解法.高效算法设计.动态规划初步.数学概念与方法.图论模型与算法.高级专题等内容,覆盖了算法竞赛入门和提高所需的主要知识点,并含有大量例题和习题.书中的代码规范.简洁.易懂,不

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

棋盘覆盖问题(算法竞赛入门经典)

在一个 2^k * 2^k 个方格组成的棋盘中,若恰有一个方格与其它方格不同,则称该方格为一特殊方格,称该棋盘为一特殊棋盘.显然特殊方格在棋盘上出现的位置有 4^k 种情形.因而对任何 k>=0 ,有 4^k 种不同的特殊棋盘.下图所示的特殊棋盘为 k=2 时 16 个特殊棋盘中的一个. 在棋盘覆盖问题中,要用下图中 4 中不同形态的 L 型骨牌覆盖一个给定的特殊棋牌上除特殊方格以外的所有方格,且任何 2 个 L 型骨牌不得重叠覆盖.易知,在任何一个 2^k * 2^k 的棋盘中,用到的 L 型

(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大

《算法竞赛入门经典第二版》 P35 习题2-4 子序列的和(subsequence)

/* <算法竞赛入门经典第二版> P35 习题2-4: 输入两个正整数 n < m < 10^6,输出 (1/n)^2 + 1/(n+1)^2 +……+ 1/m^2,保留5位小数. 输入包含多组数据,结束标志为 m=n=0. 有错欢迎指出^_^ */ #include<stdio.h> int main() { int m,n,i,j=1; while(scanf("%d%d",&m,&n) != EOF) { double sum

算法竞赛入门经典_4.3_递归

看代码 #include <stdio.h> int f(int n){ return n == 0?1:f(n-1)*n; } int main() { printf("%d\n", f(5)); return 0; } 上面f函数使用了递归,递归由两部分组成,一是递归头,二是递归体. 我们使用gcc调试工具 H:\编程书籍学习\算法竞赛入门经典2代码\算法入门经典第四章>b f 'b' 不是内部或外部命令,也不是可运行的程序 或批处理文件. H:\编程书籍学习\算

《算法竞赛入门经典》动态规划复习

codevs 4979 数塔 1 #define N 100 2 #include<iostream> 3 using namespace std; 4 #include<cstdio> 5 int a[N][N],b[N][N],n; 6 int main() 7 { 8 scanf("%d",&n); 9 for(int i=1;i<=n;++i) 10 for(int j=1;j<=i;++j) 11 { 12 scanf("