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

Problem Description
There are n soda conveniently labeled by 1,2,…,n. beta, their best friends, wants to invite some soda to go hiking. The i-th soda will go hiking if the total number of soda that go hiking except him is no less than li and no larger than ri. beta will follow the rules below to invite soda one by one:
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.

 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

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.

 
Output
For each test case, output the maximum number of soda. Then in the second line output a permutation of 1,2,…,n denoting the invitation order. If there are multiple solutions, print any of them.
 
Sample Input
4
8
4 1 3 2 2 1 0 3
5 3 6 4 2 1 7 6
8
3 3 2 0 5 0 3 6
4 5 2 7 7 6 7 6
8
2 2 3 3 3 0 0 2
7 4 3 6 3 2 2 5
8
5 6 5 3 3 1 2 4
6 7 7 6 5 4 3 5
 
Sample Output
7
1 7 6 5 2 4 3 8
8
4 6 3 1 2 5 8 7
7
3 6 7 1 5 2 8 4
0
1 2 3 4 5 6 7 8
 
 
题目大意:Bate想邀请sodas出去玩。每个sodai有一个要求,即当前已经邀请到的人数必须在[l,r]之间。问按照什么顺序邀请sodas能使最后邀请到的人最多。
 
解题思路:首先按照l从小到大排,如果l相同,按照r从小到大排。然后用优先队列保存所有l值小于等于当前人数kt的soda。优先队列是让r按照小的优先。每次从队列中取出soda,判断是否r值大于等于当前人数kt。如果是,则让kt加1,放入ans数组。如果不是,则放入b数组。如果队列中没有元素的r值大于等于当前人数kt,则说明出现中断,以后的所有soda都放入b数组。
 
#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——————【贪心+优先队列】的更多相关文章

  1. 2015多校第6场 HDU 5360 Hiking 贪心,优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须 ...

  2. HDU 5360 Hiking (贪心)

    题意:邀请 n 参加聚会,如果在邀请第 i 个人之前,已经成功邀请了 x 个人,并且 li <= x <= ri,那么第 i 人才会去,问你怎么排列使得邀请的人最多. 析:对于所有的人,按 ...

  3. HDU 5360 Hiking(优先队列)

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  4. hdu 5360 Hiking(优先队列+贪心)

    题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L, ...

  5. HDU 5360 Hiking 登山 (优先队列,排序)

    题意: 有n个人可供邀请去hiking,但是他们很有个性,每个人都有个预期的人数上下限[Li,Ri],只有当前确定会去的人数在这个区间内他才肯去.一旦他答应了,无论人数怎样变更,他都不会反悔.问最多能 ...

  6. 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 ...

  7. 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 ...

  8. HDU 5360 (贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:告诉你n个区间[ l[i],r[i] ],然后让你排序,必须左区间不大于它前边的总区间个数 ...

  9. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

随机推荐

  1. code manager tools myeclipse10 svn插件安装及使用

    一.下载: 1.在线安装地址:http://subclipse.tigris.org/update_1.6.x 2.安装包下载地址:http://subclipse.tigris.org/servle ...

  2. @RestControllerAdvice注解使用

    在spring 3.2中,新增了@ControllerAdvice,@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...

  3. win10更新后IE不见了

    只是快捷方式不见了,到C:\Program Files\internet explorer\iexplore.exe就看到了,可以正常运行

  4. java中抽象类与接口

    1.抽象类是类,它的子类不能再继承其它类了,但可以实现一个和多个接口.接口不是类,它的子接口可以继承多个接口.2.抽象类中是可以有不用abstract修饰的方法,而接口中只能有抽象方法,即方法都要用a ...

  5. P2542 [AHOI2005]航线规划 LCT维护双连通分量

    \(\color{#0066ff}{ 题目描述 }\) 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系--一个巨大的由千百万星球构成的Samuel ...

  6. P4172 [WC2006]水管局长 LCT维护最小生成树

    \(\color{#0066ff}{ 题目描述 }\) SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的 ...

  7. 模板【洛谷P3390】 【模板】矩阵快速幂

    P3390 [模板]矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 矩阵A的大小为n×m,B的大小为n×k,设C=A×B 则\(C_{i,j}=\sum\limits_{k=1}^{n}A_{i, ...

  8. 数据结构java学习(三)循环队列

    @TOC 和栈一样,队列也是表,但是使用队列的特点是先进先出. 队列模型 \(\color{black}{队列的基本操作是入队,它是在表的末端插入一个元素,和出队,它是删除在表开头的一个元素}\) g ...

  9. CSS基础(续)

      老男孩第39天 老男孩 CSS  CSS的常用属性 4 文本属性 font-size: 10px; text-align: center; 横向排列 line-height: 200px; 文本行 ...

  10. java中Runtime类和Process类的简单介绍

    在java.lang包当中定义了一个Runtime类,在java中对于Runtime类的定义如下: Java code public class Runtime extends Object 每个 J ...