题干:

NN people numbered from 1 to NN 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 ii-th person as hihi. The ii-th person remembers that there were kiki 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 kiki in a wrong direction. kiki could be either the number of taller people before or after the ii-th person.
Can you help them to determine the original order of the queue?

Input

The first line of input contains a number TT indicating the number of test cases (T≤1000T≤1000).
Each test case starts with a line containing an integer NN indicating the number of people in the queue (1≤N≤1000001≤N≤100000). Each of the next NN lines consists of two integers hihi and kiki as described above (1≤hi≤109,0≤ki≤N−11≤hi≤109,0≤ki≤N−1). Note that the order of the given hihi and kiki is randomly shuffled.
The sum of NN over all test cases will not exceed 106106

Output

For each test case, output a single line consisting of “Case #X: S”. XX is the test case number starting from 1. SS 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

题目大意:

有 N 个人排队,每个人都忘记自己的位置。但是每个人都知道自己的Hi,Ki,Hi代表自己的身高,Ki代表自己前边后边比自己高的人的个数。给你每个人的 Hi,Ki,求满足题意的一组解。输出每个位置上站着的人的身高,如果有多组解,输出字典序最小的那一组。如果没有满足的情况,则输出"impossible"。

解题报告:

首先按照身高从小到大排个序,依次决策位置,因为题目给出的是身高比当前人高的人的个数,所以这样可以保证此时之前的决策对我自己没啥影响,因为之前的决策都是身高比我矮的,关于这一点题干中并没有约束。

考虑线段树维护没有被占的位置。这样只需要在线段树中查询从前往后第K+1个不为0的位置,从后往前第K+1个不为0的位置,选最靠前的一个位置放上这个人即可。

这题按照身高从大到小排序也是可解的。思路类似,但是因为只知道相对位置不知道每个人的实际位置,所以需要平衡树维护。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1e5 + 5;
struct Node{int h,id,k;bool operator<(const Node & b) const {return h < b.h;}
} R[MAX];
struct TREE {int l,r,sum;
} tr[MAX<<2];
void pushup(int cur) {tr[cur].sum = tr[cur*2].sum + tr[cur*2+1].sum;
}
void build(int l,int r,int cur) {tr[cur].l = l,tr[cur].r = r;if(l == r) {tr[cur].sum = 1;return;}int m = (l+r)>>1;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur);
}
int query(int k,int cur) {if(tr[cur].l == tr[cur].r) return tr[cur].l;if(tr[cur*2].sum >= k) return query(k,cur*2);else return query(k-tr[cur*2].sum,cur*2+1);
}
void update(int tar,int cur) {if(tr[cur].l == tr[cur].r) {tr[cur].sum = 0;return;}if(tar <= tr[cur*2].r) update(tar,cur*2);else update(tar,cur*2+1);pushup(cur);
}
int ans[MAX],n;
int main()
{int T,iCase=0;cin>>T;while(T--) {scanf("%d",&n);for(int i = 1; i<=n; i++) {scanf("%d%d",&R[i].h,&R[i].k);R[i].id = i;}sort(R+1,R+n+1); build(1,n,1);int flag = 1;for(int i = 1; i<=n; i++) {int res = tr[1].sum;if(R[i].k >= res) {flag = 0;break;}int tar = min(query(R[i].k+1,1),query(res-R[i].k,1));ans[tar] = R[i].h;update(tar,1);}printf("Case #%d:",++iCase);if(flag == 0) printf(" impossible");else for(int i = 1; i<=n; i++) printf(" %d",ans[i]);printf("\n");}return 0 ;
}

