Robotic Sort

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

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

题意:n个试管,每次找最短的位置p[i]输出,翻转i~p[i]

没想到写了好长时间
对序列的理解:
1.build之后数组下标就是序列编号,中序遍历就是序列
2.这样的splay并没有左小右大的关系,因为有反转操作
3.加哨兵之后,翻转序列[l,r],就是处理kth(l)和kth(r+2)
 
一开始想了很多应该处理哪个序列,都错了
其实题目已经很清楚了,就是翻转i~p[i]
处理第i短的编号为x,x就是序列编号就是数组下标,把x splay到根然后p[i]=左子树大小+1(就是t[lc].size 因为1是哨兵)
翻转操作就是rever(i,t[lc].size);
 
关于标记下传:
1.翻转操作先要kth,在kth中下传了就没必要在splay中下传了
2.否则的话先从x找到tar,然后从tar往x下传标记,最后splay(x,tar)
 
//
// main.cpp
// hdu1890
//
// Created by Candy on 30/11/2016.
// Copyright © 2016 Candy. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
struct node{
int fa,ch[],w,size,flp;
}t[N];
int root;
inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;}
inline int wh(int x){return t[pa].ch[]==x;}
inline void pushDown(int x){
if(t[x].flp){
swap(lc,rc);
t[lc].flp^=;t[rc].flp^=;
t[x].flp=;
}
}
inline void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(g) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
update(f);update(x);
}
inline void splay(int x,int tar){
for(;t[x].fa!=tar;rotate(x))
if(t[pa].fa!=tar) rotate(wh(pa)==wh(x)?pa:x);
if(tar==) root=x;
}
int ne[N];
void spl(int x,int tar){
int _=x;
while(x!=tar) ne[pa]=x,x=pa;
x=_;
for(int i=tar;i!=x;i=ne[i]) pushDown(i);
pushDown(x);
splay(x,tar);
}
int build(int l,int r){
if(l>r) return ;
int x=(l+r)>>;
lc=build(l,x-);rc=build(x+,r);
t[lc].fa=t[rc].fa=x;
t[x].w=;t[x].flp=;
update(x);
//printf("build %d %d %d\n",x,lc,rc);
return x;
}
int kth(int k){
int x=root,ls=;
while(x!=){
pushDown(x);
int _=ls+t[lc].size;
if(_<k&&k<=_+t[x].w) return x;
if(k<=_) x=lc;
else ls=_+t[x].w,x=rc;
}
return -;
}
void rever(int l,int r){//printf("rev %d %d ",l,r);
splay(kth(l),);
int x=kth(r+);//printf("x %d\n",x);
splay(x,root);
t[lc].flp^=;
}
int n;
struct data{
int id,v;
bool operator <(const data &a)const{
if(v==a.v) return id<a.id;
return v<a.v;
}
}a[N];
int main(int argc, const char * argv[]){
while(scanf("%d",&n)!=EOF&&n){
memset(t,,sizeof(t));
for(int i=;i<=n;i++) a[i].v=read(),a[i].id=i+;
sort(a+,a++n);
root=build(,n+);
for(int i=;i<=n;i++){
int x=a[i].id;//printf("hi %d %d\n",i,x);
spl(x,);
printf("%d%c",t[lc].size,i<n?' ':'\n');
rever(i,t[lc].size);
}
}
return ;
}
 
 
 

HDU1890 Robotic Sort[splay 序列]的更多相关文章

  1. hdu1890 Robotic Sort (splay+区间翻转单点更新)

    Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratori ...

  2. HDU1890 Robotic Sort Splay tree反转,删除

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...

  3. HDU 1890 Robotic Sort | Splay

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

  4. BZOJ 1552: [Cerc2007]robotic sort( splay )

    kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...

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

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

  6. 【BZOJ1552】[Cerc2007]robotic sort Splay

    [BZOJ1552][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N ...

  7. 【bzoj1552/3506】[Cerc2007]robotic sort splay翻转,区间最值

    [bzoj1552/3506][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000. ...

  8. [BZOJ1552] [Cerc2007] robotic sort (splay)

    Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. Output ...

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

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

随机推荐

  1. 数据结构:链表(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Minion Xu class LinkedListUnderflow(ValueErro ...

  2. Castle Windsor常用介绍以及其在ABP项目的应用介绍

    最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载 ...

  3. AutoMapper之ABP项目中的使用介绍

    最近在研究ABP项目,昨天写了Castle Windsor常用介绍以及其在ABP项目的应用介绍 欢迎各位拍砖,有关ABP的介绍请看阳光铭睿 博客 AutoMapper只要用来数据转换,在园里已经有很多 ...

  4. IE8,IE10下载的临时文件到哪里去了???

    操作攻略: 打开IE浏览器=>工具=>Internet选项=>常规选项卡中,找到"浏览历史记录"=>设置,然后就可看到"当前位置"所列出 ...

  5. 向ES6靠齐的Class.js

    写在前面 在2008年的时候,John Resig写了一 Class.js,使用的方式如下: var Person = Class.extend({ init: function(isDancing) ...

  6. jQuery静态方法isFunction,isArray,isWindow,isNumeric使用和源码分析

    上一篇随笔中总结了js数据类型检测的几个方法和jQuery的工具方法type方法,本篇要分析几个方法都依赖type方法,所以不了解type方法的请先参看http://www.cnblogs.com/y ...

  7. iOS 开发技术牛人博客

    dark_gmn 的博客   http://blog.csdn.net/dark_gmn?viewmode=contents Tel_小超 的博客  http://blog.csdn.net/qq_2 ...

  8. Android开发过程遇到的问题小计

    1.在真机上正常运行,而模拟器会报出一些so文件找不到 unexpected e_machine: 40. 解决方法:采用x86的NDK进行编译,问题解决.

  9. 谈谈AppDelegate

    谈谈AppDelegate 前言 每个iOS程序都会有一个AppDelegate的类,这个类就是一个代理类,我们新建一个Project的时候,里面都会带有这个类.现在就让我们看看这个类. 开始介绍 对 ...

  10. Mac使用极简教程

    最近领导让我写一篇关于Mac的使用教程,因为使用人群未知,所以尽量写的通俗易懂,可谓是关于Mac电脑使用的精简教程吧,在此发表出来以供参考. Mac因为安全性而闻名,我们拥有了一部Mac,那么我们来了 ...