HDU 1890——Robotic Sort(伸展树)

Robotic Sort

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


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. 

HDU 1890——Robotic Sort(伸展树)_第1张图片

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
 


————————————————————————分割线————————————————————

题目大意:

对你一个序列,每次将第i个位置到第i大的数所在位置 之间的数进行翻转,输出第i大的数所在的位置


思路:

对序列离散化为1~n,然后用个数组存储权值为i,在伸展树中的节点号为rt,mp[x]=rt;

从小到大将权值为i(第i大),节点编号为mp[i]旋转到根,那么这个数在序列中的位置就是i+size[ch[root][0]],然后标记rev


注意点:

不用新建边界点,否则在翻转操作会把边界点也一起翻转,导致错误

写得蛋碎,自己太弱了....


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#define REP(i,n) for(int i=0;i<(n);++i)
#define Key_value ch[ch[root][1]][0]
#define lson ch[rt][0]
#define rson ch[rt][1]
const int maxn=100010;
using namespace std;
int n;
struct node {
    int x;
    int id;
    bool operator<(const node&cmp) const{
        return ((x<cmp.x)||(x==cmp.x&&id<cmp.id));
    }
} a[maxn];
struct SplayTree{
    int root,tot;
    int ch[maxn][2],pre[maxn],rev[maxn],size[maxn];
    int dis[maxn],mp[maxn],key[maxn];
    void update_rev(int rt) {
        if(!rt) return;
        swap(lson,rson);
        rev[rt]^=1;
    }
    void push_up(int rt) {
        size[rt]=size[lson]+size[rson]+1;
    }
    void push_down(int rt) {
        if(rev[rt]) {
            update_rev(lson);
            update_rev(rson);
            rev[rt]=0;
        }
    }
    void NewNode(int &rt,int f) {
        rt=++tot;
        lson=rson=0;
        pre[rt]=f;
        size[rt]=1;
        rev[rt]=0;
    }
    void build(int &rt,int l,int r,int f) {
        if(l>r) return ;
        int m=(l+r)>>1;
//        cout<<dis[m]<<endl;
        NewNode(rt,f);
        mp[dis[m]]=rt;
        build(lson,l,m-1,rt);
        build(rson,m+1,r,rt);
        push_up(rt);
    }
    void Init() {
        root=tot=0;
        ch[0][0]=ch[0][1]=pre[0]=size[0]=rev[0]=0;
        NewNode(root,0);
        NewNode(ch[root][1],root);
        REP(i,n) {
            scanf("%d",&a[i].x);a[i].id=i;
        }
        sort(a,a+n);
        REP(i,n) dis[a[i].id]=i;
        mp[dis[0]]=1;//注意这两个操作,没有新建边界端点;如果没有,那么翻转的时候会把边界端点也翻转,导致错误!!!
        mp[dis[n-1]]=2;//
        build(Key_value,1,n-2,ch[root][1]);
        push_up(ch[root][1]);
        push_up(root);
    }
    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);
    }
    void Splay(int x,int goal) {
        push_down(x);
        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);
                }
            }
        }
        push_up(x);
        if(goal==0) root=x;
    }
    void RotateTo(int k,int goal) {
        int rt=root;
        push_down(rt);
        while(size[lson]+1!=k) {
            if(size[lson]>=k) rt=lson;
            else {
                k-=(size[lson]+1);
                rt=rson;
            }
            push_down(rt);
        }
        Splay(rt,goal);
    }
    int Get_kth(int rt,int k) {
        push_down(rt);
        int t=size[lson]+1;
        if(t==k) return rt;
        if(t>k) return Get_kth(lson,k);
        else return Get_kth(rson,k-t);
    }
    void del_root() {
        if(ch[root][1]) {
            int t=root;
            root=ch[root][1];
            //
            RotateTo(1,0);
            ch[root][0]=ch[t][0];
            if(ch[t][0]) pre[ch[t][0]]=root;
        } else root=ch[root][0];

        pre[root]=0;
        push_up(root);
    }
    void solve() {
        REP(i,n) {
            Splay(mp[i],0);
            if(i>0) printf(" ");
            printf("%d",size[ch[root][0]]+i+1);
            update_rev(ch[root][0]);
            del_root();
//            cnt=0;InOrder(root);puts("");
        }
        puts("");
    }
}spt;
int main()
{
    while(scanf("%d",&n),n) {
        if(n==1) {scanf("%*d");printf("1\n");continue;}
        spt.Init();
        spt.solve();
    }
    return 0;
}




你可能感兴趣的:(HDU 1890——Robotic Sort(伸展树))