[HDOJ5360]Hiking
2015-09-04 16:44
417 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360
优先队列+贪心。(感觉这个贪心在《挑战程序设计竞赛》一书中有个类似的,有兴趣的可以找一下。)
写优先队列优先级判断的时候把maxx写成minn也是醉了,调了2个小时才发现。看来应该再仔细一些啊。
初始化将下限小于当前人数的人全部压入队列中,再判断要求上限和当前人数是否符合,如果符合,则当前去的人数+1,此人出队。同时记下顺序。接着将下限符合条件的压入队列。
优先队列+贪心。(感觉这个贪心在《挑战程序设计竞赛》一书中有个类似的,有兴趣的可以找一下。)
写优先队列优先级判断的时候把maxx写成minn也是醉了,调了2个小时才发现。看来应该再仔细一些啊。
初始化将下限小于当前人数的人全部压入队列中,再判断要求上限和当前人数是否符合,如果符合,则当前去的人数+1,此人出队。同时记下顺序。接着将下限符合条件的压入队列。
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <cctype> #include <queue> #include <map> #include <set> #include <stack> #include <list> #include <vector> using namespace std; typedef struct Node { int maxx; int minn; int id; friend bool operator <(Node a, Node b) { return a.maxx > b.maxx; } }Node; bool cmp(Node a, Node b) { return a.minn < b.minn; } const int maxn = 1000010; Node peo[maxn]; priority_queue<Node> pq; int ord[maxn]; int n; int main() { // freopen("in", "r", stdin); int T; scanf("%d", &T); while(T--) { int cnt = 0; while(!pq.empty()) { pq.pop(); } scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &peo[i].minn); peo[i].id = i; } for(int i = 1; i <= n; i++) { scanf("%d", &peo[i].maxx); } sort(peo+1, peo+n+1, cmp); int cur = 0; //当前有多少人去 int k = 1; while(peo[k].minn <= cur) { //init pq.push(peo[k++]); } while(!pq.empty()) { if(pq.top().maxx >= cur) { cur++; } ord[cnt++] = pq.top().id; pq.pop(); while(peo[k].minn <= cur && k <= n) { pq.push(peo[k++]); } } printf("%d\n", cur); for(int i = 0; i < cnt; i++) { printf("%d ", ord[i]); } for(int i = k; i <= n; i++) { printf("%d ", peo[i].id); } printf("\n"); } return 0; }
相关文章推荐
- Struts2 part6:数据校验
- Linux之grub的运行机制及grub修复
- C++基础---无返回值函数(void函数)
- 家里接双线方法,带宽和网速都提高
- 网线接错,也可以上网,老断线
- 我是懒人
- usaco Longest Prefix
- Wget下载终极用法和15个详细的例子
- Largest Number —— Leetcode(sort的妙用)
- [网络流24题] 21 最长k可重区间集(最大权不相交路径 ,最小费用最大流)
- 28. 原型
- VMware CentOS Device eth0 does not seem to be present
- io心得
- 内部排序方法比较
- sql注入漏洞检测方法
- 忆昔悟今
- Java线程池简单了解
- QT跨目录编译及多工程集成
- 移动光纤使用了美国的ip
- scala (3)传参