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]中没有出现过得最小的非负整数, 然后我 ...
随机推荐
- 创建UILabel
UILabelCreate.h #import <UIKit/UIKit.h> @interface UILabelCreate : UILabel /** * 创建UILabel 初始化 ...
- [core Java学习笔记][第一二三章基本语法]
基本语法 1 Java 简单的类型 1.1 一些常量 正无穷大 Double.POSITVE_INFINITY 负无穷大 Double.NEGATIVE_INFINITY 不存在 Double.NaN ...
- WEB服务器6--IIS架构补充篇
第一部分我将谈谈IIS的两个不同的版本—IIS 5.x 和 IIS 6的处理模型:IIS如何监听来自外界的Http request,如何根据ISAPI Extension Mapping将对于不同Re ...
- css-下拉菜单案例
<!DOCTYPE html>CSS4-布局2-display下拉菜单案例 <style>.xiala{width:200px;background:#ddd;}.xiala ...
- html中opacity的使用
今天做项目要用到一个层背景透明,层上的内容不透明的效果 结果网上找了半天,没一个靠谱的, 最后倒是被一句话点醒了:纸烧了,纸上面的字也会没了 所以,要设2层遮罩层,看代码: .dialog_1//内容 ...
- Android-------手机屏幕适配之文件适配
public class Main { //定义文件本地存储路径,可按照需求更改 private final static String rootPath = &quo ...
- 0119——UITextField
1.默认文本 _loginTextField.placeholder = @"QQ号/手机"; 2.设置边框 _loginTextField.borderStyle = UITe ...
- hdu3998 Sequence(最大流,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Sequence Time Limit: 2000/1000 MS (Java/O ...
- Linq的基础2
var 构建匿名类型1 = from c in ctx.GetTable<Customers>() select new ...
- 设置secureCRT中vim的字体颜色
1.在/etc/vimrc新增以下一行 syntax on 注:上述方法对root用户无效,原因为在一般用户中,alias vi=vim,而在root用户中默认无此设置,因此若需要root用户也显示颜 ...