HDU-5493---Queue (线段树)
来源:互联网 发布:同和行知职业技术学校 编辑:程序博客网 时间:2024/05/21 06:36
Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1391 Accepted Submission(s): 709
Total Submission(s): 1391 Accepted Submission(s): 709
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
Recommend
wange2014
题意:t组数据,n个人,每个人有个身高h,和在他前面或者后面比他高的人数k,输出满足条件的最小字典序序列,没有满足条件的序列输出impossible;
思路:首先不考虑字典序最小,那么先把每个人按身高从小到大排序,每个人的位置就是他当前在从左往右的第k个空位或者从右往左的第k个空位,如果没有这么多空位则输出impossible,再考虑字典序最小则是选择每个数填在从左往右和从右往左的第k个位置靠左的那一个,这样确定策略之后就好办了,我们可以考虑用线段树维护当前空位的个数,然后填上数字之后单点更新。主要是难得想,写起来并不难~
AC代码:
#include<iostream>#include<cstring>#include<set>#include<map>#include<vector>#include<stack>#include<string>#include<algorithm>#include<queue>#include<cstdio>#define LL long longusing namespace std;priority_queue<LL,vector<LL>,greater<LL> > que,q;int t;struct node{ int val; int id;}arr[100005];int brr[100005];bool cmp(node a,node b){ return a.val<b.val;}struct nn{ int l; int r; int sum;//空位数}segtree[100005*4];void pushup(int root){ segtree[root].sum=segtree[root<<1].sum+segtree[root<<1|1].sum;}void build(int root,int l,int r){ segtree[root].l=l; segtree[root].r=r; if(l==r) { segtree[root].sum=1; return; } int mid=(l+r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); pushup(root);}int query(int root,int id){ int ll=segtree[root].l; int rr=segtree[root].r; if(ll==rr) return ll; int mid=(ll+rr)>>1; if(segtree[root<<1].sum>=id) return query(root<<1,id); else return query(root<<1|1,id-segtree[root<<1].sum);}void update(int root,int p){ int ll=segtree[root].l; int rr=segtree[root].r; if(ll==rr) { segtree[root].sum=0; return; } int mid=(ll+rr)>>1; if(p<=mid) update(root<<1,p); else update(root<<1|1,p); pushup(root);}int main(){ int t; int cas=0; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&arr[i].val,&arr[i].id); sort(arr+1,arr+1+n,cmp);//按身高排序 build(1,1,n); int flag =0; for(int i=1;i<=n;i++) { int tmp=arr[i].id+1;//tmp为要填在第几个空位 if(arr[i].id+1>n-i+1-arr[i].id&&n-i+1-arr[i].id!=0) tmp=n-i+1-arr[i].id; if(segtree[1].sum<tmp)//没有这么多空格,直接结束 { flag=1; break; } int ans=query(1,tmp); brr[ans]=arr[i].val; update(1,ans); } printf("Case #%d: ",++cas); if(flag) printf("impossible\n"); else{ for(int i=1;i<=n;i++) printf("%d%c",brr[i],i==n?'\n':' '); } } return 0;}
阅读全文
0 0
- hdu 5493 Queue(线段树)
- hdu 5493 Queue(线段树)
- HDU-5493---Queue (线段树)
- hdu 5493 Queue 线段树
- hdu 5493 Queue 线段树
- HDU 5493 Queue(线段树)
- HDU 5493 Queue 线段树
- hdu 5493 Queue 线段树
- HDU 5493 Queue (线段树) 2015合肥网络赛
- HDU 5493 Queue (线段树逐个插入)
- HDU 5493 Queue(线段树+离线处理)
- hdu 5493 Queue 贪心+线段树
- hdu 5493 Queue(逆序对,线段树)
- hdu 5493 Queue 线段树加二分
- HDU 5493 Queue(线段树啊 单点更新)
- HDU 5493 Queue (线段树)2015合肥赛区网络赛
- hdu 4441 Queue Sequence (splay + 线段树)
- HDU5493 Queue(线段树)
- POJ2299-Ultra-QuickSort(树状数组+离散化)
- LeetCode笔记:(Java) 9. Palindrome Number
- java中String、StringBuffer、StringBuilder的区别
- Android 手机卫士(4) 包名/版本号/版本名的获取
- ListView 缓存机制源码浅析
- HDU-5493---Queue (线段树)
- Binary Tree Tilt
- 20170914
- 高阶函数
- 卡特兰数(Catalan)
- MFC picture 控件error RC2108: expected numerical dialog constant
- 为何要用番茄工作法(笔记)
- 03 Hiernate常见错误
- BZOJ 2744: [HEOI2012]朋友圈