Queue
Time Limit:1 Sec
Memory limit:256 MB
Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=5493
Description
N people numbered from 1 to N is 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 has lunch first. When they get back, they don ' t remember the exact order of the queue. Fortunately, there is some clues.
Every person have a unique height, and we denote the height of the i-th person as hi. The i-th person remembers this there were Ki people who stand before him and is taller than him. Ideally, the 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 ki in a wrong direction. Ki could is either the number of taller people before or after the i-th person.
Can them to determine the original order of the queue?
Input
The first line of input contains a number T indicating the number of the test cases (t≤1000).
Each test case is starts with a line containing an integer N indicating the number of people in the queue (1≤n≤100000). Each of the next N lines consists of the 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 of N over all test cases would not exceed 106
Output
For each test case, the output a single line consisting the "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 is unique, so you have 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
HINT
Test instructions
Give you some people, everyone's height is not the same
And then give you a k, indicating the person's left or right, there are k people taller than him
And let you construct a sequence that satisfies this condition
Exercises
Everyone can actually stand in two positions, not on the left or right.
When you're small enough to plug in, the rest of the place is bigger than you.
You just plug it in.
It would be nice to maintain it with a tree-like array/splay/treap.
Code:
//Qscqesze#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<bitset>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200006#defineMoD 1000000007#defineEPS 1e-9#definee exp (1.0)#definePI ACOs (-1)#defineLowbit (x) (x) & (-X)Const DoubleEP = 1e-Ten ;intNum;//const int INF=0X7FFFFFFF;Constll inf=999999999; inline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//*************************************************************************************intN,VAL1[MAXN],VAL2[MAXN];voidAddint* VC,intU,intv) { while(U <=N) {Vc[u]+=v; U+=lowbit (U); }}intQueryint* VC,intu) { intres=0; while(u) {res+=Vc[u]; U-=lowbit (U); } returnRes;}intRankget (int* VC,intk) { intL =1, R =N; while(L <R) {intMID = L + ((r-l) >>1); intres =query (VC,MID); //cout << "Mid is" << mid << "Res is" << res << Endl; if(res = = k) R =mid; Else if(Res < k) L = mid +1; ElseR = mid-1; } returnL;}structkkk{intx, y;};BOOLcmp (KKK aa,kkk BB) {returnaa.x<bb.x;} KKK P[MAXN];intAns[maxn];vector<int>V1,v2;intMain () {//freopen ("In.txt", "R", stdin); intt=read (); for(intcas=1; cas<=t;cas++) {n=read (); for(intI=1; i<=n;i++) {p[i].x=read (), p[i].y=read (); P[i].y++; } sort (P+1, p+n+1, CMP); intflag=1; memset (Val1,0,4* (n+2)); memset (Val2,0,4* (n+2)); for(inti =1; I <= N; ++i) {Add (Val1,i,1); Add (Val2,i,1); } //return 0; for(intI=1; i<=n;i++) { if(p[i].y>n-i+1) {flag=0; Break; } intP1 =Rankget (VAL1,P[I].Y); intP2 = N-rankget (val2,p[i].y) +1; //cout << "I is" << i << "P1 are" << p1 << "P2 is" << p2 << Endl; //int P1=V1[P[I].Y],P2=V2[P[I].Y]; if(p1<p2) {ANS[P1]=p[i].x; Add (VAL1,P1,-1); Add (Val2,n-p1+1,-1); //cout << "Up delete" << p1 << "down delete" << n-p1 +1 << Endl; //v1.erase (V1.begin () +p[i].y); //v2.erase (V2.begin () +n-i+2-p[i].y); } Else{ANS[P2]=p[i].x; Add (VAL1,P2,-1); Add (Val2,n-p2+1,-1); //cout << "Up delete" << p2 << "down delete" << n-p2 +1 << Endl; //v2.erase (V2.begin () +p[i].y); //v1.erase (V1.begin () +n-i+2-p[i].y); } //cout << Endl;} printf ("Case #%d:", CAs); if(!flag) {printf ("impossible\n"); } Else { for(intI=1; i<=n;i++) printf ("%d", Ans[i]); printf ("\ n"); } }}
HDU 5493 Queue Tree Array