用优先队列来维护每个触发器的下一个时间,然后每次从中取出最早发生的一个,重复k次即可。时间复杂度为klogn。
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct Item{
int Q_num,period,time;
bool operator < (const Item &A) const{//原始优先队列是大顶堆
return time > A.time || (time == A.time && Q_num > A.Q_num);
}
};
priority_queue <Item> pq;
char s[20];
int main(){
while(scanf("%s",s) && s[0] != '#'){
Item item;
scanf("%d%d",&item.Q_num,&item.period);
item.time = item.period;
pq.push(item);
}
int K;
scanf("%d",&K);
while(K--){
Item now = pq.top();
pq.pop();
printf("%d\n",now.Q_num);
now.time += now.period;
pq.push(now);
}
return 0;
}