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 ;
}

最新文章

  1. 【Java并发编程实战】-----&ldquo;J.U.C&rdquo;:ReentrantLock之一简介
  2. HDU 1005 F(Contest #1)
  3. js正则表达式replace里有变量的解决方法用到RegExp类
  4. R12 供应商SQL
  5. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON HighpassImage
  6. 77. Combinations
  7. iOS 十六进制的相加取反
  8. 应用之间进行跳转,ComponentName的方式
  9. Impala源代码分析---1
  10. VS2010 c/c++ 本地化 emscripten 配置
  11. 实验吧_貌似有点难(php代码审计)&amp;头有点大
  12. 探索未知种族之osg类生物---渲染遍历之Renderer::draw()简介
  13. 使用 Zabbix 监控 Jenkins
  14. linux添加自定义命令
  15. TCC分布式事务
  16. Web Api in Orchard
  17. berkelydb学习
  18. 猴子选大王【PHP】
  19. 关于Http协议、ASP.NET 核心知识(2)
  20. [转]将oracle数据库的编码变成utf-8

热门文章

  1. 【WIP_S2】递归
  2. 插入CSS的方法
  3. (博弈论)51NOD 1066 Bash游戏
  4. 【SPOJ-GCDEX】GCD Extreme(欧拉函数)
  5. Hadoop Hive概念学习系列之HiveQL编译基础(十)
  6. [转]ASP.NET MVC Domain Routing
  7. Mui使用jquery并且使用点击跳转新窗口
  8. Windows下重置MySQL密码【MYSQL】
  9. Java 8 (6) Stream 流 - 并行数据处理与性能
  10. netty 引用计数器 ,垃圾回收