您的位置:首页 > 其它

HDU1890 Robotic Sort(slpay,区间旋转)

2017-07-28 21:38 363 查看


Robotic Sort

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4395    Accepted Submission(s): 1966


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

 

Source

2008 “Shun
Yu Cup” Zhejiang Collegiate Programming Contest - Warm Up(2) 

 

Recommend

linle

 

题意:给一个数列,通过旋转区间的方式使其有序,具体的方法是,每一次找到需要旋转的那个数,旋转到根节点,删除根节点。而每一次旋转之前的位置则是在旋转的数左边的数的个数加上已经删除的数的个数。

代码还是抄kuangbin巨巨的,不会自己打splay哇。

/*
* HDU 1890 Robotic Sort
* 这题就是对 n个不同的数的排序过程,通过旋转将第i到的数放在正确的位置,题目就是输出每次旋转前第i大的数的位置。
* 这题的意思就是直接按照结点编号1-n建立一颗树。每个数代表初始位置的那个点,记录下每个数对应的点。
* 每一次就是把第i大的数旋转到根结点。删除这个点然后旋转左区间。
* 其实也可以用区间模拟,不删除的做法。
* 操作有:反转、删除根结点
* 需要的变量:pre,ch,size,rev;
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=100010;
int pre[MAXN],ch[MAXN][2],size[MAXN],rev[MAXN];
int root,tot1;
int n;
void NewNode(int &r,int father,int k)
{
r=k;
pre[r]=father;
ch[r][0]=ch[r][1]=0;
size[r]=1;
rev[r]=0;
}
//反转的更新
void Update_Rev(int r)
{
if(!r)return;
swap(ch[r][0],ch[r][1]);
rev[r]^=1;
}
void Push_Up(int r)
{
size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
}
void Push_Down(int r)
{
if(rev[r])
{
Update_Rev(ch[r][0]);
Update_Rev(ch[r][1]);
rev[r]=0;
}
}
void Build(int &x,int l,int r,int father)
{
if(l>r)return;
int mid=(l+r)/2;
NewNode(x,father,mid);
Build(ch[x][0],l,mid-1,x);
Build(ch[x][1],mid+1,r,x);
Push_Up(x);//这个不用忘记
}
void Init()
{
root=tot1=0;
ch[root][0]=ch[root][1]=size[root]=rev[root]=0;
Build(root,1,n,0);
}
//旋转,基本固定
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);
}
//Splay调整
void Splay(int r,int goal)
{
Push_Down(r);
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
{
//这题有反转操作,需要先push_down,在判断左右孩子
Push_Down(pre[r]);
Push_Down(r);
Rotate(r,ch[pre[r]][0]==r);
}

else
{
//这题有反转操作,需要先push_down,在判断左右孩子
Push_Down(pre[pre[r]]);
Push_Down(pre[r]);
Push_Down(r);
int y=pre[r];
int kind=(ch[pre[y]][0]==y);
//两个方向不同,则先左旋再右旋
if(ch[y][kind]==r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
//两个方向相同,相同方向连续两次
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
Push_Up(r);
if(goal==0)root=r;
}
int Get_Min(int r)
{
Push_Down(r);
while(ch[r][0])
{
r=ch[r][0];
Push_Down(r);
}
return r;
}
int Get_Max(int r)
{
Push_Down(r);
while(ch[r][1])
{
r=ch[r][1];
Push_Down(r);
}
return r;
}
//删除根结点
void Remove()
{
if(ch[root][0]==0)//没有左孩子
{
root=ch[root][1];
pre[root]=0;
}
else
{
int m=Get_Max(ch[root][0]);
Splay(m,root);
ch[m][1]=ch[root][1];
pre[ch[root][1]]=m;
root=m;
pre[root]=0;
Push_Up(root);//要更新
}
}
struct Node
{
int id,num;
}a[MAXN];
bool cmp(Node n1,Node n2)
{
if(n1.num!=n2.num)return n1.num<n2.num;
else return n1.id<n2.id;
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
Init();
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].num);
a[i].id=i;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<n;i++)
{
Splay(a[i].id,0);
Update_Rev(ch[root][0]);
printf("%d ",i+size[ch[root][0]]);
Remove();
}
printf("%d\n",n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: