Robotic Sort
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Description
Somewhere deep in the Czech Technical University buildings, there are laboratories for examining mechanical and electrical properties of various materials. In one of yesterday’s presentations, you have seen how was one of the laboratories changed into a new multimedia lab. But there are still others, serving to their original purposes.
In this task, you are to write 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 between 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.
In this task, you are to write 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 between 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.
![](https://i-blog.csdnimg.cn/blog_migrate/c52bd44ce803cbe3b0cb329ca6c76697.jpeg)
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.
Input
The 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.
The last scenario is followed by a line containing zero.
Output
For 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 should output the number Pi anyway, indicating that the “interval between Pi and Pi ” (a single sample) should be reversed.
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 should output the number Pi anyway, indicating that the “interval between Pi and Pi ” (a single sample) should be reversed.
Sample Input
6
3 4 5 1 6 2
4
3 3 2 1
0
Sample Output
4 6 4 5 6 6
4 2 4 4
题意:给你一种排序方式,对于第i次操作,输出第i大的数的位置,并将第i为何第i大的区间内的数反转。
分析:说是Splay的入门题,我还是觉得不简单,初次学,几乎都是套版的。
主要的思想是这样:每进行操作后,前面的数就没用了,就删除了。而Splay里面保存的是原数组数的下标,那么此时第i大的位置在哪里呢?我们把第i大的数对应原数组的下标在Splay中旋转到根节点,那么它的左子树就是下标比它小的数,再加上之前删除的i个数,所以就输出i+sz[ch[i.id]][0]就行了。而需要进行的reverse操作其实就是根节点左子树这个区间,添加一个rev标记,在更新时传递下去就行了。
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const int MAXN=100010; int pre[MAXN],ch[MAXN][2],sz[MAXN],rev[MAXN]; int root,tot1; int n; void NewNode(int &r,int father,int k) { r=k; pre[r]=father; ch[r][0]=ch[r][1]=0; sz[r]=1; rev[r]=0; } //反转的更新 void Update_Rev(int r) { if(!r)return; swap(ch[r][0],ch[r][1]); rev[r]^=1; } void Push_Up(int r) { sz[r]=sz[ch[r][0]]+sz[ch[r][1]]+1; } void Push_Down(int r) { if(rev[r]) { Update_Rev(ch[r][0]); Update_Rev(ch[r][1]); rev[r]=0; } } void Build(int &x,int l,int r,int father) { if(l>r)return; int mid=(l+r)/2; NewNode(x,father,mid); Build(ch[x][0],l,mid-1,x); Build(ch[x][1],mid+1,r,x); Push_Up(x);//这个不用忘记 } void Init() { root=tot1=0; ch[root][0]=ch[root][1]=sz[root]=rev[root]=0; Build(root,1,n,0); } //旋转,基本固定 void Rotate(int x,int kind) { int y=pre[x]; Push_Down(y); Push_Down(x); ch[y][!kind]=ch[x][kind]; pre[ch[x][kind]]=y; if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x; pre[x]=pre[y]; ch[x][kind]=y; pre[y]=x; Push_Up(y); } //Splay调整 void Splay(int r,int goal) { Push_Down(r); while(pre[r]!=goal) { if(pre[pre[r]]==goal) { //这题有反转操作,需要先push_down,在判断左右孩子 Push_Down(pre[r]); Push_Down(r); Rotate(r,ch[pre[r]][0]==r); } else { //这题有反转操作,需要先push_down,在判断左右孩子 Push_Down(pre[pre[r]]); Push_Down(pre[r]); Push_Down(r); int y=pre[r]; int kind=(ch[pre[y]][0]==y); //两个方向不同,则先左旋再右旋 if(ch[y][kind]==r) { Rotate(r,!kind); Rotate(r,kind); } //两个方向相同,相同方向连续两次 else { Rotate(y,kind); Rotate(r,kind); } } } Push_Up(r); if(goal==0)root=r; } int Get_Min(int r) { Push_Down(r); while(ch[r][0]) { r=ch[r][0]; Push_Down(r); } return r; } int Get_Max(int r) { Push_Down(r); while(ch[r][1]) { r=ch[r][1]; Push_Down(r); } return r; } //删除根结点 void Remove() { if(ch[root][0]==0)//没有左孩子 { root=ch[root][1]; pre[root]=0; } else { int m=Get_Max(ch[root][0]); Splay(m,root); ch[m][1]=ch[root][1]; pre[ch[root][1]]=m; root=m; pre[root]=0; Push_Up(root);//要更新 } } struct Node { int id,num; }a[MAXN]; bool cmp(Node n1,Node n2) { if(n1.num!=n2.num)return n1.num<n2.num; else return n1.id<n2.id; } int main() { while(scanf("%d",&n)==1&&n) { Init(); for(int i=1;i<=n;i++) { scanf("%d",&a[i].num); a[i].id=i; } sort(a+1,a+1+n,cmp); for(int i=1;i<n;i++) { Splay(a[i].id,0); printf("%d ",i+sz[ch[root][0]]); Update_Rev(ch[root][0]); Remove(); } printf("%d\n",n); } return 0; }