(算法入门经典大赛 优先级队列)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
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说明)的更多相关文章
- 算法入门经典大赛 Dynamic Programming
111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence ...
- 【python cookbook】【数据结构与算法】5.实现优先级队列
问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Exam ...
- 《Java数据结构与算法》笔记-CH4-6优先级队列
/** * 优先级队列 * 效率:插入O(n),删除O(1).第12章介绍如何通过堆来改进insert时间 */ class PriorityQueue { private int maxSize; ...
- 算法入门经典-第七章 例题7-4-1 拓展 n皇后问题 回溯法
实际上回溯法有暴力破解的意思在里面,解决一个问题,一路走到底,路无法通,返回寻找另 一条路. 回溯法可以解决很多的问题,如:N皇后问题和迷宫问题. 一.概念 回溯算法实际类似枚举的搜索尝试过程,主 ...
- 算法入门经典第七章 例题7-2-1 生成1-n的排列
输入正数n,按字典序从小到大的顺序输出n个数的所有排列.两个序列的字典序大小关系等价于从头开始第一个不相同位置处的大小关系. 递归的边界应该很好理解吧,当集合s[]中没有一个元素的时候,按照上面的伪码 ...
- 算法入门经典-第六章 例题6-21 SystemDependencies
题意:软件组件之间会有依赖关系,比如你下一个Codeblocks你也得顺带着把编译器给下上.你的任务是模拟安装和卸载软件组件的过程.有以下五种指令,如果指令为“END”则退出程序:若为以下四种指令,则 ...
- 算法入门经典第六章 例题6-14 Abbott的复仇(Abbott'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 SF ...
- [PY3]——实现一个优先级队列
import heapq class PriorityQueue: def __init__(self): self._queue=[] self._index=0 def push(self,ite ...
- 算法竞赛入门经典 LA 4329(树状数组)
题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...
随机推荐
- ASP.NET常被忽视的一些细节
原文:ASP.NET常被忽视的一些细节 前段时间碰到一个问题:为什么在ASP.NET程序中定时器有时候会不工作? 这个问题看起来很奇怪,代码好像也没错,但就是结果与预期不一致. 其实这里是ASP.NE ...
- ThinkPHP页面跳转、Ajax技巧详细介绍(十八)
原文:ThinkPHP页面跳转.Ajax技巧详细介绍(十八) ThinkPHP页面跳转.Ajax技巧详细介绍 一.页面跳转 $this->success('查询成功',U('User/test' ...
- Lambda高手之路第一部分
转http://www.cnblogs.com/lazycoding/archive/2013/01/06/2847574.html 介绍 Lambda表达式是使代码更加动态,易于扩展并且更加快速(看 ...
- 安卓MP3播放器开发实例(1)之音乐列表界面
学习安卓开发有一年了,想想这一年的努力,确实也收获了不少.也找到了比較如意的工作. 今天准备分享一个以前在初学阶段练习的一个项目.通过这个项目我真正的找到了开发安卓软件的感觉,从此逐渐步入安卓开发的正 ...
- 介绍一个C++奇巧淫技
你能实现这样一个函数吗: MyType type; HisType htype; serialize_3(11, type, htype); serialize_4(type, hty ...
- JAVA进阶----ThreadPoolExecutor机制(转)
ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...
- 肯德基champs各个字母代表什么_百度知道
肯德基champs各个字母代表什么_百度知道 肯德基champs各个字母代表什么
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
- 红黑树-Python实现
#coding:utf8 #author:HaxtraZ #description:红黑树,python实现 RED = 'red' BLACK = 'black' class RBT: def __ ...
- POJ 1742 Coins ( 单调队列解法 )
id=1742" target="_blank">题目链接~~> 做题感悟:第一次做的时候用的二进制优化.可是没注意到是险过.so也没去看单调队列的解法. 解 ...