题意:一个系统有N种命令,每个命令有编号和时间,每隔Period秒产生一次,同时又多个任务以编号小优先,求前K次产生的命令是什么。
题解:优先队列存储每个命令,每次取出一个,然后放进去下一个即可。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = 10005;
#define _for(i,a,b) for(int i=a;i<=b;i++)
struct node
{
int Q_num,period,next;
bool operator< (const node& a)const
{
if(next!=a.next)return next>a.next;
else return Q_num>a.Q_num;
}
}a[maxn];
priority_queue<node> Q;
int n,k;
int main(int argc, char const *argv[])
{
char s[20];
while(cin>>s)
{
if(s[0]=='#')break;
else
{
node now;
cin>>now.Q_num>>now.period;
now.next=now.period;
Q.push(now);
}
}
cin>>k;
_for(i,1,k)
{
node now = Q.top();
Q.pop();
cout<<now.Q_num<<endl;
node next = now;
next.next = now.next+now.period;
Q.push(next);
}
return 0;
}