poj 2051 Argus(数据结构:优先权队列)
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.
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:
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.
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).
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
Source
题意理解:
(1)每个注册用户均有一个注册ID和一个时间间隔
(2)针对每隔用户,每隔一个自己的时间间隔该ID打印一次
(3)#说明输入到结尾处,没有用户注册了
(4)最后一行的数字为打印的次数
方法:这里使用了一个优先权队列,把所有的注册用户放入该队列中,队列的排序按照要出现的时间从小到大排序,如果时间有冲突就按照id升序。每次这个用户id输出后,先把该用户从队列里pop出来,然后把该用户的出现时间加上他的时间间隔,然后再push到优先权队列中,由于这个队列是按照要求排序的,所以每次在队头的那个元素就是要打印的ID
#include <iostream> #include <queue> 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<Register,vector<Register>,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<<reg.id<<endl; reg.time += reg.period; re.pop(); re.push(reg); } return 0; }
posted on 2012-09-10 15:53 NewPanderKing 阅读(1136) 评论(0) 编辑 收藏 举报