Robotic Sort

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

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
 
  这道题很简单,注意Push_down就好了。
 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int n,fa[maxn],ch[maxn][],sz[maxn];
int flip[maxn],pos[maxn],rt;
struct Node{
int x,id;
}a[maxn]; void Flip(int x){
swap(ch[x][],ch[x][]);
flip[x]^=;
} void Push_down(int x){
if(flip[x]){
Flip(ch[x][]);
Flip(ch[x][]);
flip[x]=;
}
} int pd[maxn];
void P(int x){
int cnt=;
while(x){
pd[++cnt]=x;
x=fa[x];
}
while(cnt){
Push_down(pd[cnt--]);
}
} void Push_up(int x){
sz[x]=sz[ch[x][]]+sz[ch[x][]]+;
} void Rotate(int x){
int y=fa[x],g=fa[y],c=ch[y][]==x;
ch[y][c]=ch[x][c^];fa[ch[x][c^]]=y;
ch[x][c^]=y;fa[y]=x;fa[x]=g;
if(g)ch[g][ch[g][]==y]=x;
Push_up(y);
} void Splay(int x,int g=){
P(x);
for(int y;(y=fa[x])!=g;Rotate(x))
if(fa[y]!=g)
Rotate((ch[fa[y]][]==y)==(ch[y][]==x)?y:x);
Push_up(x);
if(!g)rt=x;
} int Build(int f,int l,int r){
if(l>r)return ;
int mid=(l+r)>>;fa[mid]=f;
ch[mid][]=Build(mid,l,mid-);
ch[mid][]=Build(mid,mid+,r);
sz[mid]=;
Push_up(mid);
return mid;
} bool cmp(Node a,Node b){
if(a.x!=b.x)
return a.x<b.x;
return a.id<b.id;
} int main(){
while(~scanf("%d",&n)&&n){
memset(flip,,sizeof(flip));
rt=Build(,,n+);
for(int i=;i<=n;i++)
scanf("%d",&a[i].x);
for(int i=;i<=n;i++)
a[i].id=i;
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)
pos[i+]=a[i].id+;
pos[]=;pos[n+]=n+;
for(int i=,p;i<n+;i++){
Splay(pos[]);
Splay(pos[i],pos[]);
printf("%d ",sz[ch[ch[rt][]][]]+);
Splay(pos[i]);
p=ch[pos[i]][];
while(ch[p][]){
Push_down(p);
p=ch[p][];
}
Push_down(p);
Splay(pos[i-]);
Splay(p,pos[i-]);
Flip(ch[ch[rt][]][]);
}
printf("%d\n",n);
}
return ;
}
 

数据结构(Splay平衡树):HDU 1890 Robotic Sort的更多相关文章

  1. hdu 1890 Robotic Sort(splay 区间反转+删点)

    题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...

  2. HDU 1890 Robotic Sort | Splay

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Pr ...

  3. HDU 1890 Robotic Sort (splay tree)

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. HDU 1890 Robotic Sort(splay)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...

  5. HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...

  6. hdu 1890 Robotic Sort

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 如下: #include<cstdio> #include<cstdlib&g ...

  7. hdu 1890 Robotic SortI(splay区间旋转操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...

  8. 【HDOJ】1890 Robotic Sort

    伸展树伤不起啊,很容易wa,很容易T,很容易M. /* 1890 */ #include <iostream> #include <string> #include <m ...

  9. 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列

    339. [NOI2005] 维护数列 时间限制:3 s   内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际 ...

随机推荐

  1. js 高阶函数 filter

    filter用于过滤array中的一些值,通过带入的函数返回的ture 或false 保留或去除,返回一个新的array filter 使用演示:判断筛选出array中的素数: //判断素数自定义函数 ...

  2. 利用反射把数据集合转换成List

    ---ResultSet数据集 public static List toList(ResultSet rs, Class cls) { List list = new ArrayList(); tr ...

  3. SQL语句之三简单增删改查

    这是前面建的库和表 USE Test go INSERT dbo.MyTable --插入数据         ( NAME ,age) VALUES  ( '数据,20  -- NAME - var ...

  4. express 安装与卸载

    卸载: npm uninstall -g express安装指定版本: npm install -g express@3.5.0查看版本: express -V注意express -V中的V要大写,不 ...

  5. FlightGear 视角控制

    Flightgear提供了非常灵活的模块化功能 这里就简要记录一下视角切换功能 首先,需要了解一下Flightgear中的property tree的主要内容,这里暂略. http://wiki.fl ...

  6. 344. Reverse String(C++)

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

  7. Vijos1865 NOI2014 魔法森林 LCT维护生成树

    基本思路: 首先按照weightA升序排序,然后依次在图中加边,并维护起点到终点路径上weightB的最大值 如果加边过程中生成了环,则删除环中weightB最大的边 由于是无向图,点之间没有拓扑序, ...

  8. Codeforces 474E - Pillars

    一眼看上去非常像最长不下降子序列. 然后比赛的时候对每个答案长度为k的序列,维护最后一个数的最大值和最小值. 当时不知道为什么认为从长度最长倒推至前面不会太长,于是心满意足地敲了个O(n^2).结果T ...

  9. 工厂方法模式(Factory Method)

    1.本质:延迟到子类来选择实现 2.示意图: 3.主要功能: 让父类在不知道具体实现的情况下,完成自身功能的调用 类似于注入 4.备注: 1.工厂方法中,通常父类是一个抽象类,里面包含创建对象的抽象工 ...

  10. 复制JAVABEAN中的属性到另外一个JAVABEAN中

    下午写了一个属性复制方法,记录如下: class POUtil{ /** * * Function : 将一个source中的属性到复制到dest * @author : Liaokailin * C ...