伸展树 - hdu1890 Robotic Sort

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=1890


题意:

某台机器要排序一个序列,然而这台机器只能实现一种操作,将序列中某段区间完全翻转,某君提出了个思路,对长度为n的序列,机器只要翻转n次,第i次找到[i,n]中最小的数x的位置k,然后翻转[i,k]即可(想不明白为什么这么翻就可以的请看样例去),求输出每次翻转前的序列中,x的位置


思路:

在学伸展树,啊不得不说学会一种新数据结构的心情真是爽爆了,做这题前我拿poj3468先练了练手(那是道线段树加懒标记的经典问题,然而也可以用伸展树搞),伸展树在处理这种区间翻转的序列问题真是得心应手

用数组下标建splay树,当我们需要翻转[i,k]区间时,先将i-1结点旋转为根节点,然后将k+1结点旋转为i-1结点的右儿子,这时[i,k]区间就是i-1结点的左子树(这种处理方法非常非常经典,想不明白自己画个图看看,论文里面也有),此时对i-1结点的左子树打上翻转标记即可

本题的详细算法为,每次找到x,将x旋转至根节点的右儿子的左儿子(我没打错!看看上一段),此时x的左子树的结点数即为序列翻转前x前的数字数量,然后给x的左子树打上翻转标记,删去x结点,合并x的左右子树,循环执行这一步骤即可

为了方便处理,在序列的前后各加一个结点


代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

const int MAXSIZE = 1e5+100;

struct Node{
    int key, sz;
    Node *ch[2], *pnt;
    bool rever;

    inline void push_up(){
        sz = 1;
        if (ch[0]) sz += ch[0]->sz;
        if (ch[1]) sz += ch[1]->sz;
    }

    inline void push_down(){
        if (!rever) return;
        swap(ch[0],ch[1]);
        if (ch[0]) ch[0]->rever = !ch[0]->rever;
        if (ch[1]) ch[1]->rever = !ch[1]->rever;
        rever = false;
    }
};
Node *root;

struct node{
    int id, num;
};
bool operator <(const node&p1, const node&p2){
	if (p1.num!=p2.num) return p1.num<p2.num;
	else return p1.id<p2.id;
}
node arr[MAXSIZE];

void init(){
    root = NULL;
}

void Treaval(Node *x) {
    if(x) {
        Treaval(x->ch[0]);
        cout<<"key: "<<x->key
            <<" sz: "<<x->sz
            <<" reverse: "<<x->rever;

        if (x->ch[0]) cout<<" lchild: "<<x->ch[0]->key;
            else cout<<" lchild: null";
        if (x->ch[1]) cout<<" rchild: "<<x->ch[1]->key;
            else cout<<" rchild: null";
        if (x->pnt) cout<<" pnt: "<<x->pnt->key;
            else cout<<" pnt: null";

        cout<<endl;
        Treaval(x->ch[1]);
    }
}
void debug() {printf("root: %d\n",root->key);Treaval(root);}

void srotate(Node *x, bool d){
    Node *y = x->pnt;

    y->ch[!d] = x->ch[d];
    if (x->ch[d] != NULL)
        x->ch[d]->pnt = y;
    x->pnt = y->pnt;
    if (y->pnt != NULL){
        if (y == y->pnt->ch[d])
            y->pnt->ch[d] = x;
        else
            y->pnt->ch[!d] = x;
    }
    x->ch[d] = y;
    y->pnt = x;
    y->push_up();
    x->push_up();
}

void splay(Node *x, Node *target){
    Node *y;
    while (x->pnt != target){
        y = x->pnt;

        if (y->pnt) y->pnt->push_down();
        y->push_down();
        x->push_down();

        if (x == y->ch[0]){
            if (y->pnt != target && y == y->pnt->ch[0])
                srotate(y, true);
            srotate(x, true);
        }
        else{
            if (y->pnt != target && y == y->pnt->ch[1])
                srotate(y, false);
            srotate(x, false);
        }
    }
    if (target == NULL)
        root = x;
}

Node* sinsert(int key){//插入一个值
    if (root == NULL){
        root = new Node;
        root->ch[0] = root->ch[1] = root->pnt = NULL;
        root->rever = false;
        root->key = key;
        root->sz = 1;
        return root;
    }
    Node *x = root, *y;
    while (1){
        x->sz++;
        if (key < x->key){
            if (x->ch[0] != NULL)
                x = x->ch[0];
            else{
                x->ch[0] = new Node;
                y = x->ch[0];
                y->key = key;
                y->sz = 1;
                y->ch[0] = y->ch[1] = NULL;
                y->rever = false;
                y->pnt = x;
                break;
            }
        }
        else{
            if (x->ch[1] != NULL)
                x = x->ch[1];
            else{
                x->ch[1] = new Node;
                y = x->ch[1];
                y->key = key;
                y->sz = 1;
                y->ch[0] = y->ch[1] = NULL;
                y->rever = false;
                y->pnt = x;
                break;
            }
        }
    }
    splay(y, NULL);
    return y;
}

Node* searchMinimum(Node *x){
    Node *y = x->pnt;
    if (x) x->push_down();
    while (x->ch[0]){
        x = x->ch[0];
        if (x) x->push_down();
    }
    splay(x,y);
    return x;
}

Node* searchMaximum(Node *x){
    Node *y = x->pnt;
    while (x->ch[1])
        x = x->ch[1];
    splay(x,y);
    return x;
}

//执行join时,需要保证x子树的任一结点均小于y中任一结点
Node* join(Node *s1, Node *s2){
    if (!s1) return s2;
    s1->rever = !s1->rever;
    if (!s2) return s1;
    Node *t = searchMinimum(s2);
    t->ch[0] = s1;
    s1->pnt = t;
    t->push_up();
    return t;
}

void del(Node *x){
    if (x->ch[0]) x->ch[0]->pnt = NULL;
    if (x->ch[1]) x->ch[1]->pnt = NULL;
    root = join(x->ch[0],x->ch[1]);
    delete x;
}

Node* arrmap[MAXSIZE];

int main(){
    int n;
    while (scanf("%d",&n),n!=0){
        init();
        for (int i=0;i<n;++i){
            scanf("%d",&arr[i].num);
            arr[i].id = i+1;
        }
        sort(arr,arr+n);
        for (int i=0;i<n;++i){
            //cout<<arr[i].id<<" "<<arr[i].num<<endl;
            arrmap[i] = sinsert(arr[i].id);
        }

        //debug();
        for (int i=0;i<n-1;++i){
            int ans = i + 1;
            arrmap[i]->push_down();
            splay(arrmap[i],NULL);
            if (root->ch[0]) ans += root->ch[0]->sz;
            //debug();

            printf("%d ",ans);
            del(root);
            //debug();
        }
        printf("%d\n",n);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值