HDU 5493 Queue 树状数组+二分
Queue
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?
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
3
10 1
20 1
30 0
3
10 0
20 1
30 0
3
10 0
20 0
30 1
Case #2: 10 20 30
Case #3: impossible
#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 树状数组+二分的更多相关文章
- hdu 5493 Queue 树状数组第K大或者二分
Queue Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5493 Queue 树状数组
Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
点我看题目 题意 :N个村子连成一条线,相邻的村子都有直接的地道进行相连,不相连的都由地道间接相连,三个命令,D x,表示x村庄被摧毁,R ,表示最后被摧毁的村庄已经重建了,Q x表示,与x直接或间 ...
- hdu 5493 (树状数组)
题意:在一个队列中,你知道一个人在他左边或者右边比他高的人的个数,求字典序最小的答案 思路:先将人按 矮-->高 排序,然后算出在每个人前面需要预留的位置.树状数组(也可以线段树)解决时,先二 ...
- 树状数组+二分||线段树 HDOJ 5493 Queue
题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...
- P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- TZOJ 4602 高桥和低桥(二分或树状数组+二分)
描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...
随机推荐
- B - Calculating Function
Problem description For a positive integer n let's define a function f: f(n) = - 1 + 2 - 3 + .. + ( ...
- B - Expression
Problem description Petya studies in a school and he adores Maths. His class has been studying arith ...
- Core篇——初探IdentityServer4(客户端模式,密码模式)
Core篇——初探IdentityServer4(客户端模式,密码模式) 目录 1.Oatuth2协议的客户端模式介绍2.IdentityServer4客户端模式实现3.Oatuth2协议的密码模式介 ...
- 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)
题目描述 Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always p ...
- Static variables in JavaScript
function MyClass () { // constructor function var privateVariable = "foo"; //NO:obj.privat ...
- jquey中的事件绑定
三种方法: $(selector).live(events, data, handler); // jQuery 1.3+ $(document).delegate(se ...
- 【Oracle】DG中 Switchover 主、备切换
操作系统:OEL 5.6 数据库版本:Oracle11gR2 11.2.0.4.0 Switchover切换要求主库和备库在数据同步情况下进行,是主备之间的正常切换,主要用于日常维护.灾备演练等.切 ...
- 基于ACE的TAO开发---一个简单的入门实例-----VS2008(二)
上一节已经说了如何编译idl文件.现在就用编好的文件来写一个最小的corba小程序的.程序分为服务器程序和客户端程序. 说明下,代码是<基于C++CORBA高级编程>一书中的例子. 1.首 ...
- 杭电1229 还是A+B
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1229 解题思路,最开始想的是把输入的数据存入数组中,比如输入 1,在数组中就储存为1000(因为数据不 ...
- 关于《Python核心编程》第2版和第3版
关于<Python核心编程>第2版和第3版 以前开始学Python的时候,根据某大神的建议买了本<Python核心编程>第2版,慢慢学习.而最近回家没带书回来,刚好JD有活动, ...