Robotic sort
Time Limit: 6000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1102 accepted submission (s): 451
Problem descriptionsomewhere deep in the Czech Technical University buildings, there are manuatories for examining Mechanical and Electrical Properties of various materials. in one of yesterday's presentations, you have seen how was one of the manuatories changed into a new
Multimedia lab. But there are still others, serving to their original purposes.
In this task, you are writing software for a robot that handles samples in such a laboratory. imagine there are material samples lined up on a running belt. the samples have different heights, which may cause troubles to the next processing unit. to eliminate
Such troubles, we need to sort the samples by their height into the ascending order.
Reordering is done by a mechanical robot arm, which is able to pick up any number of consecutive samples and turn them round, such that their mutual order is reversed. in other words, one robot operation can reverse the order of samples on positions
A and B.
A possible way to sort the samples is to find the position of the smallest one (P1) and reverse the order between positions 1 and P1, which causes the smallest sample to become first. then we find the second one on position P and reverse the order between 2
And P2. then the third sample is located etc.
The picture shows a simple example of 6 samples. the smallest one is on the 4th position, therefore, the robot arm reverses the first 4 samples. the second smallest sample is the last one, so the next robot operation will reverse the order of five samples on
Positions 2-6. The third step will be to reverse the samples 3-4, etc.
Your task is to find the correct sequence of reversal operations that will sort the samples using the above algorithm. if there are more samples with the same height, their mutual order must be preserved: the one that was given first in the initial order must
Be placed before the others in the final order too.
Inputthe input consists of several scenarios. each scenario is described by two lines. the first line contains one integer number N, the number of samples, 1 ≤ n ≤ 100 000. the second line lists EXACTLY n space-separated positive integers, they specify the heights
Of individual samples and their initial order.
The last scenario is followed by a line containing zero.
Outputfor each scenario, output one line with EXACTLY n integers P1, P1,... Pn, separated by a space.
Each PI must be an integer (1 ≤ PI ≤ n) giving the position of the I-th sample just before the I-th reversal operation.
Note that if a sample is already on its correct position Pi, you shocould output the number pi anyway, indicating that the "interval between PI and PI" (a single sample) shocould be reversed.
Sample Input
63 4 5 1 6 243 3 2 10
Sample output
4 6 4 5 6 64 2 4 4
Source2008 "Shun
Yu Cup "Zhejiang Collegiate Programming Contest-warm up (2)
Recommendlinle question: http://acm.hdu.edu.cn/showproblem.php? PID = 1890 question: This is the method used to simulate the continuous sorting. Output The position analysis of each keyword: When the interval rotation is involved, all of them can be directly simulated using splay, each time you rotate the node corresponding to the keyword to the root, the number of left sons is its position, and then flip the corresponding range .... Code:
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int mm=111111;struct data{ int a,p,id;}g[mm];bool cmp(data u,data v){ return u.a<v.a||(u.a==v.a&&u.p<v.p);}struct SplayTree{ int son[mm][2],far[mm],num[mm],turn[mm]; int rt,size; void Link(int x,int y,int c) { far[x]=y,son[y][c]=x; } void Rotate(int x,int c) { int y=far[x]; PushDown(y); PushDown(x); Link(x,far[y],son[far[y]][1]==y); Link(son[x][!c],y,c); Link(y,x,!c); PushUp(y); } void Splay(int x,int g) { for(PushDown(x);far[x]!=g;) { int y=far[x],cx=son[y][1]==x,cy=son[far[y]][1]==y; if(far[y]==g)Rotate(x,cx); else { if(cx==cy)Rotate(y,cy); else Rotate(x,cx); Rotate(x,cy); } } PushUp(x); if(!g)rt=x; } int Select(int k,int g) { int x=rt; PushDown(x); while(num[son[x][0]]!=k) { if(num[son[x][0]]>k)x=son[x][0]; else k-=num[son[x][0]]+1,x=son[x][1]; PushDown(x); } Splay(x,g); return x; } void NewNode(int y,int &x,int a) { x=++size; far[x]=y,num[x]=1,g[a].id=x; turn[x]=son[x][0]=son[x][1]=0; } void Make(int l,int r,int &x,int y) { if(l>r)return; int m=(l+r)>>1; NewNode(y,x,m); Make(l,m-1,son[x][0],x); Make(m+1,r,son[x][1],x); PushUp(x); } void Prepare(int n) { NewNode(size=0,rt,0); NewNode(rt,son[rt][1],0); Make(1,n,son[2][0],2); Splay(3,0); } void PushUp(int x) { num[x]=1+num[son[x][0]]+num[son[x][1]]; } void Reverse(int x) { turn[x]^=1; swap(son[x][0],son[x][1]); } void PushDown(int x) { if(turn[x]) { Reverse(son[x][0]); Reverse(son[x][1]); turn[x]=0; } } int Work(int a) { int x=g[a].id,ret; Splay(x,0); ret=num[son[rt][0]]; Select(a-1,0); int y=Select(ret+1,rt); Reverse(son[y][0]); return ret; }}spt;int i,n;int main(){ while(scanf("%d",&n),n) { for(i=1;i<=n;++i) scanf("%d",&g[i].a),g[i].p=i; spt.Prepare(n); sort(g+1,g+n+1,cmp); for(i=1;i<=n;++i) printf("%d%c",spt.Work(i),i<n?' ':'\n'); } return 0;}