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就能过了 ...
随机推荐
- JDK Linux下安装
下载jdk-6u45-linux-x64.bin到/root目录下执行./jdk-6u45-linux-x64.bin 会在/root目录下生成 jdk1.6.0_45 文件 之后配置环境变量 编辑/ ...
- json互转笔记
JSON.parse()与JSON.stringify()的区别 JSON.parse()[从一个字符串中解析出json对象] 例子: //定义一个字符串 var data='{"name& ...
- BZOJ1096 [ZJOI2007]仓库建设(斜率优化)
题目背景 小B的班级数学学到多项式乘法了,于是小B给大家出了个问题:用编程序来解决多项式乘法的问题. 题目描述 L公司有N个工厂,由高到底分布在一座山上. 工厂1在山顶,工厂N在山脚. 由于这座山处于 ...
- luogu2658 GCD(莫比乌斯反演/欧拉函数)
link 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 1<=N<=10^7 (1)莫比乌斯反演法 发现就是YY的GCD,左转YY的GCD ...
- 2019年GPLT L2-4 彩虹瓶 比赛题解 中国高校计算机大赛-团体程序设计天梯赛题解
彩虹瓶的制作过程(并不)是这样的:先把一大批空瓶铺放在装填场地上,然后按照一定的顺序将每种颜色的小球均匀撒到这批瓶子里. 假设彩虹瓶里要按顺序装 N 种颜色的小球(不妨将顺序就编号为 1 到 N).现 ...
- linux文件系统总结
apue中:其中进程表项内部的数组又称为 进程打开文件表 另外一个角度: 从linux内核角度开: task_struct是进程描述符对应上面的进程表项,在task_struct描述符中有str ...
- socket中close发生的事情,RST,pipe信号错误
1.server端close之后,client端write,导致server端发送RST(服务器关闭套接字):对方已经关闭或者异常终止,但是client端,不知道,这个成为半打开 当server端cl ...
- [POI2014]KUR-Couriers BZOJ3524 主席树
给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. Input 第一行两 ...
- HDU6312 Game (多校第二场1004) 简单博弈
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Luogu5324 BJOI2019删数(线段树)
考虑无修改怎么做.对于1~n的每个数,若其存在,将最后一个放在其值的位置,剩余在其前面依次排列,答案即为值域1~n上没有数的位置个数.带修改显然记一下偏移量线段树改一改就好了. #include< ...