贪心+优先队列 HDOJ 5360 Hiking
/*
题意:求邀请顺序使得去爬山的人最多,每个人有去的条件
贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀请,而且不用考虑最少人数的条件。网上的写的比我简洁,学习了。
详细解释:http://blog.csdn.net/queuelovestack/article/details/47319361
*/
/************************************************
* Author :Running_Time
* Created Time :2015-8-6 14:51:47
* File Name :H.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
struct Node {
int l, r, id;
bool operator < (const Node &x) const {
return r > x.r;
}
}node[MAXN];
priority_queue<Node> Q;
bool vis[MAXN];
int ans[MAXN];
int n; bool cmp(Node x, Node y) {
if (x.l == y.l) return x.r < y.r;
return x.l < y.l;
} int main(void) { //HDOJ 5360 Hiking
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
for (int i=; i<=n; ++i) {
scanf ("%d", &node[i].l); node[i].id = i;
}
for (int i=; i<=n; ++i) {
scanf ("%d", &node[i].r);
}
sort (node+, node++n, cmp); if (node[].l != ) {
puts ("");
for (int i=; i<=n; ++i) printf ("%d%c", i, (i==n) ? '\n' : ' ');
continue;
}
memset (vis, false, sizeof (vis));
while (!Q.empty ()) Q.pop ();
int i = , p = ;
for (; i<=n && p==node[i].l; ++i) Q.push (node[i]);
while (!Q.empty ()) {
while (!Q.empty ()) {
if (p <= Q.top ().r) {
ans[++p] = Q.top ().id;
vis[ans[p]] = true; Q.pop (); break;
}
else Q.pop ();
}
for (; i<=n && p==node[i].l; ++i) Q.push (node[i]);
} printf ("%d\n", p);
printf ("%d", ans[]);
for (int i=; i<=p; ++i) {
printf (" %d", ans[i]);
}
for (int i=; i<=n; ++i) {
if (!vis[i]) printf (" %d", i);
}
puts ("");
} return ;
}
贪心+优先队列 HDOJ 5360 Hiking的更多相关文章
- HDOJ 5360 Hiking 优先队列+贪心
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意: 大概的意思就是每个人有个人数接受范围$[l_i,r_i]$,现在你每次能从还未被选取的人 ...
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...
- Painting The Fence(贪心+优先队列)
Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...
- CF140C New Year Snowmen(贪心+优先队列)
CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...
- 2015多校第6场 HDU 5360 Hiking 贪心,优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须 ...
随机推荐
- 「CodePlus 2017 12 月赛」火锅盛宴
n<=100000种食物,给每个食物煮熟时间,有q<=500000个操作:在某时刻插入某个食物:查询熟食中编号最小的并删除之:查询是否有编号为id的食物,如果有查询是否有编号为id的熟食, ...
- Linux操作系统下IPTables配置
filter表的防火墙 1.查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n Chain INPUT (policy ACCEPT) target pro ...
- hiho一下 第五十周 (求欧拉路径)
http://hihocoder.com/contest/hiho50/problem/1 这题有重边,所以邻接矩阵用来统计节点u,v之间有多少条边相连,并且用另外一个数组统计每个节点的入度. 然后查 ...
- Our Journey of Dalian Ends 乌鲁木齐网络赛 最小费用最大流
Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpecte ...
- POJ2632 Crashing Robots 解题报告
Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...
- jquery校验框架
http://www.validform.club/ http://craftpip.github.io/jquery-confirm/
- python之SocketServer编程
编写一个SocketServer需要实现以下步骤 编写一个handler类,继承BaseRequestHandler,重写handle()方法 针对是TCP还是UDP,生成一个server对象 调用s ...
- golang 查询数据库操作
SQL.Open only creates the DB object, but dies not open any connections to the database. If you want ...
- Python 出现 can't use a string pattern on a bytes-like object
Python 出现 can't use a string pattern on a bytes-like object 学习了:https://www.cnblogs.com/andrewleeeee ...
- [TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking
TypeScript 2.0 introduced a new primitive type called never, the type of values that never occur. It ...