(算法入门经典大赛 优先级队列)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 ...
随机推荐
- vs2008编译QT开源项目三国杀(五篇文章)
请参看 http://tieba.baidu.com/f?kz=1508964881 按照上面的网址教程,下载三国杀源码,swig工具,并下载最新的QT4.8.2 for vs2008.我本机已经安装 ...
- 14.5.4 InnoDB File-Per-Table Tablespaces 每个表一个文件
14.5.4 InnoDB File-Per-Table Tablespaces 每个表一个文件 从历史上看, 所有的InnoDB 表和索引是存储在system 表空间, 这个整体的方法是针对机器专注 ...
- PropertyPlaceholderConfigurer类的使用注意
如果你在spring的applicationcontext.xml中需要使用属性配置文件,那PropertyPlaceholderConfigurer这个类就是必须的. <bean class= ...
- Mqtt协议IOS移植完1
MQTTClient.h #import <Foundation/Foundation.h> @protocol MQTTDelegate <NSObject> /** * @ ...
- Qt窗口操作函数(最大化,全屏,隐藏最大化,最小化)
Qt窗口中的一些小技术总结 //Qt主窗口没有最小化,最大化按钮且最大化显示 int main(int argc, char *argv[]) { QApplication a(argc, argv ...
- 物联网操作系统HelloX开发人员入门指南
HelloX开发人员入门指南 HelloX是聚焦于物联网领域的操作系统开发项目,能够通过百度搜索"HelloX".获取具体信息. 当前开发团队正在进一步招募中,欢迎您的了解和添加. ...
- Tutorial: 结合使用AngularJS和Django
好吧,我承认自己很懒,时间又不够用. 翻译的几个文章都是虎头蛇尾,但我保证这次肯定不太监. 关键的单词不翻译,实在觉得翻译成汉语很别扭,括号里是参考翻译. 有问题和建议尽管提出来,我会改进完善. Tu ...
- Conexant声卡实现内录功能(win7)
Conexant声卡本身没有立体声混音设备可选,所以我们采用virtual audio device,实现内录功能. [1]下载virtual audio device.下载地址:http://dow ...
- Android OpenGL ES 应用(二) 纹理
上一篇讲了基础入门 OpenGL (一) ,这一次主要学习OpenGL 纹理基本学习总结 要是做复杂的OpenGL应用程序,一定会用到纹理技术.纹理说白了就是把图片或者视频图像绘制到OpenGL空间中 ...
- fastdfs storage server的设计与实现
fastdfs是一个针对互联网应用设计的分布式文件系统.具有架构简单.结构清晰.代码量小等特点. 详细的介绍及架构请參考分布式文件系统FastDFS架构剖析(http://www.program ...