HDU 5493 Queue
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.
解题:贪心占位,按高度由低到高排序后,贪心占位就是了
#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的更多相关文章
- HDU 5493 Queue 树状数组
Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...
- 【线段树】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(线段树)
Problem Description N people numbered to N are waiting in a bank for service. They all stand in a qu ...
- 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 ...
- hdu 5493 Queue 树状数组第K大或者二分
Queue Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5493 Queue 树状数组+二分
Queue Problem Description N people numbered from 1 to N are waiting in a bank for service. They all ...
- HDU 5493 Queue 【线段树】
<题目链接> 题目大意:给你n个人的身高和他前面或者后面身高大于他的人的个数,求一个字典序最小的满足此条件的序列,如果不存在输出“impossible”. 解题分析: 因为要保证字典序最小 ...
- HDU - 5493 Queue 2015 ACM/ICPC Asia Regional Hefei Online(线段树)
按身高排序,每个人前面最高的人数有上限,如果超出上限说明impossible, 每次考虑最小的人,把他放在在当前的从左往右第k+1个空位 因为要求字典序最小,所以每次k和(上限-k)取min值. 没有 ...
- Q - Queue HDU - 5493(树状树组维护区间前缀和 + 二分找预留空位)
Q - Queue HDU - 5493 Problem Description NNN people numbered from 1 to NNN are waiting in a bank for ...
随机推荐
- U - Relatives(欧拉函数)
Description Given n, a positive integer, how many positive integers less than n are relatively prime ...
- ViewPager(3)用viewpager实现tabhost
1.示例 2.代码 2.1 TabViewPagerMain.java import android.graphics.drawable.Drawable; import android.os.Bun ...
- 国际化------international
1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=& ...
- TensorFlow---基础---GFile
使用TensorFlow的时候经常遇到 tf.gfile.exists().... 关于gfile,一个googler是这样给出的解释: The main roles of the tf.gfile ...
- 每天学点Linux命令之Linux-Shell中的数据重定向与管道命令
在Linux shell中, 数据重定向使用 > < 符号,管道命令使用 | 符号链接前后两个命令. 具体区别如下: 数据重定向 1.(>): 左侧应该有标准输出 > 右侧只能 ...
- Paxos,Raft,Zab一致性协议-Raft篇
Raft是一个一致性算法,旨在易于理解.它提供了Paxos的容错和性能.不同之处在于它被分解为相对独立的子问题,它清楚地解决了实际系统所需的所有主要部分.我们希望Raft能够为更广泛的受众提供共识,并 ...
- 2559. [NOIP2016]组合数问题
[题目描述] [输入格式] 从文件中读入数据. 第一行有两个整数t, k,其中t代表该测试点总共有多少组测试数据,k的意义见[问题描述]. 接下来t行每行两个整数n, m,其中n, m的意义见[问题描 ...
- ubuntu下编译VLC源码
http://blog.csdn.net/beitiandijun/article/details/9225591ubuntu下编译VLC源码 分类: 视频处理 2013-07-02 17:33 57 ...
- PHP开发心得二
如何解决错误:PHP SOAP Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML documen ...
- Angular——单页面实例
基本介绍 1.引入的route模块可以对路由的变化做出响应 2.创建的控制器中依然需要$http向后台请求数据 3.php中二维数据的遍历用的是foreach 4.php中$arr=array(),$ ...