(算法入门经典大赛 优先级队列)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;
}
时间: 2024-08-02 20:23:15

(算法入门经典大赛 优先级队列)LA 3135(之前K说明)的相关文章

算法入门经典大赛 Dynamic Programming

111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence LCS 674 - Coin Change 全然背包求方案数 10003  - Cutting Sticks 区间DP dp[l][r]代表分割l到r的最小费用 116 - Unidirectional TSP 简单递推 输出字典序最小解 从后往前推 10131 - Is Bigger Smarte

(算法竞赛入门经典 优先队列)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 o

浅谈算法和数据结构: 五 优先级队列与堆排序

转载自:http://www.cnblogs.com/yangecnu/p/Introduce-Priority-Queue-And-Heap-Sort.html 浅谈算法和数据结构: 五 优先级队列与堆排序 在很多应用中,我们通常需要按照优先级情况对待处理对象进行处理,比如首先处理优先级最高的对象,然后处理次高的对象.最简单的一个例子就是,在手机上玩游戏的时候,如果有来电,那么系统应该优先处理打进来的电话. 在这种情况下,我们的数据结构应该提供两个最基本的操作,一个是返回最高优先级对象,一个是

算法入门经典第六章 例题6-14 Abbott的复仇(Abbott&#39;s Revenge)BFS算法实现

Sample Input 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ELF * 2 3 SFR EL * 0 Sample Output (3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (1,3) (1,2) (1,1) (2,1) (2,2) (1,2) (1,3) (2,3) (3,3) 析 题目的大意是,输入起点,离开起点时的朝向和终点,求一条最短路. 每一个

算法入门经典-第五章 例题5-6 团体队列

题目背景 队列和优先级队列是大多数计算机科学家都知道的数据结构.但是团队队列却不被人熟知,尽管在生活中经常出现.比如,午餐时间的食堂门口的队列就是一个团队队列.在一个团队队列中,每个元素属于一个团队.如果一个元素进入一个队列,它首先从头到尾地搜寻这个队列--检查是否它的队友(在同一个团队称之为队友)也在这个队列里.如果有,它就排在它队友的后面(:-D就是插队咯~~).如果没有,它就排在整个队列的最后,成为新的最后一名(/(ㄒoㄒ)/~真是不幸).在普通队列中出队是这样的:元素从头到尾的被处理,按

【python cookbook】【数据结构与算法】5.实现优先级队列

问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Example of a priority queue import heapq class PriorityQueue: def __init__(self): self._queue = [] self._index = 0 def push(self, item, priority): heapq.h

算法入门经典-第七章 例题7-4-1 拓展 n皇后问题 回溯法

实际上回溯法有暴力破解的意思在里面,解决一个问题,一路走到底,路无法通,返回寻找另   一条路. 回溯法可以解决很多的问题,如:N皇后问题和迷宫问题. 一.概念 回溯算法实际类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现不满足条件的时候,就回溯返回,尝试别的路径. 百度解释:回溯法(探索与回溯法)是一种选优搜索法,又称为试探法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯

算法入门经典-第七章 例题7-2 八皇后问题

原本利用回溯思想解决的经典八皇后问题,其实也是可以用递归解决的~ 八皇后的递归解决思路: 从第一行开始,依次判断0~8列的哪一列可以放置Queen,这样就确定了该行的Queen的位置,然后行数递增,继而递归实现下一行的判断,依次类推直到行数增加到8(行数从0开始的),此时为递归-----归的条件,即表示一种八皇后的解决方法完成,打印结果:之后进行下一种解决方法的寻找,大致思路个人理解是这样 noDanger(row,j,(*chess)[8])函数是判断第row行第j列是否可以放置Queen #

第六章 二叉树【算法入门经典】【结构体指针】

运行效果图 结构体指针实现 #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 10000 int failed,n,v,ans[N]; char s[N];//保存读入结点 typedef struct Node//结点类型 { int flag;//是否被赋值过 int number;//结点值 struct Node *left,*right;//左右子结点 }Node; Node *