码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构(Splay平衡树):HDU 1890 Robotic Sort

时间:2016-04-04 21:00:14      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

Robotic Sort

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3456    Accepted Submission(s): 1493


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

 

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 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
 
  这道题很简单,注意Push_down就好了。
  1 #include <algorithm>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <cstdio>
  5 using namespace std;
  6 const int maxn=100010;
  7 int n,fa[maxn],ch[maxn][2],sz[maxn];
  8 int flip[maxn],pos[maxn],rt;
  9 struct Node{
 10     int x,id;
 11 }a[maxn];
 12 
 13 void Flip(int x){
 14     swap(ch[x][0],ch[x][1]);
 15     flip[x]^=1;
 16 }
 17 
 18 void Push_down(int x){
 19     if(flip[x]){
 20         Flip(ch[x][0]);
 21         Flip(ch[x][1]);
 22         flip[x]=0;
 23     }
 24 }
 25 
 26 int pd[maxn];
 27 void P(int x){
 28     int cnt=0;
 29     while(x){
 30         pd[++cnt]=x;
 31         x=fa[x];
 32     }
 33     while(cnt){
 34         Push_down(pd[cnt--]);
 35     }
 36 }
 37 
 38 void Push_up(int x){
 39     sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
 40 }
 41 
 42 void Rotate(int x){
 43     int y=fa[x],g=fa[y],c=ch[y][1]==x;
 44     ch[y][c]=ch[x][c^1];fa[ch[x][c^1]]=y;
 45     ch[x][c^1]=y;fa[y]=x;fa[x]=g;
 46     if(g)ch[g][ch[g][1]==y]=x;
 47     Push_up(y);
 48 }
 49 
 50 void Splay(int x,int g=0){
 51     P(x);
 52     for(int y;(y=fa[x])!=g;Rotate(x))
 53         if(fa[y]!=g)
 54             Rotate((ch[fa[y]][1]==y)==(ch[y][1]==x)?y:x);
 55     Push_up(x);        
 56     if(!g)rt=x;
 57 }
 58 
 59 int Build(int f,int l,int r){
 60     if(l>r)return 0;
 61     int mid=(l+r)>>1;fa[mid]=f;
 62     ch[mid][0]=Build(mid,l,mid-1);
 63     ch[mid][1]=Build(mid,mid+1,r);
 64     sz[mid]=1;
 65     Push_up(mid);
 66     return mid;
 67 }
 68 
 69 bool cmp(Node a,Node b){
 70     if(a.x!=b.x)
 71     return a.x<b.x;
 72     return a.id<b.id;
 73 }
 74 
 75 int main(){
 76     while(~scanf("%d",&n)&&n){
 77         memset(flip,0,sizeof(flip));
 78         rt=Build(0,1,n+2);
 79         for(int i=1;i<=n;i++)
 80             scanf("%d",&a[i].x);
 81         for(int i=1;i<=n;i++)
 82             a[i].id=i;    
 83         sort(a+1,a+n+1,cmp);
 84         for(int i=1;i<=n;i++)
 85             pos[i+1]=a[i].id+1;    
 86         pos[1]=1;pos[n+2]=n+2;
 87         for(int i=2,p;i<n+1;i++){
 88             Splay(pos[1]);
 89             Splay(pos[i],pos[1]);
 90             printf("%d ",sz[ch[ch[rt][1]][0]]+1);
 91             Splay(pos[i]);
 92             p=ch[pos[i]][1];
 93             while(ch[p][0]){
 94                 Push_down(p);
 95                 p=ch[p][0];
 96             }
 97             Push_down(p);
 98             Splay(pos[i-1]);
 99             Splay(p,pos[i-1]);
100             Flip(ch[ch[rt][1]][0]);
101         }
102         printf("%d\n",n);
103     }
104     return 0;
105 }

 

 

数据结构(Splay平衡树):HDU 1890 Robotic Sort

标签:

原文地址:http://www.cnblogs.com/TenderRun/p/5352761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!