HDU 5360——Hiking——————【贪心+优先队列】
Hiking
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 118 Accepted Submission(s): 69
Special Judge
1. he selects a soda not invited before;
2. he tells soda the number of soda who agree to go hiking by now;
3. soda will agree or disagree according to the number he hears.
Note: beta will always tell the truth and soda will agree if and only if the number he hears is no less than li and no larger than ri, otherwise he will disagree. Once soda agrees to go hiking he will not regret even if the final total number fails to meet some soda's will.
Help beta design an invitation order that the number of soda who agree to go hiking is maximum.
The first contains an integer n (1≤n≤105), the number of soda. The second line constains n integers l1,l2,…,ln. The third line constains n integers r1,r2,…,rn. (0≤li≤ri≤n)
It is guaranteed that the total number of soda in the input doesn't exceed 1000000. The number of test cases in the input doesn't exceed 600.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+20;
const int INF=0x3f3f3f3f;
struct Soda{
int l,r,len,ord;
bool operator < (const Soda &b)const {
return r>b.r;
}
}sodas[maxn];
priority_queue<Soda>PQ;
int ans[maxn],vis[maxn],b[maxn]; //b数组是没有去的soda编号,ans数组是去了
bool cmp(Soda a,Soda b){
if(a.l!=b.l){
return a.l<b.l;
}else{
return a.len<b.len;
}
}
int main(){
int t,n;
scanf("%d",&t);
while(t--){
memset(vis,0,sizeof(vis));
scanf("%d",&n);
int flag=0;
for(int i=0;i<n;i++){
scanf("%d",&sodas[i].l);
if(sodas[i].l==0){
flag=1;
}
}
for(int i=0;i<n;i++){
scanf("%d",&sodas[i].r);
sodas[i].ord=i+1;
sodas[i].len=sodas[i].r-sodas[i].l+1;
}
if(!flag){
printf("0\n",n);
printf("1");
for(int i=2;i<=n;i++){
printf(" %d",i);
}printf("\n");
continue;
}
sort(sodas,sodas+n,cmp);
int sum=0,kk=0,kt=0,nn=0;
int fg=0;Soda tmp;
for(int i=0;i<n;i++){
if(!fg){
if(sodas[i].l<=kt){
PQ.push(sodas[i]);
}else{
i--;
int mark=0;
while(!PQ.empty()){
tmp=PQ.top();
PQ.pop();
if(tmp.r>=kt){
mark=1;
kt++;
ans[kk++]=tmp.ord;
break;
}else{
b[nn++]=tmp.ord;
}
}
if(mark==0&&PQ.empty())
fg=1;
}
}else{
b[nn++]=sodas[i].ord;
}
}
while(!PQ.empty()){
tmp=PQ.top();
PQ.pop();
if(tmp.r>=kt){
kt++;
ans[kk++]=tmp.ord;
}else{
b[nn++]=tmp.ord;
}
}
printf("%d\n",kt);
printf("%d",ans[0]);
ans[0]=0;
for(int i=1;i<kk;i++){
printf(" %d",ans[i]);
ans[i]=0;
}
for(int i=0;i<nn;i++){
printf(" %d",b[i]);
b[i]=0;
}printf("\n");
}
return 0;
}
HDU 5360——Hiking——————【贪心+优先队列】的更多相关文章
- 2015多校第6场 HDU 5360 Hiking 贪心,优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须 ...
- HDU 5360 Hiking (贪心)
题意:邀请 n 参加聚会,如果在邀请第 i 个人之前,已经成功邀请了 x 个人,并且 li <= x <= ri,那么第 i 人才会去,问你怎么排列使得邀请的人最多. 析:对于所有的人,按 ...
- HDU 5360 Hiking(优先队列)
Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...
- hdu 5360 Hiking(优先队列+贪心)
题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L, ...
- HDU 5360 Hiking 登山 (优先队列,排序)
题意: 有n个人可供邀请去hiking,但是他们很有个性,每个人都有个预期的人数上下限[Li,Ri],只有当前确定会去的人数在这个区间内他才肯去.一旦他答应了,无论人数怎样变更,他都不会反悔.问最多能 ...
- HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6
Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...
- 2015 Multi-University Training Contest 6 hdu 5360 Hiking
Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- HDU 5360 (贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:告诉你n个区间[ l[i],r[i] ],然后让你排序,必须左区间不大于它前边的总区间个数 ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
随机推荐
- Delphi和C#数据类型对应表
Delphi DataType C# datatype ansistring string boolean bool byte byte char char comp double currency ...
- 基于Ace Admin 的菜单栏实现
1.首先是数据库表必然包含以下几个字段Id ,ParnetId,Url,Name等 create table dbo.Module ( Id uniqueidentifier not null con ...
- Login failed for user 'xxx'
今天零晨,正准备晚睡,有一网友在skype求救,意思是说,恢复专案的数据库之后,死活登录不了.出现的错误如下: Insus.NET叫他参考下面方法来做:http://www.cnblogs.com/i ...
- access + vb + asp 遇到一些问题的总结
Data Base access + vb + asp 遇到一些问题的总结 1.asp中sql语句: select * from users whre name=’张三‘ and addTime=# ...
- FPM包定制完成 (等待实现 里程碑 1 和 2) 2018年4月13日 2:18:32
前期环境准备: 关闭SELINUX : setenforce 0 关闭SELINUX : sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' / ...
- (原创)E - Straight Shot Gym - 101652R
解题思路:这道题的题意就是给你n,总距离X,速度v:以及n组数据:人行道的左端点和右端点,以及人行道的速度(竖直方向),如果从(0,0)到(X,0)的时间小于2X/v,则输出其时间,否则输出”Too ...
- 转载 NoSQL | Redis、Memcache、MongoDB特点、区别以及应用场景
NoSQL | Redis.Memcache.MongoDB特点.区别以及应用场景 2017-12-12 康哥 码神联盟 本篇文章主要介绍Nosql的一些东西,以及Nosql中比较火的三个数据库Red ...
- 区块链中的密码学(二)-RSA算法分析和实现
密码学领域中,加密算法主要分为对称加密和非对称加密,随着信息时代安全性要求越来越高,对称加密因为其易被破解的原因逐渐被舍弃.而RSA算法是目前密码学世界中比较流行的非对称加密算法,命名是根据其发明者R ...
- 记一次IIS应用程序域崩溃的原因
在日常工作中,每次新的功能上线前,我们会搭建一个测试环境提供给客户测试使用,确定无误后才会更新到正式环境上.这一次也不例外,在约定好时间地点,客户进行集中化测试的过程中,反应网站系统打不开,报500错 ...
- 树状数组【bzoj1782】: [Usaco2010 Feb]slowdown 慢慢游
[bzoj1782]: [Usaco2010 Feb]slowdown 慢慢游 Description 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N) ...