题目链接:

Queue

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1093    Accepted Submission(s): 566

Problem Description
N people numbered from 1 to N are waiting in a bank for service. They all stand in a queue, but the queue never moves. It is lunch time now, so they decide to go out and have lunch first. When they get back, they don’t remember the exact order of the queue. Fortunately, there are some clues that may help.
Every person has a unique height, and we denote the height of the i-th person as hi. The i-th person remembers that there were ki people who stand before him and are taller than him. Ideally, this is enough to determine the original order of the queue uniquely. However, as they were waiting for too long, some of them get dizzy and counted ki in a wrong direction. ki could be either the number of taller people before or after the i-th person.
Can you help them to determine the original order of the queue?
 
Input
The first line of input contains a number T indicating the number of test cases (T≤1000).
Each test case starts with a line containing an integer N indicating the number of people in the queue (1≤N≤100000). Each of the next N lines consists of two integers hi and ki as described above (1≤hi≤109,0≤ki≤N−1). Note that the order of the given hi and ki is randomly shuffled.
The sum of N over all test cases will not exceed 106
 
Output
For each test case, output a single line consisting of “Case #X: S”. X is the test case number starting from 1. S is people’s heights in the restored queue, separated by spaces. The solution may not be unique, so you only need to output the smallest one in lexicographical order. If it is impossible to restore the queue, you should output “impossible” instead.
 
Sample Input
3
3
10 1
20 1
30 0
3
10 0
20 1
30 0
3
10 0
20 0
30 1
 
Sample Output
Case #1: 20 10 30
Case #2: 10 20 30
Case #3: impossible

题意:

给出n个人的高度,以及这个人前面或者后面有多少个比她高,现在让你求字典序最小的队列的身高;

思路:

排序后可以得到这个队列中比某个人高的总人数假设是num,如果num<k比这个总人数还多的话就不可能了;

然后我们把这个k变成前边的比她高的人数,这个k是min(k,num-k)这样保证了字典序最小;

然后就是求每个位置上的身高呢,将身高从小大排序,这就是相当于给了一个序列的逆序数,然后让你还原这个序列了;

从小到大贪心,对于每个人二分她的位置,确定后更新到树状数组中,复杂度O(n*log2n);

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
int n,ans[maxn],sum[maxn],hi[maxn];
struct node
{
int h,k,id;
}po[maxn];
int cmp(node a,node b){return a.h>b.h;}
int cmp1(node a,node b)
{
if(a.h==b.h)return a.k<b.k;
return a.h<b.h;
}
inline int lowbit(int x){return x&(-x);}
inline void update(int x)
{
while(x<=n)
{
sum[x]++;
x+=lowbit(x);
}
}
inline int query(int x)
{
int s=0;
while(x)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int t,Case=0;
scanf("%d",&t);
while(t--)
{
printf("Case #%d:",++Case);
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d%d",&po[i].h,&po[i].k),po[i].id=i;
sort(po+1,po+n+1,cmp);
int flag=0,num=0;po[0].h=-1;
for(int i=1;i<=n;i++)
{
if(po[i].h!=po[i-1].h)num=i-1;
if(po[i].k>num){flag=1;break;}
hi[i]=num;
}
if(flag)printf(" impossible\n");
else
{
for(int i=1;i<=n;i++)po[i].k=min(po[i].k,hi[i]-po[i].k),sum[i]=0;
sort(po+1,po+n+1,cmp1);
for(int i=1;i<=n;i++)
{
int l=po[i].k+1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(mid-query(mid)>po[i].k)r=mid-1;
else l=mid+1;
}
ans[r+1]=po[i].h;
update(r+1);
}
for(int i=1;i<=n;i++)printf(" %d",ans[i]);
printf("\n");
}
}
return 0;
}

  

