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就能过了 ...
随机推荐
- C#使用Newtonsoft.Json操作json
1.下载 Newtonsoft.Json 右键引用,选择 管理NuGet程序包,选择浏览,输入log4net,然后点击下载 2.json数据的组装与解析 public class Class { pu ...
- Kotlin 关系运算符和Boolean
Kotlin的关系运算符和JAVA,c#都是一样. 没啥子区别 都是那几个. > 大于 < 小于 >= 大于等于 <= 小于等于 != 等于 == 等于 上面这些符号,应该是不 ...
- How to publish a pointcloud of ros msgs in a topic from a pcd file?
How to publish a pointcloud of ros msgs in a topic from a pcd file? Two methods 1. modified source 2 ...
- vue 用axios实现调用接口下载excel
了解的方式有两种: 1. 用a标签,href设置为后端提供的excel接口 <a href="excel接口">导出</a> 简单方便,缺点就是当有toke ...
- Q的进阶用法
Q的实例化用法 #q1 里面的条件都是or的关系 q1=Q() q1.connector = 'OR' q1.children.append(('id',1)) q1.children.append( ...
- web安全-接入层注入
web安全-接入层注入 1.关系型数据库 mysql 存放结构化数据 高效操作大量数据 方便处理数据之间的关联关系 2.SQL注入 select * from table where id=${id} ...
- Linux更改文件或目录的所有者和所有组
上节我们说了所有者和所有组的概念, 一个文件它的所有者是谁,属于哪个组的,不同的角色对其的操作权限是不一样的,详细信息请看上节Linux权限管理 这里我们主要说的是怎么去改变这个文件或目录的所有者和所 ...
- POI生成Excel强制换行
自动换行的设置: HSSFCellStyle cellStyle=workbook.createCellStyle(); cellStyle.setWrapText(true); cell.setCe ...
- CF796B Find The Bone
Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, numbered from ...
- springboot整合mybatis,redis,代码(五)
redis注解开发过程中包含许多注解 1.@Cacheable 可以标记在方法上,也可以标记在类上.当标记在方法上时表示该方法是支持缓存的,当标记在类上时则表示该类所有的方法都是支持缓存的.应用到读取 ...