hdu 5360 贪心+优先队列
2015-08-06 21:39
417 查看
贪心的思路还是比较好想的,每次选择cur(已经邀请成功的人数)所在的区间中右端点最小的(因为右端点大的在后面可以邀请成功的几率大),然后很自然的想到可以用一个优先队列来维护这些区间,只要每次把左端点小于等于cur的区间放到优先队列中即可。
#include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <queue> using namespace std; const int N = 100001; bool visit ; int a ; struct Node { int r, id; Node(){} Node( int _r, int _id ) { r = _r, id = _id; } bool operator < ( const Node & o ) const { return r > o.r; } }; vector<Node> v ; priority_queue<Node> q; int main () { int t; scanf("%d", &t); while ( t-- ) { int n; scanf("%d", &n); for ( int i = 0; i <= n; i++ ) { v[i].clear(); } for ( int i = 1; i <= n; i++ ) { scanf("%d", a + i); } for ( int i = 1; i <= n; i++ ) { int tmp; scanf("%d", &tmp); v[a[i]].push_back( Node( tmp, i ) ); } memset( visit, 0, sizeof(visit) ); int cur = 0, cnt = 1; while ( 1 ) { for ( int i = 0; i < v[cur].size(); i++ ) { q.push(v[cur][i]); } bool flag = false; while ( !q.empty() ) { Node tt = q.top(); q.pop(); a[cnt++] = tt.id; visit[tt.id] = true; if ( tt.r >= cur ) { cur++; flag = true; break; } } if ( !flag ) { break; } } for ( int i = 1; i <= n; i++ ) { if ( !visit[i] ) { a[cnt++] = i; } } printf("%d\n", cur); for ( int i = 1; i < n; i++ ) { printf("%d ", a[i]); } printf("%d\n", a ); } return 0; }
相关文章推荐
- [GRYZ2014]迷宫问题
- Win7环境下用MyEclipse搭建Hbase开发环境
- HTML学习笔记(1)——常用标签
- poj1328Radar Installation 贪心
- hdu 5360 贪心
- k-means算法和KNN算法的比较
- [CODEVS1116]四色问题
- java--多线程(1)--黑马程序员
- 暑假集训第四周阶段二E - Max Sum最大连续子数组和
- poj 1035 Spell checker
- NOIP2011 Mayan游戏(搜索)
- Validform使用入门(form表单验证)
- Unreal Engine 4 编码规范
- [CODEVS1216]跳马问题
- java--IO流(2)--黑马程序员
- Cocos2d-x移植WP8时间CCScrollView问题
- 从新开始,codeforces 567e
- HDU 5020 Revenge of Collinearity(枚举)
- poj2965The Pilots Brothers' refrigerator DFS+枚举
- Socket编程