Problem Description
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?
 
Input
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
 
Output
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.
Sample Input

 
Sample Output
Case #:
Case #:
Case #: impossible
Source

题意:有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(线段树)的更多相关文章

  1. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  2. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  3. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. 【线段树】HDU 5493 Queue (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5493 题目大意: N个人,每个人有一个唯一的高度h,还有一个排名r,表示它前面或后面比它高的人的个数 ...

  5. HDU 5493 Queue 【线段树】

    <题目链接> 题目大意:给你n个人的身高和他前面或者后面身高大于他的人的个数,求一个字典序最小的满足此条件的序列,如果不存在输出“impossible”. 解题分析: 因为要保证字典序最小 ...

  6. HDU - 5493 Queue 2015 ACM/ICPC Asia Regional Hefei Online(线段树)

    按身高排序,每个人前面最高的人数有上限,如果超出上限说明impossible, 每次考虑最小的人,把他放在在当前的从左往右第k+1个空位 因为要求字典序最小,所以每次k和(上限-k)取min值. 没有 ...

  7. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  8. hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值

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

  9. hdu 4747 mex 线段树+思维

    http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意: 我们定义mex(l,r)表示一个序列a[l]....a[r]中没有出现过得最小的非负整数, 然后我 ...

随机推荐

  1. Android应用开发学习之Toast消息提示框

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来看Toast消息提示框的用法.使用Toast消息提示框一般有三个步骤: 1.  创建一个Toast对象.可 ...

  2. JAVA中的正则表达式--待续

    1.关于“\”,在JAVA中的正则表达式中的不同: 在其他语言中"\\"表示为:我想要在正则表达式中插入一个普通的反斜杠: 在Java中“\\”表示为:我想要插入一个正则表达式反斜 ...

  3. 重载operator new实现检测内存泄漏是否可行

    行与不行,就凭我这水平,说出来未免显示太过自大.不还,我还想根据自己的代码来讨论这个问题. 重载operator new来检测内存只的办法,那就是在new的时候记录指针地址及文件名.行号,在delet ...

  4. PHP文件目录copy

    /**(2) PHP文件目录copy @param string $dirsrc 原目录名称字符串 @param string $dirto 目标目录名称字符串 */ function copyDir ...

  5. Python 异步IO、IO多路复用

    事件驱动模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  6. Animation Override Controller动画重载器

    假设游戏有很多个小人, 每一个人有2种动画站立,跑.  在通常情况下每一个人物都需要一个动画控制器. 有没有想过定义一个动画控制器 无须在定义全新的动画充值器实现每一个小人都播放自己的动画呢?没错An ...

  7. vps安全设置

    适合新手及才接触VPS的朋友们看一下.主要是关于VPS安全方面相关内容的 禁止ROOT登陆 保证安全性. 使用DDoS deflate简单防攻击. iftop Linux流量监控工具: 每日自己主动备 ...

  8. [Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into Immutable Data

    Immutable.js offers the fromJS() method to build immutable structures from objects and array. Object ...

  9. Swift基础--使用TableViewController自定义列表

    首先建立一个swift项目,把storyboard的内容删掉,添加一个 Navigation Controller,然后设置storyboard对应界面的class,在Navigation Contr ...

  10. 经典SQL语句大全(转载)

    原文http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html#top 一.基础 1.说明:创建数据库CREATE DATABAS ...