博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 5493 Queue(线段树)
阅读量:5142 次
发布时间:2019-06-13

本文共 2746 字,大约阅读时间需要 9 分钟。

Problem Description
N people numbered from 1 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≤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 next N lines consists of two integers hi and ki as described above (1≤hi≤109,0≤ki≤N−1). Note that the order of the given hi and ki is randomly shuffled.The sum of N over all test cases will not exceed 106

 

 
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 30 Case #2: 10 20 30 Case #3: impossible

 

Source

 

 

题意:有n个人,每个人的身高和左边或右边比自己高的人的个数num[i],输出符合给出的条件且字典序最小的从左到右队列里每个人的身高。 

题解:人有100000个,可以从线段树方法考虑,把问题转化为把每个人前面能留多少个空位给高个子的人,可以先按身高从小到大排个序,考虑第i个人前面留的位置肯定是num[i]或n-i-num[i]中的较小值,这样才能让字典序最小,一旦有n - i - num[i]的值小于0说明无解。

 

 

1 #pragma comment(linker, "/STACK:1024000000,1024000000")  2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 using namespace std; 16 #define PI acos(-1.0) 17 #define max(a,b) (a) > (b) ? (a) : (b) 18 #define min(a,b) (a) < (b) ? (a) : (b) 19 #define ll long long 20 #define eps 1e-10 21 #define MOD 1000000007 22 #define N 100006 23 #define inf 1e12 24 struct Node{ 25 int h,num; 26 }node[N]; 27 int n,s[N<<3],res[N]; 28 29 bool cmp(Node a,Node b){ 30 return a.h
View Code

 

转载于:https://www.cnblogs.com/UniqueColor/p/4851529.html

你可能感兴趣的文章
.NET使用Nuget阿里官方aliyun-net-sdk-core调用阿里云SMS
查看>>
Unable to add App ID because the '10' App ID limit in '7' days has been exceeded.
查看>>
Android几个主要版本的特性
查看>>
CCNA 之 七 路由协议 三 OSPF
查看>>
《oracle管理2》
查看>>
后台运行程序的几种方式
查看>>
html页面滚动时元素错位解决方案
查看>>
使用Markdown时如何插入公式
查看>>
loadrunner随笔1
查看>>
gslX680驱动的移植实践
查看>>
第三周——小小大佬带飞队
查看>>
linux下安装mysql
查看>>
20180531-Postman 常用测试结果验证及使用技巧
查看>>
简化日常工作系列之二 ----- 定时采集小说
查看>>
重新设定McAfee Agent的菜单语言
查看>>
IOS判断Iphone5和Retina
查看>>
西瓜书与蓝皮书 思维导图(转)
查看>>
构建之法阅读笔记03
查看>>
delphi类大体描述.(整体讲解)不错
查看>>
大话设计模式:模板方法设计模式
查看>>