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就能过了 ...
随机推荐
- Data Base 常用数据库参数的前缀表示符合
可能参数化SQL语句不同,例如在Access中参数化SQL语句是在参数直接以“?”作为参数名,在SQL Server中是参数有“@”前缀,在MySQL中是参数有“?”前缀,在Oracle中参数以“:” ...
- Object Detection: Face Detection using Haar Cascades
目录 利用基于Haar特征的级联分类器实现人脸检测:官方教程 目标 学习基于Haar特征的级联分类器(Cascade Callifiers)实现人脸检测: 扩展到人眼检测: 基础知识 Paul V ...
- 【机器学习】李宏毅机器学习-Keras-Demo-神经网络手写数字识别与调参
参考: 原视频:李宏毅机器学习-Keras-Demo 调参博文1:深度学习入门实践_十行搭建手写数字识别神经网络 调参博文2:手写数字识别---demo(有小错误) 代码链接: 编程环境: 操作系统: ...
- leftjoin及多个leftjoin执行顺序
给个通俗的解释吧.例表aaid adate1 a12 a23 a3表bbid bdate1 b12 b24 b4两个表a,b相连接,要取出id相同的字段select * from a inner jo ...
- Django个人博客开发 | 前言
本渣渣不专注技术,只专注使用技术,不是一个资深的coder,是一个不折不扣的copier 1.前言 自学 Python,始于 Django 框架,Scrapy 框架,elasticsearch搜索引擎 ...
- 浅谈python web框架django2.x
1.Django简介 Python下有多款不同的 Web 框架,Django是最有代表性的一种.许多成功的网站和APP都基于Django. Django是一个开源的Web应用框架,由Python写成. ...
- matplotlib类
1.plt.subplot 在matplotlib下,一个Figure对象可以包含多个子图(Axes),可以使用subplot()快速绘制,其调用形式如下:subplot(numRows, numCo ...
- 洛谷 P2982 [USACO10FEB]慢下来Slowing down
题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N mov ...
- java.sql.Date/ java.util.Date/ java.util.Calendar 用法与区别
在 JDK API 这样解释," 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符串.不过,这些函数的 ...
- flask-SocketIO
本文来源:https://www.jianshu.com/p/d81397edd2b1 websocket是html5中实现了服务端和客户端进行双向文本或二进制数据通信的一种新协议,其实已经低于HTT ...