HDU5493 Queue(线段树)
来源:互联网 发布:ec软件 编辑:程序博客网 时间:2024/05/29 18:53
Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1363 Accepted Submission(s): 689
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
Source
2015 ACM/ICPC Asia Regional Hefei Online
题意:一个数列,针对每个数给出一个信息,告诉你该数左边或者右边比其大的数的个数,构造原数列,多解则输出字典序最小的。
思路:从小到大考虑,因为较小的数不会影响较大的数,每次放好一个数就把其位置删去,然后接着考虑即可。线段树维护前缀和。
#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>using namespace std;const long long INF=0xfffffffffffffff;const int MAXN=100010;struct node{ int x,h;}a[MAXN];int cmp(node x,node y){ return x.x<y.x;}int n;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int sum[MAXN<<2];void pushup(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void build(int l,int r,int rt){ if(l==r){ sum[rt]=1; return; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt);}void update(int p,int l,int r,int rt){ if(l==r){ sum[rt]=0; return; } int m=(l+r)>>1; if(p<=m){ update(p,lson); }else{ update(p,rson); } pushup(rt);}int query1(int x,int l,int r,int rt){ if(l==r){ if(sum[rt]) return l; else return -1; } int m=(l+r)>>1; int y=sum[rt<<1]; if(x<=y){ return query1(x, lson); }else{ return query1(x-y, rson); }}int res[MAXN];int main(){ int t; scanf("%d",&t); int cas=1; while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i].x,&a[i].h); } sort(a+1,a+n+1,cmp); build(1,n,1); memset(res,0,sizeof res); int cnt=n; int ok=1; for(int i=1;i<=n;i++){ int x1=query1(a[i].h+1, 1, n, 1); int x2=query1(cnt-a[i].h, 1, n, 1); if(cnt-1<a[i].h){ ok=0; break; } if(x1==-1&&x2==-1){ ok=0; break; } int pos; if(x1!=-1&&x2!=-1){ x1=min(x1,x2); res[x1]=a[i].x; pos=x1; }else if(x1==-1){ res[x2]=a[i].x; pos=x2; }else{ res[x1]=a[i].x; pos=x1; } cnt--; update(pos, 1, n, 1); } printf("Case #%d: ",cas++); if(ok==0){ puts("impossible"); continue; } for(int i=1;i<=n;i++){ printf("%d%c",res[i],i==n?'\n':' '); } }}
阅读全文
0 0
- HDU5493 Queue(线段树)
- hdu5493 Queue 线段树
- HDU5493 Queue【线段树】
- HDU5493 Queue(线段树)
- HDU5493 Queue(线段树)
- HDU5493 Queue 线段树单点更新
- hdu5493 线段树
- hdu5493 Queue
- HDU5493-Queue
- hdu5493(线段树,离线操作,点修改,区间查询)
- hdu5493
- hdu 5493 Queue 线段树
- hdu 5493 Queue 线段树
- HDU 5493 Queue(线段树)
- HDU 5493 Queue 线段树
- hdu 5493 Queue 线段树
- hdu4441 Queue Sequence(线段树+splay)
- hdu 4441 Queue Sequence (splay + 线段树)
- 并发基本概念介绍
- python基础——函数编程
- javascript随机数
- Surrounded_Regions
- 混合云起航
- HDU5493 Queue(线段树)
- ubuntu安装redis及使用
- 防盗链的基本原理与实现
- eclipse下常用快捷键
- ES基础概念
- 看看这个最简单的代码,你能看得懂吗
- unity UGUI之button解析
- http cache-control 指令的设置以及意义
- Python笔记(十四)--函数