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]中没有出现过得最小的非负整数, 然后我 ...
随机推荐
- Ubuntu 启动器/快捷方式/ 制作 (Eclipse为例)
首先,在路径/usr/share/applications/,中创建eclipse.desktop(如果没有的话) sudo touch /usr/share/applications/eclipse ...
- Hive 9、Hive 在表中添加正则匹配
在Hive中还有一项比较好用的功能,也是非常重要的功能:在建表的时候可以不指定表的行.字段.列的分隔方式,通过给表指定一段正则表达式,让Hive自动去匹配: 1.创建表 CREATE TABLE ap ...
- Apache POI组件操作Excel,制作报表(一)
Apache的POI组件是Java操作Microsoft Office办公套件的强大API,其中对Word,Excel和PowperPoint都有支持,当然使用较多的还是Excel,因为Word和Po ...
- OJ2.0userInfo页面Modify逻辑bug修复,search功能逻辑实现
这周的主要任务:userInfo页面Modify逻辑bug修复,search功能逻辑实现. (一)Modify逻辑bug修复: 这里存在的bug就是在我们不重置password的时候依照前面的逻辑是不 ...
- iOS ARC注释和错误的解决方法在使用
1.一个错误The current deployment target does not support automated __weak references 这个错误被所述支持iOS版本号不支持相 ...
- HDU 1863:畅通project(带权值的并查集)
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6627260 在前面一篇文章浅谈Service ...
- cookie那些事
本文面向对cookie有基本了解的读者,小白出门左转 设置cookie (HTTP 响应头) Set-Cookie: {name}={value};path={path};domain={doma ...
- jQuery中click()与trigger方法的区别
click()可以执行单击事件,但是不可传参. $("button").click(function(){ alert("hello."); }); trigg ...
- Delphi 接口托管实现
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...