HDU 1890:Robotic Sort Splay

1 篇文章 0 订阅

Robotic Sort

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


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

题意是给出了一序列的数,然后每一次都要把最小的那个数挪到前面去,相应地要把区间中的数全部旋转,然后每一次输出最小数的那个位置。

作为菜鸟表示Splay真的好神奇,在纸上划了划发现区间旋转那块,直接就对应着树的旋转,感觉好炫酷。。。然后自己也是看了网上很多Splay树的模板,树这块的题自己做得很少,看懂了旋转的原理之后越看越神奇。。。

感觉这道题真真是作为入门题了,也不需要多说了。感觉后面还有好多好玩的题目。。。后面再补充自己的理解吧,当前的理解还是太肤浅了。。。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;

#define INF 0x3fffffff
typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1000005;

#define Key_value ch[ch[root][1]][0]

int root, tot;
int pre[maxn], ch[maxn][2], siz[maxn], rev[maxn];

struct no
{
    int val, id;
}node[maxn];

bool cmp(no n1, no n2)
{
    if (n1.val == n2.val)
    {
        return n1.id < n2.id;
    }
    else
    {
        return n1.val < n2.val;
    }
}

inline void pushup(int x)
{
    siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
}

inline void pushdown(int x)
{
    if (rev[x])
    {
        rev[x] = 0;
        rev[ch[x][0]] ^= 1;
        rev[ch[x][1]] ^= 1;
        swap(ch[x][0], ch[x][1]);
    }
}

inline void rotate(int x,int c)//旋转,c=0左旋,c=1右旋
{
    int y = pre[x];
    pushdown(y);
    pushdown(x);

    ch[y][!c] = ch[x][c];
    if (ch[x][c])pre[ch[x][c]] = y;
    pre[x] = pre[y];
    if (pre[y])ch[pre[y]][ch[pre[y]][1] == y] = x;
    ch[x][c] = y;
    pre[y] = x;
    pushup(y);
}

inline void Splay(int x, int fa)//把节点x转到节点fa的下面
{
    pushdown(x);
    while (pre[x] != fa)
    {
        int y = pre[x], z = pre[y];
        pushdown(z);//旋转之前去除反转标记
        pushdown(y);
        pushdown(x);

        if (pre[pre[x]] == fa)
        {
            rotate(x, ch[pre[x]][0] == x);
        }
        else
        {
            if (ch[z][0] == y)
            {
                if (ch[y][0] == x)
                    rotate(y, 1), rotate(x, 1);
                else
                    rotate(x, 0), rotate(x, 1);
            }
            else
            {
                if (ch[y][0] == x)
                    rotate(x, 1), rotate(x, 0);
                else
                    rotate(y, 0), rotate(x, 0);
            }
        }
    }
    pushup(x);
    if (fa == 0)
        root = x;
}

inline void select(int k, int fa)//把第k个节点旋转到fa的下面
{
    int x = root;
    while (true)
    {
        pushdown(x);
        if (k == siz[ch[x][0]] + 1)
            break;
        else if (k <= siz[ch[x][0]])
        {
            x = ch[x][0];
        }
        else
        {
            k -= (siz[ch[x][0]] + 1);
            x = ch[x][1];
        }
    }
    Splay(x, fa);
}

inline void del_root()//删除根节点
{
    int t = root;
    if (ch[root][1])
    {
        root = ch[root][1];
        select(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;
    pushup(root);
}

void Newnode(int &x, int fa,int k)
{
    x = k;
    pre[x] = fa;
    ch[x][1] = ch[x][0] = 0;
    rev[x] = 0;
    siz[x] = 1;
}

void build(int &x, int le, int ri, int fa)
{
    if (le > ri)return;
    int mid = (le + ri) >> 1;
    Newnode(x, fa, mid);
    build(ch[x][0], le, mid - 1, x);
    build(ch[x][1], mid + 1, ri, x);
    pushup(x);
}

void init(int n)
{
    int i;
    pre[0] = ch[0][0] = ch[0][1] = 0;
    siz[0] = rev[0] = tot = 0;
    
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &node[i].val);
        node[i].id = i;
    }
    sort(node + 1, node + n + 1, cmp);
    Newnode(root, 0, 1);
    Newnode(ch[root][1], root, n);
    build(Key_value, 2, n - 1, ch[root][1]);
    pushup(ch[root][1]);
    pushup(root);
}

void solve(int n)
{
    int i;
    for (i = 1; i <= n; i++)
    {
        int v = node[i].id;
        Splay(v, 0);
        printf("%d", i + siz[ch[root][0]]);
        if (i < n)printf(" ");
        rev[ch[root][0]] ^= 1;
        del_root();
    }
    printf("\n");
}
//得到第k个结点(需要push_down)
inline int Get_kth(int r, int k)
{
    pushdown(r);
    int t = siz[ch[r][0]] + 1;
    if (t == k)return r;
    if (t > k)return Get_kth(ch[r][0], k);
    else return Get_kth(ch[r][1], k - t);
}
//找前驱(需要push_down)
inline int Get_pre(int r)
{
    pushdown(r);
    if (ch[r][0] == 0)return -1;//不存在
    r = ch[r][0];
    while (ch[r][1])
    {
        r = ch[r][1];
        pushdown(r);
    }
    return r;
}
//找后继(需要push_down)
inline int Get_next(int r)
{
    pushdown(r);
    if (ch[r][1] == 0)return -1;
    r = ch[r][1];
    while (ch[r][0])
    {
        r = ch[r][0];
        pushdown(r);
    }
    return r;
}
int main()
{
    //freopen("i.txt","r",stdin);
    //freopen("o.txt","w",stdout);

    int t;
    while (scanf("%d", &t) != EOF)
    {
        if (t == 0)
            break;
        if (t == 1)
        {
            scanf("%d", &t);
            printf("1\n");
            continue;
        }
        init(t);
        solve(t);
    }
    //system("pause");
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值