HDU 1890 Splay interval flip, hdu1890splay Interval

Source: Internet
Author: User

HDU 1890 Splay interval flip, hdu1890splay Interval

D-Robotic Sort Time Limit:2000 MS Memory Limit:32768KB 64bit IO Format:% I64d & % I64uSubmit Status Practice HDU 1890 Appoint description: System Crawler)

Description

Somewhere 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 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.

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.

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 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 
 

Sort the intervals with flip, and create an splay as 1, 2 ,... n, create A struct array A [maxn], first stores the key, and second stores the subscript in the initial array. Sort the keys in order, then A [I]. second is the number of the I-th element in splay. Every time you rotate it to the heel, I is the position of the element that is smaller than him, after the output, delete the root of the tree (because this part is already ordered and not deleted will affect the subsequent operations ). It is best to flip two layers at a time. At first I flipped only one layer and it timed out.

Code:

/*************************************************************************    > File Name: Spaly.cpp    > Author: acvcla    > QQ:    > Mail: acvcla@gmail.com    > Created Time: 2014Äê11ÔÂ16ÈÕ ÐÇÆÚÈÕ 00ʱ14·Ö26Ãë ************************************************************************/#include<iostream>#include<algorithm>#include<cstdio>#include<vector>#include<cstring>#include<map>#include<queue>#include<stack>#include<string>#include<cstdlib>#include<ctime>#include<set>#include<math.h>using namespace std;typedef long long LL;const int maxn = 1e5 + 100;#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define pb push_backint ch[maxn][2],pre[maxn],siz[maxn],rev[maxn];int root,tot;struct  node{int first,second;bool operator < (const node &a)const{if(first==a.first)return second<a.second;return first<a.first;}};node A[maxn];inline void newnode(int &x,int fa,int id){x=id;siz[x]=1;pre[x]=fa;ch[x][1]=ch[x][0]=0;}void update_rev(int r)  {      if(!r)return;      swap(ch[r][0],ch[r][1]);      rev[r]^=1;  }void push_down(int r)  {      if(rev[r])      {          update_rev(ch[r][0]);          update_rev(ch[r][1]);          rev[r]=0;      }  }inline void push_up(int x){siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;}inline 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;ch[x][kind]=y;if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;pre[x]=pre[y];pre[y]=x;push_up(y);push_up(x);}inline void Splay(int x,int goal){while(pre[x]!=goal){if(pre[pre[x]]==goal)Rotate(x,ch[pre[x]][0]==x);else{int y=pre[x];int kind=(ch[pre[y]][0]==y);if(ch[y][kind]==x){Rotate(x,!kind);Rotate(x,kind);}else{Rotate(y,kind);Rotate(x,kind);}}}if(goal==0)root=x;}int Get_max(int x){push_down(x);while(ch[x][1]){x=ch[x][1];push_down(x);}return x;}inline void remove_root(){push_down(root);if(!ch[root][0]){root=ch[root][1];}else{int t=Get_max(ch[root][0]);Splay(t,root);ch[t][1]=ch[root][1];pre[ch[root][1]]=t;root=t;}pre[root]=0;push_up(root);}inline void built(int &x,int L,int R,int fa){if(L>R)return;int mid=(R+L)>>1;newnode(x,fa,mid);built(ch[x][0],L,mid-1,x);built(ch[x][1],mid+1,R,x);push_up(x);}inline void init(int n){pre[0]=siz[0]=0;root=tot=ch[0][0]=ch[0][1]=0;for(int i=1;i<=n;i++){scanf("%d",&A[i].first);A[i].second=i;}sort(A+1,A+1+n);built(root,1,n,0);}int main(int argc, char const *argv[]){int n;while(~scanf("%d",&n)&&n){init(n);rep(i,1,n-1){Splay(A[i].second,0);update_rev(ch[root][0]);printf("%d ",i+siz[ch[root][0]]);remove_root();}printf("%d\n",n);}return 0;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.