Robotic Sort
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3913 Accepted Submission(s): 1717
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.
The last scenario is followed by a line containing zero.
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.
3 4 5 1 6 2
4
3 3 2 1
0
4 2 4 4
//
// main.cpp
// hdu1890
//
// Created by Candy on 30/11/2016.
// Copyright © 2016 Candy. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
struct node{
int fa,ch[],w,size,flp;
}t[N];
int root;
inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;}
inline int wh(int x){return t[pa].ch[]==x;}
inline void pushDown(int x){
if(t[x].flp){
swap(lc,rc);
t[lc].flp^=;t[rc].flp^=;
t[x].flp=;
}
}
inline void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(g) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
update(f);update(x);
}
inline void splay(int x,int tar){
for(;t[x].fa!=tar;rotate(x))
if(t[pa].fa!=tar) rotate(wh(pa)==wh(x)?pa:x);
if(tar==) root=x;
}
int ne[N];
void spl(int x,int tar){
int _=x;
while(x!=tar) ne[pa]=x,x=pa;
x=_;
for(int i=tar;i!=x;i=ne[i]) pushDown(i);
pushDown(x);
splay(x,tar);
}
int build(int l,int r){
if(l>r) return ;
int x=(l+r)>>;
lc=build(l,x-);rc=build(x+,r);
t[lc].fa=t[rc].fa=x;
t[x].w=;t[x].flp=;
update(x);
//printf("build %d %d %d\n",x,lc,rc);
return x;
}
int kth(int k){
int x=root,ls=;
while(x!=){
pushDown(x);
int _=ls+t[lc].size;
if(_<k&&k<=_+t[x].w) return x;
if(k<=_) x=lc;
else ls=_+t[x].w,x=rc;
}
return -;
}
void rever(int l,int r){//printf("rev %d %d ",l,r);
splay(kth(l),);
int x=kth(r+);//printf("x %d\n",x);
splay(x,root);
t[lc].flp^=;
}
int n;
struct data{
int id,v;
bool operator <(const data &a)const{
if(v==a.v) return id<a.id;
return v<a.v;
}
}a[N];
int main(int argc, const char * argv[]){
while(scanf("%d",&n)!=EOF&&n){
memset(t,,sizeof(t));
for(int i=;i<=n;i++) a[i].v=read(),a[i].id=i+;
sort(a+,a++n);
root=build(,n+);
for(int i=;i<=n;i++){
int x=a[i].id;//printf("hi %d %d\n",i,x);
spl(x,);
printf("%d%c",t[lc].size,i<n?' ':'\n');
rever(i,t[lc].size);
}
}
return ;
}