任务系统
蒜头君设计了一个任务系统。这个系统是为了定时提醒蒜头君去完成一些事情。
系统大致如下,初始的时候,蒜头君可能会注册很多任务,每一个任务的注册如下:
Register Q_num Period
表示从系统启动开始,每过 Period 秒提醒蒜头君完成编号为 Qnum的任务。
你能计算出蒜头君最先被提醒的 k 个任务吗?
输入描述
第一行输入n(0<n≤3000),k(0<k≤10000),其中 n 表示蒜头君注册的任务数量。
接下来 n 行,每行输入一条注册命令,其中 30000<qnum≤3000,0≤Period≤3000。
输出描述
顺序输出 k 行,表示依次提醒的任务的编号。
如果同一时间有多个任务,最先提醒编号小的任务。
用例输入 1
2 5 Register 2004 200 Register 2005 300
用例输出 1
2004 2005 2004 2004 2005
用结构体储存一个任务的编号和每次执行的时间
用map储存每个任务的执行间隔
优先队列储存结构体(即任务),所以需要对结构体重载运算符
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return x*f;
}
struct node{
int time,num;
bool operator<(const node&a)const{ //优先队列默认大顶堆,将结构体'<'重载为'>'
if(time==a.time)return num>a.num;
return time>a.time; //先按当前时间比较,然后按num比较
}
};
map<int,int>m; //储存num和提醒间隔的对应关系
int main(){
int n=read(),k=read();
priority_queue<node>q;
while(n--){
string s;
int a,b;
cin>>s>>a>>b;
node t;
t.num=a;
t.time=b;
m[t.num]=b; //设置每个num对应的间隔
q.push(t);
//q.push({b,b,a});
}
while(k--){
node a=q.top();
q.pop();
cout<<a.num<<endl; //每次取出最早提醒的任务
a.time+=m[a.num]; //设置该任务下次提醒时间,放入队列
q.push(a);
}
return 0;
}