博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2051 Argus(数据结构:优先权队列)
阅读量:6928 次
发布时间:2019-06-27

本文共 3420 字,大约阅读时间需要 11 分钟。

Argus
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8312   Accepted: 3701

Description

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 200Register 2005 300#5

Sample Output

20042005200420042005

Source

题意理解:

(1)每个注册用户均有一个注册ID和一个时间间隔

(2)针对每隔用户,每隔一个自己的时间间隔该ID打印一次

(3)#说明输入到结尾处,没有用户注册了

(4)最后一行的数字为打印的次数

方法:这里使用了一个优先权队列,把所有的注册用户放入该队列中,队列的排序按照要出现的时间从小到大排序,如果时间有冲突就按照id升序。每次这个用户id输出后,先把该用户从队列里pop出来,然后把该用户的出现时间加上他的时间间隔,然后再push到优先权队列中,由于这个队列是按照要求排序的,所以每次在队头的那个元素就是要打印的ID

#include 
#include
using namespace std;//定义一个注册用户信息typedef struct{ int id;//用户ID int time;//下一次出该出现的时间 int period;//时间间隔}Register;//定义优先权队列的比较函数struct cmp{ bool operator()(Register a,Register b) { if(a.time==b.time) return a.id>b.id;//时间相等时按照id升序排列 return a.time>b.time;//按照时间升序排列,成为最小堆 }};int main(){ //定义优先权队列,到达时间最小的最先到,相等时按照id升序排列 priority_queue
,cmp > re; string cmd;//命令 int id;//定义id int period;//定义时间间隔 int count;//定义输出条数 while(true) { cin>>cmd; if(cmd.compare("#")==0)break; cin>>id>>period; Register r; r.id = id; r.period = period; r.time = period; re.push(r); } cin>>count; while(count--) { Register reg = re.top(); cout<
<

转载地址:http://mtpjl.baihongyu.com/

你可能感兴趣的文章
MySQL 压缩包安装方法
查看>>
人机对话这件事为什么难?| 清华x-lab人工智能研习社
查看>>
PSR-1
查看>>
android 出错集
查看>>
POSIX线程同步
查看>>
解读2017企业网防火墙魔力象限,这四家变化最吸引眼球!
查看>>
如何让人工智能更加实际的辅助网络安全
查看>>
神马搜索变身购票神器 8.8元看《美国队长3》
查看>>
利用lk-reducer进行Linux内核审计监控文件访问
查看>>
《工业控制网络安全技术与实践》一3.1.2 过程控制与监控网络
查看>>
最全Linux的发行版简介,一文读懂各发行版之间的联系和区别
查看>>
如何用Python检测伪造的视频
查看>>
快速上手Kotlin的11招
查看>>
《深入理解C++11:C++ 11新特性解析与应用》——2.9 扩展的friend语法
查看>>
《信息可视化:交互设计(原书第2版)》——2.8节多属性
查看>>
为何硅谷第一性感女人也没能拯救雅虎?
查看>>
阿里巴巴启动“NASA”计划 谋划20年后的发展格局
查看>>
安全专家:勒索病毒只能预防 被感染还无法破解
查看>>
2016半年盘点:最酷的10家云初创公司
查看>>
探讨MES在质量管理中的应用
查看>>