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
//才开始没有想到这里 然后借用了其他人的博客 然后很容易就明白了;
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
struct Test{
int qnum,period,time;
//下面的运算符的重载是很关键的一步
bool operator < (const Test &a)const//这里的const是不可以缺少的
{
return time>a.time||(time==a.time&&qnum>a.qnum);//考虑二者周期时间相等的时候的情况 然后对qnum 进行比较输出
}
};
int main()
{
priority_queue<Test> pq;//优先级队列;
char s[20];
Test item;
while(scanf("%s",s)&&s[0]!='#'){
scanf("%d%d",&item.qnum,&item.period);
item.time = item.period; //借用其他的变量进行周期定位
pq.push(item);//将item入队
}
int k;
scanf("%d",&k);
while(k--){
Test it=pq.top();
pq.pop();
printf("%d\n",it.qnum);//出队列;
it.time+=it.period;//运用这个周期循环的方法 进队列;
pq.push(it);//把it入队
}
return 0;
}