hdu 5493 Queue(线段树)
N people numbered from 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?
The first line of input contains a number T indicating the number of test cases (T≤).
Each test case starts with a line containing an integer N indicating the number of people in the queue (≤N≤). Each of the next N lines consists of two integers hi and ki as described above (≤hi≤,≤ki≤N−). Note that the order of the given hi and ki is randomly shuffled.
The sum of N over all test cases will not exceed
For each test case, output a single line consisting of “Case #X: S”. X is the test case number starting from . 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.
Case #:
Case #:
Case #: impossible
题意:有n个人,每个人的身高和左边或右边比自己高的人的个数num[i],输出符合给出的条件且字典序最小的从左到右队列里每个人的身高。
题解:人有100000个,可以从线段树方法考虑,把问题转化为把每个人前面能留多少个空位给高个子的人,可以先按身高从小到大排个序,考虑第i个人前面留的位置肯定是num[i]或n-i-num[i]中的较小值,这样才能让字典序最小,一旦有n - i - num[i]的值小于0说明无解。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
struct Node{
int h,num;
}node[N];
int n,s[N<<],res[N]; bool cmp(Node a,Node b){
return a.h<b.h;
} void pushup(int k){
s[k]=s[k*]+s[k*+];
} void build(int k,int left,int right){
if(left==right){
s[k]=;
return;
}
int mid=(left+right)/;
build(k*,left,mid);
build(k*+,mid+,right);
pushup(k);
} void modify(int k,int left,int right,int pos,int val){
if(left==right){
res[left]=val;
s[k]=;
return;
}
int mid=(left+right)/;
if(pos<=s[k*]){
modify(k*,left,mid,pos,val);
}else{
modify(k*+,mid+,right,pos-s[k*],val);
}
pushup(k);
} int main()
{
int ac=;
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&node[i].h,&node[i].num);
}
sort(node+,node++n,cmp);
build(,,n);//建树 int flag=;
for(int i=;i<=n;i++){
int m=n-i;
int tmp=m-node[i].num;
if(tmp<){
flag=;
break;
}
if(node[i].num<tmp){
modify(,,n,node[i].num+,node[i].h);
}else{
modify(,,n,tmp+,node[i].h);
}
}
printf("Case #%d: ",++ac);
if(flag==){
printf("impossible\n");
}
else{
printf("%d",res[]);
for(int i=;i<=n;i++){
printf(" %d",res[i]);
}
printf("\n");
}
}
return ;
}
hdu 5493 Queue(线段树)的更多相关文章
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 【线段树】HDU 5493 Queue (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5493 题目大意: N个人,每个人有一个唯一的高度h,还有一个排名r,表示它前面或后面比它高的人的个数 ...
- HDU 5493 Queue 【线段树】
<题目链接> 题目大意:给你n个人的身高和他前面或者后面身高大于他的人的个数,求一个字典序最小的满足此条件的序列,如果不存在输出“impossible”. 解题分析: 因为要保证字典序最小 ...
- HDU - 5493 Queue 2015 ACM/ICPC Asia Regional Hefei Online(线段树)
按身高排序,每个人前面最高的人数有上限,如果超出上限说明impossible, 每次考虑最小的人,把他放在在当前的从左往右第k+1个空位 因为要求字典序最小,所以每次k和(上限-k)取min值. 没有 ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
Conturbatio Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- hdu 4747 mex 线段树+思维
http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意: 我们定义mex(l,r)表示一个序列a[l]....a[r]中没有出现过得最小的非负整数, 然后我 ...
随机推荐
- JAVA中常用需要设置的三个环境变量(JAVA_HOME、CLASSPATH、PATH)
JAVA中常用需要设置的三个环境变量: JAVA_HOME.CLASSPATH.PATH (一) 配置环境变量:(相对路径) 1. JAVA_HOME=x:/jdk1.6.0 2. 用%JAVA_HO ...
- 【转】网络视频监控P2P解决方案
一.摘要 本文分析了日益增长的民用级别家庭和个人网络视频监控市场的需求特点,并给出了一种经济可行易于大规模部署的P2P解决方案. 由于篇幅有限,本文只给出了方案的思路,未对更深入的技术细节做详细的论述 ...
- feof()
百度知道 >电脑/网络 >编程语言 >C/C++ feof()这个函数是用来判断指针是否已经到达文件尾部的. 若fp已经指向文件末尾,则feof(fp)函数值为"真&quo ...
- python list 去重
print u'列表去重'a=[1,2,3,3,2,1,4,4,5,6,'a','a','b','c']print list(set(a))
- To Miss Our Children Time(dp)
To Miss Our Children Time Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Jav ...
- 黑马程序员 Java正则表达式,详解反斜线在Java中的作用
---------------------- ASP.Net+Android+IO开发S. .Net培训.期待与您交流! ---------------------- 在程序设计过程中,经常需要对获取 ...
- BOOST 线程完全攻略 - 扩展 - 事务线程
扩展threadtimermoduleexceptionsocket 什么叫事务线程 举个例子: 我们写一个IM客户端的登录子线程,则该子线程会有这么几个事务要处理 No.1 TCP Socket物理 ...
- css_day5
- 解决IE11只能用管理员身份运行的问题
解决IE11只能用管理员身份运行的问题 IE11 打不开,必须要用管理员身份运行才可以打开,而且重置浏览器这个方法也不奏效. 今天本人也遇到了,上网查找发现是注册表权限的问题,原因尚不明确,安装了或被 ...
- 读取xml文件转成List<T>对象的两种方法(附源码)
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...