Queue

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 68    Accepted Submission(s): 40

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\leq 1000)$.
Each test case starts with a line containing an integer N indicating the number of people in the queue $(1\leq N\leq 100000)$. Each of the next N lines consists of two integers hi and ki as described above $(1\leq h_i\leq 10^9,0\leq k_i\leq N−1)$. Note that the order of the given $h_i\ and\ k_i$ is randomly shuffled.
The sum of N over all test cases will not exceed $10^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
3
3
10 1
20 1
30 0
3
10 0
20 1
30 0
3
10 0
20 0
30 1
 
Sample Output
Case #1: 20 10 30
Case #2: 10 20 30
Case #3: impossible
 
Source

解题:贪心占位,按高度由低到高排序后,贪心占位就是了

 #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct INFO {
int height,k;
bool operator<(const INFO &rhs)const {
return height < rhs.height;
}
} info[maxn];
struct node {
int lt,rt,val;
} tree[maxn<<];
int ret[maxn];
inline void pushup(int v) {
tree[v].val = tree[v<<].val + tree[v<<|].val;
}
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt) {
tree[v].val = ;
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid + ,rt,v<<|);
pushup(v);
}
void update(int pos,int v) {
if(tree[v].lt == tree[v].rt) {
tree[v].val = ;
return;
}
if(pos <= tree[v<<].rt) update(pos,v<<);
else update(pos,v<<|);
pushup(v);
}
int query(int cnt,int v,bool ok) {
if(cnt > tree[v].val) return INF;
if(tree[v].lt == tree[v].rt) return tree[v].lt;
if(ok) {
if(tree[v<<].val >= cnt) return query(cnt,v<<,ok);
else return query(cnt - tree[v<<].val,v<<|,ok);
}
if(tree[v<<|].val >= cnt) return query(cnt,v<<|,ok);
else return query(cnt - tree[v<<|].val,v<<,ok);
}
int main() {
int kase,n,cs = ;
scanf("%d",&kase);
while(kase--) {
scanf("%d",&n);
for(int i = ; i < n; ++i)
scanf("%d%d",&info[i].height,&info[i].k);
sort(info,info + n);
bool flag = true;
build(,n,);
for(int i = ; i < n && flag; ++i){
int L = query(info[i].k + ,,true);
int R = query(info[i].k + ,,false);
if(min(L,R) == INF) {
flag = false;
break;
}
update(min(L,R),);
ret[min(L,R)] = info[i].height;
}
printf("Case #%d:",cs++);
if(flag){
for(int i = ; i <= n; ++i)
printf(" %d",ret[i]);
puts("");
}else puts(" impossible");
}
return ;
}

HDU 5493 Queue的更多相关文章

  1. HDU 5493 Queue 树状数组

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

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

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

  3. hdu 5493 Queue(线段树)

    Problem Description N people numbered to N are waiting in a bank for service. They all stand in a qu ...

  4. hdu 5493 Queue treap实现将元素快速插入到第i个位置

    input T 1<=T<=1000 n 1<=n<=100000 h1 k1 h2 k2 ... ... hn kn 1<=hi<=1e9  0<=ki&l ...

  5. hdu 5493 Queue 树状数组第K大或者二分

    Queue Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. HDU 5493 Queue 树状数组+二分

    Queue Problem Description N people numbered from 1 to N are waiting in a bank for service. They all ...

  7. HDU 5493 Queue 【线段树】

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

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

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

  9. Q - Queue HDU - 5493(树状树组维护区间前缀和 + 二分找预留空位)

    Q - Queue HDU - 5493 Problem Description NNN people numbered from 1 to NNN are waiting in a bank for ...

随机推荐

  1. 浙江省CIO协会钱塘江论坛近日在网易云创沙龙宣布成立

    本文来自网易云社区 9月19日,由网易云与浙江省首席信息官协会共同主办的“网易云创CIO沙龙”在杭州举行,本次活动以“瞩目钱塘,赋能企业数字化创新”为主题,网易云企业服务部总经理岳峥辉,浙江省首席信息 ...

  2. map的遍历方式(使用Junit测试)

    package cn.sdut.lah; import java.util.HashMap; import java.util.Iterator; import java.util.Map; impo ...

  3. NestedPreb

    屌丝手动版 One of the things we’re sorely missing from Unity is nested prefabs. So we rolled this little ...

  4. Chrome教程之NetWork面板分析网络请求

    官方文档:https://developers.google.com/web/tools/chrome-devtools/network/ 最近打算写一写Chrome教程文档,不知道大家最感兴趣的是什 ...

  5. C头文件中尖括号与双引号的区别及编译搜索顺序

    这两天被问到一个很有意思的问题:C头文件中尖括号与双引号有什么区别,以前只大约知道 <> 常用在系统库文件,"" 常用在自定义的借口文件中,那具体在gcc编译搜索过程中 ...

  6. 大数高精度加减乘除 51nod 1005 大数加法

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...

  7. 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers

    题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...

  8. Spring加载applicationContext.xml实现spring容器管理的几种方式

    package com.etc.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; i ...

  9. List 的属性与方法整理

    List<T> 类与 ArrayList 类比较类似.它实现了 IList<T> 泛型接口,长度可以动态增加. 可以使用 Add 或 AddRange 方法将项添加到 List ...

  10. [转]TFS下的源代码控制

    本文转自:http://www.cnblogs.com/ajiefj/archive/2010/04/23/1718450.html 以下主要描述了: TFS源代码控制系统的基本场景 如何把一个项目添 ...