Robotic Sort hdu1890 (伸展树翻转+删根)

本文讨论了在实验室环境中使用机器人手臂对不同高度的材料样本进行排序的方法。通过一系列逆序操作,实现了样本的高效排序,确保了后续处理单元的顺利运行。详细介绍了排序算法的具体步骤和实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Robotic Sort

Time Limit:2000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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 题意很明确,就是要你每次找最小数字位置,翻转整个区间,达到排序的目的,相同的数字按下标小的优先。 分析:伸展树的区间翻转操作和删除根节点操作,每次将最小的那个数的编号放到根节点,那么根节点的左儿子的siz+1就是当前的位置,然后在左树递归 查找到一个根节点的可替代节点,替换当前的根节点即可。 注意在splay时先判断父节点是否可翻转在执行当前旋转操作,不然会出错。 源代码在加入了inline之后活生生少了100+ms,不可思议
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f
#define maxn 100005
#define keytree (ch[ch[root][1]][0])
int e[maxn],tmp[maxn];
int siz[maxn],ch[maxn][2],rev[maxn],pre[maxn],id[maxn];
int root,top;

bool cmp(int a,int b)
{
    if(tmp[a]!=tmp[b])
        return tmp[a]<tmp[b];
    return a<b;
}

inline void newnode(int &x,int p)
{
    x=++top;
    siz[x]=1;
    ch[x][0]=ch[x][1]=0;
    pre[x]=p;
    rev[x]=0;
}

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

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

inline void build(int &x,int l,int r,int p)
{
    int mid=(l+r)>>1;
    newnode(x,p);
    id[mid]=top;
    if(l<mid) build(ch[x][0],l,mid-1,x);
    if(r>mid) build(ch[x][1],mid+1,r,x);
    pushup(x);
}

inline void init(int n)
{
    root=1;
    top=0;
    siz[0]=ch[0][0]=ch[0][1]=pre[0]=rev[0]=0;
    build(ch[0][1],0,n-1,0);
}

inline void Rotate(int x,int kind)
{
    int y=pre[x];
    pushdown(y);
    pushdown(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;
    pushup(y);
}

inline void splay(int x,int goal)
{
    while(pre[x]!=goal)
    {
        if(pre[pre[x]]==goal)
        {
            pushdown(pre[x]);
            pushdown(x);
            Rotate(x,ch[pre[x]][0]==x);
        }
        else
        {
            pushdown(pre[pre[x]]);
            pushdown(pre[x]);
            pushdown(x);
            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);
            }
        }
    }
    pushup(x);
    if(goal==0) root=x;
}

inline void find_left(int x)
{
    pushdown(x);
    siz[x]--;
    if(ch[x][1]==0)
    {
        int kind=ch[pre[x]][1]==x;
        pre[ch[x][0]]=pre[x];
        ch[pre[x]][kind]=ch[x][0];
        ch[x][0]=ch[root][0];
        ch[x][1]=ch[root][1];
        pre[ch[x][0]]=pre[ch[x][1]]=x;
        pre[x]=0;
        root=x;
        pushup(x);
    }
    else find_left(ch[x][1]);
}

inline int update(int r)
{
    splay(r,0);
    int ans=siz[ch[r][0]]+1;
    rev[ch[r][0]]^=1;
    if(ch[r][0]==0)
    {
        ch[0][1]=ch[r][1];
        pre[ch[r][1]]=0;
    }
    else find_left(ch[r][0]);
    return ans;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=0; i<n; i++) scanf("%d",&tmp[i]),e[i]=i;
        init(n);
        sort(e,e+n,cmp);
        for(int i=0; i<n; i++)
        {
            int r=id[e[i]];
            int ans;
            ans=update(r);
            printf("%d%c",ans+i,i==n-1?'\n':' ');
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值