HDU 1890 Robotic Sort | Splay
Robotic Sort
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
The last scenario is followed by a line containing zero.
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 Output】
【题意】
机械臂可以翻转整个数列中连续的某一串序列,要求用这种翻转的方式完成整个数列的排序。
【分析】
排序的方式题目已经给出了,只需要按照要求去模拟即可,而模拟这个过程的方法很重要,时间上还是要求比较严格的。
获取数据之后首先排序一遍,记录下排完序之后每个点原本的位置,这个就是等一下在树中进行操作时需要用到的了。
题目要求的翻转方式是每次刚好将第i个翻转到位,每次翻转之后固定一个数,然后翻转下一个数,直到翻转完全部的数。
只要用splay将每次的目标结点移到根,那么其左子树的大小就是数列中排在它左边的点的个数,也就是题目要求输出的部分。然后翻转整个左子树(将子树的左右儿子交换,递归到底),删除根,就完成了一次操作。
思路很清晰,但是这里遇到的问题是如何翻转左子树,如果每次都手动递归向下翻转所有树的话,时间上必然是要超时的。从线段树中启发,可以用标记的方式标记翻转情况,减少直接操作的次数,用传递标记的方式传下去:
输出结果后,将左子树打上翻转标记,然后删除根。
一个结点翻转标记的传递必须要发生在对其子树进行操作之前,本题中,后续直接操作子树的过程是接下来的删根操作(我的代码中要从左右子树中找前后继)和下一轮将下一个目标移到根的过程。所以要在这两个过程前加入维护的过程。
删根中找前后继因为是从上往下的,刚好可以顺便查看标记情况对子树完成翻转。问题是下一次提升结点到根的时候不能确定上方父节点的标记情况,因此只能从当前结点开始向上查到根,然后从根开始向下回到目标结点,一路查看标记完成翻转维护......这一块我只能想出这个方法了,不知道其他大神有没有什么更好的方法。这里用递归反查结果栈溢出了,只好开了个大数组记录,,,最后提交AC的通过时间也比较长,应该确实是还有更好的方法吧
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU1890
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef struct nod
{
int key,num;
} node; #define MAXN 100010 node a[MAXN];
int list[MAXN]; bool op(node a,node b)
{
if (a.key==b.key) return a.num<b.num;
else return a.key<b.key;
} int sons[MAXN][];
int father[MAXN],size[MAXN],data[MAXN];
bool flag[MAXN];
int spt=,spttail=; void rotate(int x,int w) //rotate(node,0/1)
{
int y=father[x];
sons[y][-w]=sons[x][w];
if (sons[x][w]) father[sons[x][w]]=y; father[x]=father[y];
if (father[y])
if (y==sons[father[y]][]) sons[father[y]][]=x;
else sons[father[y]][]=x; sons[x][w]=y;
father[y]=x; size[x]=size[y];
size[y]=size[sons[y][]]+size[sons[y][]]+;
} void splay(int x,int y) //splay(node,position)
{
if (!x) return ;
//check(x);
while(father[x]!=y)
{
if (father[father[x]]==y)
if (x==sons[father[x]][]) rotate(x,);
else rotate(x,);
else
if (father[x]==sons[father[father[x]]][])
if (x==sons[father[x]][])
{
rotate(father[x],);
rotate(x,);
} else
{
rotate(x,);
rotate(x,);
}
else
if (x==sons[father[x]][])
{
rotate(father[x],);
rotate(x,);
} else
{
rotate(x,);
rotate(x,);
}
}
if (!y) spt=x;
} void insert(int w) //insert(value)
{
spttail++;
data[spttail]=w;
size[spttail]=;
sons[spttail][]=;
sons[spttail][]=;
flag[spttail]=;
if (!spt)
{
father[spttail]=;
spt=spttail;
} else
{
int x=spt;
while()
{
size[x]++;
if (w<data[x])
if (sons[x][]) x=sons[x][];
else break;
else
if (sons[x][]) x=sons[x][];
else break;
}
father[spttail]=x;
if (w<data[x]) sons[x][]=spttail;
else sons[x][]=spttail;
splay(spttail,);
}
} void down(int x)
{
flag[x]=;
int t=sons[x][];
sons[x][]=sons[x][];
sons[x][]=t;
flag[sons[x][]]=-flag[sons[x][]];
flag[sons[x][]]=-flag[sons[x][]];
} void del(int x) //del(number)
{
splay(x,); int y=sons[x][];
if (flag[y]) down(y);
while(sons[y][])
{
y=sons[y][];
if (flag[y]) down(y);
} int z=sons[x][];
if (flag[z]) down(z);
while(sons[z][])
{
z=sons[z][];
if (flag[z]) down(z);
} if (y)
{
splay(y,);
size[y]--;
if (z)
{
splay(z,y);
sons[z][]=;
size[z]--;
} else sons[y][]=;
} else
{
if (z)
{
splay(z,);
sons[z][]=;
size[z]--;
} else
{
spt=;
spttail=;
}
}
} void check(int x)
{
//if (father[x]) check(father[x]);
//if (flag[x]) down(x);
int i=;
while (father[x])
{
i++;
list[i]=x;
x=father[x];
}
while (i>)
{
if (flag[list[i]]) down(list[i]);
i--;
}
} int main()
{
//freopen("1.txt","r",stdin); int n;
scanf("%d",&n);
while(n)
{
spt=;
spttail=;
for (int i=;i<=n;i++)
{
scanf("%d",&a[i].key);
a[i].num=i;
insert(i);
}
sort(&a[],&a[n+],op); for (int i=;i<=n;i++)
{
check(a[i].num);
splay(a[i].num,);
if (i>) printf(" ");
printf("%d",size[sons[spt][]]+i); flag[sons[spt][]]=-flag[sons[spt][]]; del(spt);
} printf("\n");
scanf("%d",&n);
} return ;
}
HDU 1890 Robotic Sort | Splay的更多相关文章
- hdu 1890 Robotic Sort(splay 区间反转+删点)
题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...
- HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...
- 数据结构(Splay平衡树):HDU 1890 Robotic Sort
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1890 Robotic Sort (splay tree)
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1890 Robotic Sort(splay)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...
- hdu 1890 Robotic Sort
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 如下: #include<cstdio> #include<cstdlib&g ...
- hdu 1890 Robotic SortI(splay区间旋转操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...
- HDU1890 Robotic Sort[splay 序列]
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- BZOJ 1552: [Cerc2007]robotic sort( splay )
kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...
随机推荐
- 单片机上使用TEA加密通信(转)
源:单片机上使用TEA加密通信 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:MDK4.72 单片机:STM32 说 ...
- Spring框架_代理模式(静态代理,动态代理,cglib代理)
共性问题: 1. 服务器启动报错,什么原因? * jar包缺少.jar包冲突 1) 先检查项目中是否缺少jar包引用 2) 服务器: 检查jar包有没有发布到服务器下: ...
- 第四十二节,configparser特定格式的ini配置文件模块
configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 特定格式的ini配置文件模块,用于处理ini配置文件,注意:这个ini配置文件,只是ini文件名称的文本文件,不是后 ...
- java多维数组
int a[][][] = {{{1,2},{1,2}},{{1,2},{1,2}}}; int b[][][] = new int[][][]{{{1,2},{1,2}},{{1,2},{1,2}} ...
- hdu_2446_Shell Pyramid(数学,二分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2446 题意:题面很大,有用的就那么几句,意思就是用自然数来堆它画的那个金字塔,比如第一个金字塔的第一个 ...
- 谈CSS模块化【封装-继承-多态】
第一次听到“CSS模块化”这个词是在WebReBuild的第四届“重构人生”年会上,当时我还想,“哈,CSS也有模块化,我没听错吧?”事实上,我没听错,你也没看错,早就有CSS模块化这个概念了.之所以 ...
- textarea内容太多, 鼠标点击全部显示
strRight+="<td bordercolor='#DEDEDE' width='500px' height='50'><div title='"+data ...
- chrome浏览器调试功能之后端篇
作为后端开发人员,可能有很多同学不怎么了解chrome调试功能,而即将成为大神的我们,怎么也得会,知其然更要知其所以然,今天我带领大家好好的梳理一下,chrome浏览器调试,个人把它分成了前端功能和后 ...
- LVS负载均衡中arp_ignore和arp_annonuce参数配置的含义
先简单的介绍下关于LVS负载均衡 LVS(Linux Virtual Server)Linux服务器集群系统 针对高可伸缩,高可用服务的需求,给予IP层和内容请求分发的负载均衡调度解决方法,并在Li ...
- weblogic 密码加密
先启动域: