蒜头君设计了一个任务系统。这个系统是为了定时提醒蒜头君去完成一些事情。
系统大致如下,初始的时候,蒜头君可能会注册很多任务,每一个任务的注册如下:
Register Q_num Period
表示从系统启动开始,每过 PeriodPeriod 秒提醒蒜头君完成编号为 Q_{num}Qnum 的任务。
你能计算出蒜头君最先被提醒的 kk 个任务吗?
输入格式
第一行输入 n(0 < n \le 3000)n(0<n≤3000),k(0 < k \le 10000)k(0<k≤10000),其中 nn 表示蒜头君注册的任务数量。
接下来 nn 行,每行输入一条注册命令,其中 0 < q_{num} \le 30000<qnum≤3000,0 \le Period \le 30000≤Period≤3000。
输出格式
顺序输出 kk 行,表示依次提醒的任务的编号。如果同一时间有多个任务,最先提醒编号小的任务。
样例输入
2 5 Register 2004 200 Register 2005 300
样例输出
2004 2005 2004 2004 2005
#include<iostream>
using namespace std;
#include<queue>
#include<bits/stdc++.h>
struct node
{
int num,period,onetime_period,times;
bool operator<(const node&b) const
{
return period>b.period || (period==b.period && num>b.num);
}
};
int main()
{
int n,k;
cin>>n>>k;
int num,onetime_period;
node s[n];
priority_queue<node> q;
string feihua;
for(int i=0;i<n;i++)
{
cin>>feihua;
cin>>num>>onetime_period;
// s[i]=new node(num,onetime_period,onetime_period,1);
s[i].num=num;
s[i].period=onetime_period;
s[i].onetime_period=onetime_period;
s[i].times=1;
q.push(s[i]);
}
node s1;
while(k--)
{
printf("%d\n",q.top().num);
s1=q.top();
s1.times+=1;
s1.period=s1.times*s1.onetime_period;
q.pop();
q.push(s1);
}
return 0;
}
学到的点:
①结构体的声明不用new内存。
②关于优先队列的排序:
一般来说,如果优先队列元素只有int元素,那么是默认less也就是默认从大到小的。那么也就是以下这种写法(注意> >之间空格)
priority_queue<int,vector<int>,less<int> >q; (这就等同于priority_queue<int>q)
如果想要int的优先队列从小到大排序,就是greater:
priority_queue<int,vector<int>,greater<int> >q;
如果元素不是int,比如优先队列的元素是结构体的话,一般需要对该结构体进行<运算符重载(注意形参就一个元素),比如:
- struct number1{
- int x;
- bool operator < (const number1 &a) const {
- return x>a.x;//最小值优先
- }
- };
对于<运算符重载,我的理解是“右边优先”(男左女右+lady first),比如上例,右边的小,所以最小值优先。
当然,也可以在优先队列的声明时加个cmp函数形参:
- struct cmp1{
- bool operator ()(int &a,int &b){
- return a>b;//最小值优先
- }
- };
具体可看看以下代码,viaCSDN博主Sherlock_n
1) 优先队列的定义
包含头文件:"queue.h", "functional.h"
可以使用具有默认优先级的已有数据结构;也可以再定义优先队列的时候传入自定义的优先级比较对象;或者使用自定义对象(数据结构),但是必须重载好< 操作符。
2) 优先队列的常用操作
优先级队列支持的操作 |
q.empty() 如果队列为空,则返回true,否则返回false q.size() 返回队列中元素的个数 q.pop() 删除队首元素,但不返回其值 q.top() 返回具有最高优先级的元素值,但不删除该元素 q.push(item) 在基于优先级的适当位置插入新元素 |
其中q.top()为查找操作,在最小优先队列中搜索优先权最小的元素,在最大优先队列中搜索优先权最大的元素。q.pop()为删除该元素。优先队列插入和删除元素的复杂度都是O(lgn),所以很快
另外,在优先队列中,元素可以具有相同的优先权。
- #include<iostream>
- #include<functional>
- #include<queue>
- #include<vector>
- using namespace std;
- //定义比较结构
- struct cmp1{
- bool operator ()(int &a,int &b){
- return a>b;//最小值优先
- }
- };
- struct cmp2{
- bool operator ()(int &a,int &b){
- return a<b;//最大值优先
- }
- };
- //自定义数据结构
- struct number1{
- int x;
- bool operator < (const number1 &a) const {
- return x>a.x;//最小值优先
- }
- };
- struct number2{
- int x;
- bool operator < (const number2 &a) const {
- return x<a.x;//最大值优先
- }
- };
- int a[]={14,10,56,7,83,22,36,91,3,47,72,0};
- number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};
- number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};
- int main()
- {
- priority_queue<int>que;//采用默认优先级构造队列
- priority_queue<int,vector<int>,cmp1>que1;//最小值优先
- priority_queue<int,vector<int>,cmp2>que2;//最大值优先
- priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
- priority_queue<int,vector<int>,less<int> >que4;最大值优先
- priority_queue<number1>que5; //最小优先级队列
- priority_queue<number2>que6; //最大优先级队列
- int i;
- for(i=0;a[i];i++){
- que.push(a[i]);
- que1.push(a[i]);
- que2.push(a[i]);
- que3.push(a[i]);
- que4.push(a[i]);
- }
- for(i=0;num1[i].x;i++)
- que5.push(num1[i]);
- for(i=0;num2[i].x;i++)
- que6.push(num2[i]);
- printf("采用默认优先关系:/n(priority_queue<int>que;)/n");
- printf("Queue 0:/n");
- while(!que.empty()){
- printf("%3d",que.top());
- que.pop();
- }
- puts("");
- puts("");
- printf("采用结构体自定义优先级方式一:/n(priority_queue<int,vector<int>,cmp>que;)/n");
- printf("Queue 1:/n");
- while(!que1.empty()){
- printf("%3d",que1.top());
- que1.pop();
- }
- puts("");
- printf("Queue 2:/n");
- while(!que2.empty()){
- printf("%3d",que2.top());
- que2.pop();
- }
- puts("");
- puts("");
- printf("采用头文件/"functional/"内定义优先级:/n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)/n");
- printf("Queue 3:/n");
- while(!que3.empty()){
- printf("%3d",que3.top());
- que3.pop();
- }
- puts("");
- printf("Queue 4:/n");
- while(!que4.empty()){
- printf("%3d",que4.top());
- que4.pop();
- }
- puts("");
- puts("");
- printf("采用结构体自定义优先级方式二:/n(priority_queue<number>que)/n");
- printf("Queue 5:/n");
- while(!que5.empty()){
- printf("%3d",que5.top());
- que5.pop();
- }
- puts("");
- printf("Queue 6:/n");
- while(!que6.empty()){
- printf("%3d",que6.top());
- que6.pop();
- }
- puts("");
- return 0;
- }
- /*
- 运行结果 :
- 采用默认优先关系:
- (priority_queue<int>que;)
- Queue 0:
- 83 72 56 47 36 22 14 10 7 3
- 采用结构体自定义优先级方式一:
- (priority_queue<int,vector<int>,cmp>que;)
- Queue 1:
- 7 10 14 22 36 47 56 72 83 91
- Queue 2:
- 83 72 56 47 36 22 14 10 7 3
- 采用头文件"functional"内定义优先级:
- (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
- Queue 3:
- 7 10 14 22 36 47 56 72 83 91
- Queue 4:
- 83 72 56 47 36 22 14 10 7 3
- 采用结构体自定义优先级方式二:
- (priority_queue<number>que)
- Queue 5:
- 7 10 14 22 36 47 56 72 83 91
- Queue 6:
- 83 72 56 47 36 22 14 10 7 3
- */