hdu-5493 Queue(二分+树状数组)的更多相关文章

  1. 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  2. 【BZOJ-2527】Meteors 整体二分 + 树状数组

    2527: [Poi2011]Meteors Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 831  Solved: 306[Submit][Stat ...

  3. BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组

    BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位 ...

  4. bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)

    https://www.lydsy.com/JudgeOnline/problem.php?id=3173 插入的数是以递增的顺序插入的 这说明如果倒过来考虑,那么从最后一个插入的开始删除,不会对以某 ...

  5. 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...

  6. zoj-3963 Heap Partition(贪心+二分+树状数组)

    题目链接: Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence ...

  7. 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组

    题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...

  8. 【bzoj2527】[Poi2011]Meteors 整体二分+树状数组

    题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BI ...

  9. [ZJOI2006]书架(二分+树状数组)

    这题90%以上的人做法为裸的平衡树,实际上根本没必要还常数大,最好的方法是二分+树状数组.具体做法是,开3倍内存,初始把中间n位赋值为1.对于每个操作:1&2.删除该位,将其丢在头/尾(开三倍 ...

随机推荐

  1. 原生JS:Date对象详细参考

    Date对象:基于1970年1月1日(世界标准时间)起的毫秒数 本文参考MDN做的详细整理,方便大家参考MDN 构造函数: new Date(); 依据系统设置的当前时间来创建一个Date对象. ne ...

  2. PHP美元符和花括号组合那些事—${${}}

    双美元符+{}:${${variable}}是一种比较常见的用法,但是它的实现原理是什么呢?今天来探究一下: 提及这种用法,还得先说一下PHP的String类型php.net上指出,一个字符串可以用4 ...

  3. SharePoint 2013 WebPart 管理工具分享[开源]

    前言 之前做门户的时候,经常要导入导出WebPart,非常的频繁,然后就需要一个个导出,然后一个个导入,非常繁琐:闲暇之际,就考虑能不能自动化一下,把这个功能写成一个工具,可以方便的管理WebPart ...

  4. SharePoint 2013 JavaScript 对象判断用户权限

    场 景 近期有个场景,判断当前用户对项目有没有编辑权限,使用JavaScript完成,弄了好久才弄出来,分享一下,有需要的自行扩展吧,具体如下: 代 码 function getPermissions ...

  5. 微信小程序之后台https域名绑定以及免费的https证书申请

    微信小程序在11月3号发布了,这是一个全新的生态,没有赶上微信公众号红利的开发者,运营者可别错过这趟车了. 但是微信的后台需要全https,之前我还不相信,后台注册了后进后台才发现,服务器配置如下图 ...

  6. UIscrollView和UIPageControl的循环滚动

    因为昨天在网上找了很久,很多只能实现向右滚动,而且一张图一个imageview ,感觉工作量很可怕啊 ,  下面的例子就是不论你多少图 , 只和我代码里面的几个数值有关,  只需要修改分页和循环i的最 ...

  7. mysql GROUP BY 与 ORDER BY 查询不是最新记录

    转载:http://blog.csdn.net/qvbfndcwy/article/details/7200910 鉴于项目的需要,就从网上找到该文章,文章分析得很详细也很易懂,在android里,( ...

  8. 振奋人心啊!!!!下一代.NET——ASP.NET vNext

    这两天看到的.NET的新闻都好振奋人心啊!微软北美技术大会带来了好多好消息! 看到一篇博客园的文章,感觉太棒了.摘录下来.原文链接:http://news.cnblogs.com/n/208133/ ...

  9. Vector和Stack(已过时,不建议使用)

    以下内容基于jdk1.7.0_79源码: 什么是Vector和Stack Vector:线程安全的动态数组 Stack:继承Vector,基于动态数组实现的一个线程安全的栈: Vector和Stack ...

  10. Javascript 优化项目代码技巧之语言基础(一)

        Javascript的弱类型以及函数作用域等规则使用编写Javascript代码极为容易,但是编写可维护.高质量的代码却变得十分困难,这个系列的文章将总结在项目开发过程中,能够改善代码可读性. ...