HDU 5493 线段树
来源:互联网 发布:树莓派安装unity3d 编辑:程序博客网 时间:2024/06/05 06:14
Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1368 Accepted Submission(s): 693
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 个比他高的人
让你在满足条件情况下按字典序最小排序。
思路:先让人按照高度从小到大排序,然后从小到大去给它安排位置
例如:样例 3 10 1 20 1 30 0
当前调用的人数据 为 h = 10 num = 1,那么说明他前面或后面需要留有 1个空位,因为后面进来的人都会比他高,我们就拿前面来留空位好了,用掉一个位置后还剩2个位置,因为后面都比它高,所以它必须在2位置
接下来的人数据为 h = 20 num = 1 ,那么它前或后需要一个空位,用掉自己的位置还剩1个空位,所以此时它放前放后都可以,为了字典序最小,所以它放前。
int cnt = n - i;
int tmp = cnt - a[i].num;
tmp = min(tmp,a[i].num); 表示可以放在剩下的第tmp个也可以放第a[i].num个,选小的放,使字典序最小
int tmp = cnt - a[i].num;
tmp = min(tmp,a[i].num); 表示可以放在剩下的第tmp个也可以放第a[i].num个,选小的放,使字典序最小
在update的时候,判断这个区间里够不够 need(需要空位),如果够的话就进入这个区间去放置数,不够的话则去另一个区间补,比如你需要5个空位,而左边的区间只有3个空位,那么你就得去右边区间找,此时右边区间需要的是need - tree[i << 1].sum = 2个空位
这样使用线段树的做法相比你直接在数组里放数之后一个一个去找要快的多,而且不会浪费空间,他可以利用区间剩余空间 sum 巧妙地跳过空位找到他需要放置的地方。
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define mem(a,x) memset(a,x,sizeof(a));#define maxn 250005struct trie{int l,r,sum;}tree[maxn * 4];struct node{int h,num;}a[maxn * 4];int val[maxn * 4];bool cmp(node a,node b){return a.h < b.h;}void push_up(int root){tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;}void build(int i,int l,int r){ tree[i].l = l; tree[i].r = r; tree[i].sum = 1; if(l == r) return ; int mid = (l + r) >> 1; build(i << 1,l,mid); build(i << 1 | 1,mid + 1,r); push_up(i);}void update(int need,int v,int i){if(tree[i].l == tree[i].r){val[tree[i].l] = v;tree[i].sum = 0;return;}if(need <= tree[i << 1].sum){ //如果这个区间里的数足够多,那就放到这个区间里去 update(need,v,i << 1);}else{//不够的话则放到右边的区间去 update(need - tree[i << 1].sum,v,i << 1 | 1);}push_up(i);}int main(){int t,n,m,Case = 1;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i = 1;i <= n;i++){scanf("%d %d",&a[i].h,&a[i].num);}sort(a + 1,a + 1 + n,cmp);build(1,1,n);int flag = 0;for(int i = 1;i <= n;i++){int cnt = n - i;int tmp = cnt - a[i].num;tmp = min(tmp,a[i].num); //可以放在剩下的第tmp个也可以放第a[i].num个,选小的放,使字典序最小 if(tmp < 0){flag = 1;break;}update(tmp + 1,a[i].h,1);}printf("Case #%d: ",Case++);if(flag)puts("impossible");else{for(int i = 1;i <= n;i++){printf("%d",val[i]);if(i != n)printf(" ");}puts("");}}return 0;}
阅读全文
1 0
- hdu 5493(线段树)
- hdu 5493(线段树)
- HDU 5493 线段树
- HDU 5493 线段树
- hdu 5493 Queue 线段树
- hdu 5493 Queue 线段树
- HDU 5493 Queue(线段树)
- HDU 5493 Queue 线段树
- hdu 5493线段树+模拟
- hdu 5493 Queue 线段树
- hdu 5493 Queue 贪心+线段树
- hdu 5493 Queue(线段树)
- hdu 5493 Queue(线段树)
- hdu 5493 Queue(逆序对,线段树)
- hdu 5493 Queue 线段树加二分
- 线段树 HDU - 5493 加贪心
- HDU-5493---Queue (线段树)
- hdu 1754 线段树
- 解决:javac: 无效的目标发行版: 1.8
- ios中几种跳转方式,普通,和StoryBoard跳转
- 基于UWB技术的隧道人员定位系统分析
- 设计模式的几个原则
- 51nod 1405 树的距离之和
- HDU 5493 线段树
- ubuntu16.04环境下安装Python SPAMS包
- Android开发第三方库glide和圆形控件circleimageview一起使用第一次进入无法显示问题
- JavaWeb开发中关于JS校验的代码优化问题
- cordova 一些配置的讲解
- 全局变量、局部变量、const、static、extern
- redis全部数据类型命令整合
- AngularJS 学习笔记(3)-指令(Directive)
- 127.0.0.1可以访问,localhost不能访问的问题