Description
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."
Register Q_num Period
Input
Output
Sample Input
Register 2004 200 Register 2005 300 # 5
Sample Output
2004 2005 2004 2004 2005
Source
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<string> 5 6 using namespace std; 7 8 struct node 9 { 10 friend bool operator<(node n1,node n2) 11 { 12 if(n1.time==n2.time) return n1.ID>n2.ID; //如果时间相同则按,ID号从小到大 13 return n1.time>n2.time; //优先级按秒数从小到大 14 } 15 int ID; 16 int Period; 17 int time; 18 }; 19 20 21 int main() 22 { 23 char s[20]; 24 int ID,Period,T; 25 int i; 26 node temp; 27 priority_queue<node> qn; //声明一个优先队列 28 while(scanf("%s",s)) 29 { 30 if(s[0]==‘#‘) break; 31 scanf("%d%d",&ID,&Period); 32 temp.ID=ID; 33 temp.Period=Period; 34 temp.time=Period; 35 qn.push(temp); //入队列 36 } 37 scanf("%d",&T); 38 while(T--) 39 { 40 temp=qn.top(); //把队列头部的数据赋值给temp 41 qn.pop(); //出队列 42 temp.time+=temp.Period; //计算下次出现此ID号的秒数 43 qn.push(temp); //把得到的新数据加入队列 44 printf("%d\n",temp.ID); 45 } 46 return 0; 47 }
原文:http://www.cnblogs.com/gantaiyuan/p/3515133.html