LA 3135 (优先队列)

来源:互联网 发布:天盾数据恢复官网下载 编辑:程序博客网 时间:2024/05/27 20:09
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 P eriod
Q num (0 < Qnum ≤ 3000) is query ID-number, and P eriod (0 < P eriod ≤ 3000) is the interval
between two consecutive returns of the result. After P eriod seconds of register, the result will be
returned for the first time, and after that, the result will be returned every P eriod 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



题意:有一个系统,它每p秒就会产生一个Q的事件,这个系统是优先处理Q值小的,请输出处理顺序



题解:开始对于每p秒就会产生一个Q的事件这个问题不知道怎么处理,想着每次都遍历一遍事件,然后定义一个变量计时之类的。。。。。。,看了书上的才发现根本没有这么复杂,只要加上自己的生成周期重新压回优先队列,定义队列p小的先出对,否则Q小的先出队就可以了,然后直接使用优先队列就可了


这里定义的优先队列是

priority_queue<int>pq;

这是默认数值大的优先出队



AC代码:


#include<iostream>#include<cstdio>#include<vector>#include<string>#include<algorithm>#include<queue>using namespace std;#define N 100005struct point{int qm,pr;int orgin;point(int _O,int _Q,int _P):orgin(_O),qm(_Q),pr(_P){}point(){}bool operator <(const point &x1)const{return(this->qm>x1.qm||this->qm==x1.qm&&this->pr>x1.pr); }};priority_queue<point>pq;int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifstring s;int q,p;while(cin>>s&&s!="#"){scanf("%d%d",&p,&q);pq.push(point(q,q,p));}int k;scanf("%d",&k);while(k--){point tmp=pq.top();printf("%d\n",tmp.pr);pq.pop();tmp.qm+=tmp.orgin;pq.push(tmp);}return 0;}




0 0
原创粉丝点击