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. Java学习笔记——反射

    反射就是把Java类中的各种成分映射成相应的java类. Class类-->java程序中的各个java类属于同一事物,描述这类事物的Java类名就是Class. Class.forName的作 ...

  2. bzoj 3401: [Usaco2009 Mar]Look Up 仰望【单调栈】

    用单调递减的栈从后往前扫一遍即可 #include<iostream> #include<cstdio> using namespace std; const int N=10 ...

  3. PWA技术深入学习

    PWA技术 PWA全称Progressive Web App,即渐进式WEB应用. 解决的问题 实现离线缓存功能,即使用户手机没有网络,依然可以使用一些离线功能 可以添加至主屏幕,点击主屏幕图标可以实 ...

  4. knockout 和mvc4结合使用

    Knockout (或者Knockout.js ,KnockoutJS)是一个开源的JavaScript库,网址为www.knockoutjs.com.Knockout语法简洁.可读性好,能轻松实现与 ...

  5. magento getUrl函数跳转admin模块问题

    在用以下代码时,跳转后的url里面会是空的,即没有admin这个值 $this->getUrl('admin/catalog_product/edit', array('id' => $c ...

  6. magento后台开发学习笔记(入门实例向)

    目的是做一个grid,参考博客http://www.sunzhenghua.com/magento-admin-module-development-part1-grid-forms-tabs-con ...

  7. 使用JS分页 <span> beta 2.0 未封装的分页

    <html> <head> <title>分页</title> <style> #titleDiv{ width:500px; backgr ...

  8. 全面学习ORACLE Scheduler特性(10)管理Chains

    5.2  管理Chains 5.2.1  修改Chains属性 基本上碰到修改CHAIN属性的机率不会太大,因此确实没啥可修改的,对于CHAIN对象来说,能够修改的属性只有两个:evaluation_ ...

  9. Intellij使用心得(四) -- 导入Eclipse的代码格式化文件

    https://my.oschina.net/flashsword/blog/137598

  10. Selenium 进行参数化

    Selenium参数化分为大小: 小:list.dict.函数 大:txt.excel.mysql.redis 哪种方式使自己的工作简单高效就选那种!!! Selenium进行参数化有多种形式: 本文 ...