HDU 5493 Queue(线段树)
来源:互联网 发布:iphonex监控预约软件 编辑:程序博客网 时间:2024/04/30 07:30
Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 736 Accepted Submission(s): 396
Problem Description
N people numbered from 1 toN 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 h_i. The i-th person remembers that there were k_i 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 k_i in a wrong direction.k_i could be either the number of taller people before or after thei-th person.
Can you help them to determine the original order of the queue?
Every person has a unique height, and we denote the height of the i-th person as h_i. The i-th person remembers that there were k_i 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 k_i in a wrong direction.k_i could be either the number of taller people before or after thei-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≤1000).
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 nextN lines consists of two integers h_i and k_i as described above (1≤h_i≤10^9,0≤k_i≤N-1). Note that the order of the givenh_i and k_i is randomly shuffled.
The sum of N over all test cases will not exceed10^6
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 nextN lines consists of two integers h_i and k_i as described above (1≤h_i≤10^9,0≤k_i≤N-1). Note that the order of the givenh_i and k_i is randomly shuffled.
The sum of N over all test cases will not exceed10^6
Output
For each test case, output a single line consisting of “Case #X: S”.X is the test case number starting from 1. 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
3310 120 130 0310 020 130 0310 020 030 1
Sample Output
Case #1: 20 10 30Case #2: 10 20 30Case #3: impossible
Source
2015 ACM/ICPC Asia Regional Hefei Online
#include <cstdio>#include <iostream>#include <string.h>#include <algorithm>using namespace std;const int N=1e5+10;struct Node{int h,x;}node[N];int re[N],sum[N*4];bool cmp(Node a,Node b){return a.h<b.h;}void Build(int now,int le,int ri){if(le==ri){sum[now]=1; //这个点还没被用 return;}int mid=(le+ri)>>1;int ne=now<<1;Build(ne,le,mid);Build(ne+1,mid+1,ri);sum[now]=sum[ne+1]+sum[ne]; //这段区间有多少个点没被用 }void Update(int now,int le,int ri,int pos,int val){if(le==ri){sum[now]=0; //该点被使用 re[le]=val; //插入位置 /*for(int i=1;i<=3;i++) printf(" %d",re[i]); printf("\n");*/return;}int mid=(le+ri)>>1;int ne=now<<1;if(pos<=sum[ne]) //这个区间段未使用的点够,则进入 Update(ne,le,mid,pos,val);else //不够,则进入右子树,要减去前面左子树未使用的点,因为左子树在前面 Update(ne+1,mid+1,ri,pos-sum[ne],val); sum[now]=sum[ne+1]+sum[ne];}int main(){int t,i,n,j;scanf("%d",&t);for(j=1;j<=t;j++){bool ok=1;//memset(sum,0,sizeof(sum));//memset(re,0,sizeof(re));scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d%d",&node[i].h,&node[i].x);Build(1,1,n);sort(node+1,node+n+1,cmp);for(i=1;i<=n;i++){int m=n-i; //还有多少个比现在的高 因为前面的人矮 不能算 要n-i int temp=m-node[i].x; if(temp<0){ ok=0; break; } //temp指后面方向高Node[i].x人的话的位置在哪// Node[i].x指前面方向的话位置在哪;//然后2个选靠前的 即更小的 if(node[i].x<=temp) //在前面预留node[i].x人 插入到 node[i].x+1位置 //如果存在就一直往后推,这个在Update里实现 Update(1,1,n,node[i].x+1,node[i].h); else Update(1,1,n,temp+1,node[i].h);} printf("Case #%d:",j);if(ok){for(i=1;i<=n;i++) printf(" %d",re[i]); printf("\n");}elseprintf(" impossible\n");}return 0;}/*9981 02 33 14 25 36 27 18 0*/
0 0
- hdu 5493 Queue 线段树
- hdu 5493 Queue 线段树
- HDU 5493 Queue(线段树)
- HDU 5493 Queue 线段树
- hdu 5493 Queue 线段树
- hdu 5493 Queue 贪心+线段树
- hdu 5493 Queue(线段树)
- hdu 5493 Queue(线段树)
- hdu 5493 Queue(逆序对,线段树)
- hdu 5493 Queue 线段树加二分
- HDU-5493---Queue (线段树)
- HDU 5493 Queue (线段树) 2015合肥网络赛
- HDU 5493 Queue(线段树啊 单点更新)
- HDU 5493 Queue (线段树)2015合肥赛区网络赛
- HDU 5493 Queue (线段树逐个插入)
- HDU 5493 Queue(线段树+离线处理)
- hdu 4441 Queue Sequence (splay + 线段树)
- 【线段树】HDU 5493 Queue (2015 ACM/ICPC Asia Regional Hefei Online)
- Poj.2689 Prime Distance【素数筛选】 2015/11/13
- 解决Linux平台上 php与mysql通讯产生中文乱码问题
- (解题报告)HDU4513---吉哥系列故事之礼尚往来(错排)
- ffmpeg 2.8.1 最新版本 VS2013 可调式动态库
- Android_SVG概述及生成使用SVG详解
- HDU 5493 Queue(线段树)
- C++常见面试题
- 哈密顿算符
- Nginx配置详解
- IOS,Tabbar,设置全局导航UINavigationBar
- MyEclipse强制退出后闪退进不去workbench
- 第十二章 与Spring集成
- MATLAB和C/C++混合编程实现图像处理(一)
- 创建网页版UFT脚本技术点