【HDU - 5493】Queue(思维,贪心,线段树)相关推荐

  1. Educational Codeforces Round 67 (Rated for Div. 2)(D思维题 线段树/E树形dp(换根dp) 二次扫描与换根法)

    心得 D写了个假算法被hack了wtcl- E据涛神说是二次扫描与换根法,看了看好像和树形dp差不多 F概率dp G费用流 回头再补 思路来源 马老师 归神 贤神等代码 http://www.mami ...

  2. CF1526C2 Potions (Hard Version) (贪心 + 线段树)

    题目链接: Potions (Hard Version) 大致题意 有n个数, 编号从1~n, 第i个位置的值为a[i]. 从编号为1的数字开始选择, 一直到编号为n的数字. 对于第i个数字, 你可以 ...

  3. HDU 1166 敌兵布阵(线段树:点更新,区间求和)

    HDU 1166 敌兵布阵(线段树:点更新,区间求和) http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意: 给你n个整数,然后给你多条命令,每条命令如 ...

  4. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  5. 2019CCPC网络赛 1002 HDU 6703(权值线段树)

    2019CCPC网络赛 1002 HDU 6703(权值线段树) 思路:用权值线段树存题目给的数据后,2操作就是求权值线段树中大于等于k的部分中,靠近左端点的第一个大于r的值(这个求出来的只是原序列中 ...

  6. hdu 5493 Queue(逆序对,线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5493 解题思路: 一道线段树的题目,因为是前面或者后面有k个比自己高的人,所以我们应该按照由身高从小到 ...

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

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

  8. CodeForces - 1529E Trees of Tranquillity(贪心+线段树)

    题目链接:https://vjudge.net/problem/CodeForces-1529E 题目大意:给出两棵根节点为 111 的树,分别称为 AAA 树和 BBB 树,现在通过两棵树可以构造出 ...

  9. hdu 4391 Paint The Wall 线段树 +优化 2012 Multi-University Training Contest 10 )

    http://acm.hdu.edu.cn/showproblem.php?pid=4391 题意: 刷墙, 以开始 有 n个节点,每个节点有一种颜色 ,m 次询问 m次  输入 a,l,r,z 如果 ...

  10. hdu 4288 Coder (成都赛区 线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=4288 题意: 给出一个有序集合,3种操作.插入一个数,删除一个数,都保证序列有序.以及求和 其中求和是将下标%5 ...

最新文章

  1. Android中设置TextView的颜色setTextColor
  2. Sass-也许你想和CSS玩耍起来(上篇)
  3. 5、kafka的操作
  4. 简述推荐系统中的矩阵分解
  5. 你知道C#中的Lambda表达式的演化过程吗
  6. java并发之线程池
  7. mysql库与oracle库的区别_开源数据库Oracle与MySQL的SQL语法区别
  8. UML(1) - 概述
  9. congruent matrix
  10. 数控直流电压源的设计与制作【keil5 AD20]
  11. OpenCV Mat类的convertTo函数,数据类型转换
  12. 对List<Map>数据排序
  13. Selenium IDE安装与运行
  14. ExecutorService,Executors 使用
  15. CFA插值基本方法简单介绍
  16. 火星存在大型地下水系统,火星或曾是一片海洋
  17. 日冕物质抛射检测matlab,中国科学技术大学 日冕物质抛射研究取得重要进展
  18. PHP--入门(一)
  19. C语言学习笔记13-文件
  20. 如何用python画函数曲线_python 画函数曲线示例 用python 怎么画函数图像

热门文章

  1. 安装mysql5.6.10_windows下安装mysql(mysql-installer-community-5.6.10.1)详细教程
  2. c语言char有什么作用,C语言中char*和char[]用法区别分析
  3. 1037C. Equalize
  4. c 的word转为html5,word与html互转(1) -- word转html
  5. python dropna失败_使用Python部署机器学习模型的10个实践经验
  6. java 代码重用需要注意的事项_程序员笔记|编写高性能的Java代码需要注意的4个问题...
  7. linux优先级队列,Python3 线程优先级队列( Queue)
  8. UE4 左右立体参数
  9. cmake学习(一)静态库与动态库构建
  10. VXWORKS 几种定时机制