http://poj.org/problem?id=2051
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
经常用堆来实现优先队列,堆培训如下
1 #include"iostream" 2 #include"cstdio" 3 #include"cstring" 4 #include"string" 5 using namespace std; 6 struct abc 7 { 8 int time; 9 int id; 10 int t; 11 } a[3003]; 12 //int len; 13 int k; 14 void down(abc *a,int p,int len) 15 { 16 abc r=a[p]; 17 int i; 18 for(i=p*2;i<=len;i*=2) 19 { 20 if(i<len) 21 { 22 if(a[i].time>a[i+1].time) 23 i++; 24 else if(a[i].time==a[i+1].time&&a[i].id>a[i+1].id) 25 i++; 26 } 27 if(r.time<a[i].time||(r.time==a[i].time&&r.id<a[i].id)) 28 break; 29 a[p]=a[i]; 30 p=i; 31 } 32 a[p]=r; 33 return ; 34 } 35 void build(abc *a,int len) 36 { 37 for(int i=len/2;i>0;i--) 38 down(a,i,len); 39 return ; 40 } 41 int main() 42 { 43 string str; 44 int i=1; 45 cin>>str; 46 while(str.compare("#")!=0) 47 { 48 cin>>a[i].id>>a[i].t; 49 a[i].time=a[i].t; 50 i++; 51 cin>>str; 52 } 53 cin>>k; 54 int len=i-1; 55 build(a,len); 56 for(i=1;i<=k;i++) 57 { 58 cout<<a[1].id<<endl; 59 a[1].time+=a[1].t; 60 down(a,1,len); 61 } 62 return 0; 63 }
用优先队列,STL解很简单,更具竞争力
#include"iostream" #include"queue" #include"string" using namespace std; struct abc { int time; int id; int t; bool operator<(const abc &a) const { if(time==a.time) return id>a.id; else return time>a.time; } } ; int main() { priority_queue<abc> p; abc a; string str; int k; cin>>str; while(str.compare("#")!=false) { cin>>a.id>>a.t; a.time=a.t; p.push(a); cin>>str; } cin>>k; for(int i=1;i<=k;i++) { a=p.top(); a.time+=a.t; cout<<a.id<<endl; p.pop(); p.push(a); } return 0; }