Queue

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个人,每个人都有不同的身高,
  告诉你这个人向前有k个人比他高,或者是向后有k个人比他高两者满足其一,
  问你是否能给出一个身高字典序最小的排列,
  能的话就输出
 
题解:
  
  答案要使身高字典序最小,
  我们以身高自小到大插入序列
  假设我们放第i个数在x位置,我们放之前,保证好要留多少个空位给后面n-i个都比他大的数
  这个二分位置就好,BIT维护前缀和
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+, M = , mod = 1e9+, inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0 int n;
struct ss{int h,k;}a[N];
int cmp (ss s1,ss s2) {
return s1.h<s2.h;
}
struct BIT {
int tree[N] ; int lowbit(int x) {
return x&(-x);
}
void add(int x, int add, int n) {
for (; x <= n; x += lowbit(x)) {
tree[x] += add;
}
}
int sum(int x) {
int s = ;
for (; x > ; x -= lowbit(x)) {
s += tree[x];
}
return s;
}
void clears() {
memset(tree, , sizeof tree);
}
}Bit;
int cal(int x) {
int l = , r = n, ret = -;
while(l<=r) {
int mid = (l+r)>>;
int G = mid - Bit.sum(mid);
if(G>=x) r = mid-,ret = mid;
else l = mid+;
}
return ret;
}
int main() {
int T,cas = ,ans[N];
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&a[i].h,&a[i].k);
sort(a+,a+n+,cmp);
Bit.clears();bool OK = true;
for(int i=;i<=n;i++) {
int la = n - i;
int k = a[i].k;
if(a[i].k>la) {OK = false; break;}
int p = (k<=la/)? k:la-k;
int ret = cal(p+);
Bit.add(ret,,n);
ans[ret] = a[i].h;
}
printf("Case #%d: ", cas++);
if(!OK) puts("impossible");
else {
for(int i=;i<n;i++) printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
}
return ;
}
  

HDU 5493 Queue 树状数组+二分的更多相关文章

  1. hdu 5493 Queue 树状数组第K大或者二分

    Queue Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  2. HDU 5493 Queue 树状数组

    Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...

  3. POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)

    点我看题目 题意 :N个村子连成一条线,相邻的村子都有直接的地道进行相连,不相连的都由地道间接相连,三个命令,D x,表示x村庄被摧毁,R  ,表示最后被摧毁的村庄已经重建了,Q x表示,与x直接或间 ...

  4. hdu 5493 (树状数组)

    题意:在一个队列中,你知道一个人在他左边或者右边比他高的人的个数,求字典序最小的答案 思路:先将人按  矮-->高 排序,然后算出在每个人前面需要预留的位置.树状数组(也可以线段树)解决时,先二 ...

  5. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  6. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  7. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

  8. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  9. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

随机推荐

  1. JDBC基础02

    今日知识 1. sql注入问题2. jdbc批处理3. 事务 SQL注入问题解决 1.什么是sql注入. * 用户通过相关的特殊关键字sql语句非法访问数据库 *例如: Xxx(' or '1'='1 ...

  2. BZOJ 3065 替罪羊树+动态开节点线段树

    思路: RT 可以看VFK的题解 我写了半天拍了半天... 不过是$nlog^2n$的 要写垃圾回收的 线段树 如果某个节点的sum是0  也可以free掉 //By SiriusRen #inclu ...

  3. VirtualBox里如何正确安装增强工具(图文详解)

    不多说,直接上干货! 找到 复制到

  4. 异步lambda表达式

  5. Python 之 基础知识(五)

    一.变量 1.引用 id() 函数传参 与 返回值 都是传递保存的数据的引用 2.可变和不可变类型(变量的引用地址只在赋值语句后变化) 不可变类型 内存中的数据不允许被修改 数字类型 int,bool ...

  6. GCC G++ Make CMake自我科普

    Linux下gcc g++ make cmake 联系和区别 C/C++程序从编写到可执行一般经历这几个阶段 编写源代码 编译器编译代码生成目标文件,如.o文件 链接器链接目标文件和其他目标文件/库文 ...

  7. (转)RabbitMQ学习之Headers交换类型(java)

    http://blog.csdn.net/zhu_tianwei/article/details/40923131 Headers类型的exchange使用的比较少,它也是忽略routingKey的一 ...

  8. /usr/bin/ld: cannot find -lxxx 问题 解决方法总结

    最近在做毕设的收尾工作,很多程序都要部署到linux下来运行,遇到了挺多问题,昨天就集中性遇到了 在编译应用时,遇到了 /usr/bin/ld: cannot find -lxxx 这种情况是系统找不 ...

  9. Java中面向对象三大特性之——封装

    概述 面向对象编程语言是对客观世界的模拟,客观世界里成员变量都是隐藏在对象内部的,外界无法直接操作和修改. 封装可以被认为是一个保护屏障,防止该类的代码和数据被其他类随意访问.要访问该类的数据,必须通 ...

  10. 06 Django组件-cookie与session

    一.会话跟踪技术 1.什么是会话跟踪技术 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而 ...