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]中没有出现过得最小的非负整数, 然后我 ...
随机推荐
- 单链表之C++实现
在实现单链表时要注意对单链表的逻辑存储.物理存储有清晰的概念. 如上图链表已经完成,其逻辑结构如上.当需要对其进行操作,比如插入.删除,通常需要引 入指针,如上的ptr1.ptr2.在编程时一定要注意 ...
- 一个sigaction的C++ wrap
在上一篇文章(http://www.cnblogs.com/coding-my-life/p/4220128.html)中,提到了libev提供了处理信号的C++ wrap.但我显然接受不了需要进入l ...
- LinqToXML~读XML文件续
上篇文章读了如何通过linq to xml去读取XML文件,而这讲主要通过linq to xml来读取由属性组件的XML文件,例如读取一个web.config的XML格式的配置文件,下面是config ...
- LinqToXML~读XML文件
linq的出现,带给我们的是简结,快速,可读性,它由linq to sql,linq to object,linq to XML组成,我的博客之前有对linq to sql的讲解,而今天,我将讲一个l ...
- [转]CodeIgniter与Zend Acl结合实现轻量级权限控制
Tag :CodeIgniter Zend Acl 权限控制 1. Zend_Acl简介 Zend_Acl 为权限管理提供轻量并灵活的访问控制列表 (ACL,access control list) ...
- 1. Git 克隆代码
1. Git 克隆代码 git clone git://github.com/facebook/hiphop-php.git 2. Git更新分支 查看服务器上的所有分支 [huzg@slave3 h ...
- 密码算法详解——AES
0 AES简介 1997年1月2号,美国国家标准技术研究所宣布希望征集一个安全性能更高的加密算法(AES)[3],用以取代DES.我们知道DES的密钥长度是64 bits,但实际加解密中使用的有效长度 ...
- ie11加载不了css的问题
ie11打开页面,竟然一点css都没有加载出来,而且是老大的电脑遇到了这个问题,啃爹啊 上网查资料,做了如下修改: 1.pc端最好不要用h5来写页面,兼容性是个头疼的问题,切记切记(移动端可以用H5来 ...
- javascript运动功能-分享到
<script> //窗体载入,为div控件绑定事件 window.onload = function () { var div1 = document.getElementById('d ...
- django的Model 模型中常用的字段类型
常用的字段类型: AutoField:自增长字段,通常不用,如果未在Model中显示指定主键,django会默认建立一个整型的自增长主键字段 BooleanField:布尔型,值为True或False ...