优先队列(priority_queue)的cmp,POJ(2051)
2024-08-24 14:13:47
sort()函数的cmp为函数,priority_queue的cmp为类,具体写法是:
struct Node {
int i,j;
} node[]; struct cmp {
bool operator() (Node a,Node b) {
if(a.i==b.i)
return a.j<b.j;///j的升序
return a.i<b.i; ///i的升序
}
}; priority_queue<Node,vector<Node>,cmp> re;
#include <iostream>
#include <queue>
using namespace std; typedef struct {
int id;///用户ID
int time;///下一次出该出现的时间
int period;///时间间隔
} Register; ///定义优先权队列的比较函数
struct cmp {
bool operator()(Register a,Register b) {
if(a.time==b.time)
return a.id>b.id;///时间相等时按照id升序排列
return a.time>b.time;///按照时间升序排列,成为最小堆
}
}; int main() {
///定义优先权队列,到达时间最小的最先到,相等时按照id升序排列
priority_queue<Register,vector<Register>,cmp > re;
string cmd;///命令
int id;///定义id
int period;///定义时间间隔
int count;///定义输出条数
while(true) {
cin>>cmd;
if(cmd.compare("#")==)break;
cin>>id>>period;
Register r;
r.id = id;
r.period = period;
r.time = period;
re.push(r);
}
cin>>count;
while(count--) {
Register reg = re.top();
cout<<reg.id<<endl;
reg.time += reg.period;
re.pop();
re.push(reg);
}
return ;
}
最新文章
- nodejs pm2部署配置
- Object.prototype和Function.prototype一些常用方法
- 反汇编工具capstone安装后import error
- mac OS X操作 -- 常用
- UWP/Win10新特性系列—UserConsentVerifier
- APPLICATION ERROR #1502 .
- 二级c程序设计题(2)
- sql数据库监控语句
- Design Pattern——开放封闭原则
- JS 数组及函数
- 在虚拟机中安装Centos系统
- blob对象的应用
- 算法之DP
- Ubuntu install TensorFlow
- 《重构网络-SDN架构与实现》阅读随笔
- PHP中刷新输出缓冲详解[转载]
- 部署一个flask服务记录
- Linux入门——基础系统设置命令
- Android 识别身份证号码(图片识别)
- 【BZOJ2882】工艺 [SAM]