hdoj 5493 Queue 【线段树 单点更新 + 区间查询】
来源:互联网 发布:提词器app软件 编辑:程序博客网 时间:2024/05/17 09:05
Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 471 Accepted Submission(s): 252
Problem Description
Every person has a unique height, and we denote the height of the
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≤1000 ).
Each test case starts with a line containing an integerN indicating the number of people in the queue (1≤N≤100000 ). Each of the next N lines consists of two integers hi and ki as described above (1≤hi≤109,0≤ki≤N−1 ). Note that the order of the given hi and ki is randomly shuffled.
The sum ofN over all test cases will not exceed 106
Each test case starts with a line containing an integer
The sum of
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
3310 120 130 0310 020 130 0310 020 030 1
Sample Output
Case #1: 20 10 30Case #2: 10 20 30Case #3: impossible
题意:N个人排队,由于吃午饭队伍散了。已知每个人的身高h(身高唯一),和一个数字num——代表 要么在他前面比他高的人有num个 要么在他后面比他高的人有num个。 现在让你找出一个满足约束的序列,若不存在输出impossible,反之输出字典序最小的序列。
思路:先按身高升序排列,排序后,对第i个插入的人,可以求出最优情况下他前面的人有min(N-i-num, num)个。
下面就是用线段树更新、维护、查询区间里面空位数,在每次插入前需要判断区间空位数是否少于num值。
AC代码:
#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 100000+1#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define INF 0x3f3f3f3fusing namespace std;struct Tree{ int l, r; int sum;//剩余位置};Tree tree[MAXN<<2];struct rec{ int h, num; int pos;};rec man[MAXN];bool cmp(rec a, rec b){ return a.h < b.h;}bool cmp1(rec a, rec b){ return a.pos < b.pos;}void PushUp(int o){ tree[o].sum = tree[ll].sum + tree[rr].sum;}void build(int o, int l, int r){ tree[o].l = l; tree[o].r = r; tree[o].sum = 1; if(l == r) return ; int mid = (l + r) >> 1; build(lson); build(rson); PushUp(o);}int query(int o, int v){ if(tree[o].l == tree[o].r) return tree[o].l; int mid = (tree[o].l + tree[o].r) >> 1; if(tree[ll].sum >= v) return query(ll, v); else return query(rr, v-tree[ll].sum);}void update(int o, int P){ if(tree[o].l == tree[o].r) { tree[o].sum = 0; return ; } int mid = (tree[o].l + tree[o].r) >> 1; if(P <= mid) update(ll, P); else update(rr, P); PushUp(o);}int main(){ int t, k = 1; scanf("%d", &t); int N; while(t--) { scanf("%d", &N); for(int i = 1; i <= N; i++) scanf("%d%d", &man[i].h, &man[i].num); sort(man+1, man+N+1, cmp); build(1, 1, N); bool flag = true; for(int i = 1; i <= N; i++) { if(N-i+1 <= man[i].num) { flag = false; break; } int Front = min(N-i-man[i].num, man[i].num);//最优状态下前面有多少个人 int P = query(1, Front+1); update(1, P); man[i].pos = P;//记录位置 } printf("Case #%d: ", k++); if(flag) { sort(man+1, man+N+1, cmp1);//按最优位置先后排序 for(int i = 1; i <= N; i++) { if(i > 1) printf(" "); printf("%d", man[i].h); } printf("\n"); } else printf("impossible\n"); } return 0;}
0 0
- hdoj 5493 Queue 【线段树 单点更新 + 区间查询】
- hdoj 4339 线段树 单点更新,区间查询
- hdoj 4819 Mosaic 【二维线段树 单点更新 区间查询】
- HDOJ 5316 Magician【线段树 单点更新 区间查询】
- HDOJ-2795 Billboard [线段树][单点更新+单点查询+维护区间最值]
- zoj (单点更新区间查询:线段树)
- 线段树单点更新和区间查询
- hdu1166(线段树单点更新区间查询)
- hdoj 4325 Flowers 【线段树 + 离散化】【区间更新 单点查询】
- HDOJ 4325 Flowers 【线段树 离散化 区间更新 单点查询】
- HDOJ 5596 GTW likes gt (线段树区间更新+单点查询+技巧)
- HDOJ 4399 —— 线段树单点更新&单点查询
- hdu5316 Magician (线段树+单点更新+区间查询+区间合并)
- HDOJ--4893--Wow! Such Sequence!【线段树+单点、区间更新】
- hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】
- [模板]线段树的建树、查询、单点更新、区间更新
- 线段树(堆式)[单点更新, 区间查询]
- HDU 2795 线段树(单点更新 区间查询)
- Android的 LinearLayout的布局属性
- Tomcat+MySql 配置图解
- English power——每天给彼此一个stupid smile
- 数字求和
- 线程知识的学习 一
- hdoj 5493 Queue 【线段树 单点更新 + 区间查询】
- 数字求和
- iOS float类型比较大小
- ocp-20
- iOS- 集成百度地图
- 【SSH快速进阶】——Hibernate自动建表
- iOS开发之有趣的UI —— iOS8之前和之后的自定义不等高cell
- ocp-21
- 回首过去,展望未来 (二)