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)
 
  1. //
  2. // main.cpp
  3. // hdu1890
  4. //
  5. // Created by Candy on 30/11/2016.
  6. // Copyright © 2016 Candy. All rights reserved.
  7. //
  8. #include <iostream>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <algorithm>
  12. #include <cmath>
  13. using namespace std;
  14. #define pa t[x].fa
  15. #define lc t[x].ch[0]
  16. #define rc t[x].ch[1]
  17. const int N=1e5+;
  18. inline int read(){
  19. char c=getchar();int x=,f=;
  20. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  21. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  22. return x*f;
  23. }
  24. struct node{
  25. int fa,ch[],w,size,flp;
  26. }t[N];
  27. int root;
  28. inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;}
  29. inline int wh(int x){return t[pa].ch[]==x;}
  30. inline void pushDown(int x){
  31. if(t[x].flp){
  32. swap(lc,rc);
  33. t[lc].flp^=;t[rc].flp^=;
  34. t[x].flp=;
  35. }
  36. }
  37. inline void rotate(int x){
  38. int f=t[x].fa,g=t[f].fa,c=wh(x);
  39. if(g) t[g].ch[wh(f)]=x;t[x].fa=g;
  40. t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
  41. t[x].ch[c^]=f;t[f].fa=x;
  42. update(f);update(x);
  43. }
  44. inline void splay(int x,int tar){
  45. for(;t[x].fa!=tar;rotate(x))
  46. if(t[pa].fa!=tar) rotate(wh(pa)==wh(x)?pa:x);
  47. if(tar==) root=x;
  48. }
  49. int ne[N];
  50. void spl(int x,int tar){
  51. int _=x;
  52. while(x!=tar) ne[pa]=x,x=pa;
  53. x=_;
  54. for(int i=tar;i!=x;i=ne[i]) pushDown(i);
  55. pushDown(x);
  56. splay(x,tar);
  57. }
  58. int build(int l,int r){
  59. if(l>r) return ;
  60. int x=(l+r)>>;
  61. lc=build(l,x-);rc=build(x+,r);
  62. t[lc].fa=t[rc].fa=x;
  63. t[x].w=;t[x].flp=;
  64. update(x);
  65. //printf("build %d %d %d\n",x,lc,rc);
  66. return x;
  67. }
  68. int kth(int k){
  69. int x=root,ls=;
  70. while(x!=){
  71. pushDown(x);
  72. int _=ls+t[lc].size;
  73. if(_<k&&k<=_+t[x].w) return x;
  74. if(k<=_) x=lc;
  75. else ls=_+t[x].w,x=rc;
  76. }
  77. return -;
  78. }
  79. void rever(int l,int r){//printf("rev %d %d ",l,r);
  80. splay(kth(l),);
  81. int x=kth(r+);//printf("x %d\n",x);
  82. splay(x,root);
  83. t[lc].flp^=;
  84. }
  85. int n;
  86. struct data{
  87. int id,v;
  88. bool operator <(const data &a)const{
  89. if(v==a.v) return id<a.id;
  90. return v<a.v;
  91. }
  92. }a[N];
  93. int main(int argc, const char * argv[]){
  94. while(scanf("%d",&n)!=EOF&&n){
  95. memset(t,,sizeof(t));
  96. for(int i=;i<=n;i++) a[i].v=read(),a[i].id=i+;
  97. sort(a+,a++n);
  98. root=build(,n+);
  99. for(int i=;i<=n;i++){
  100. int x=a[i].id;//printf("hi %d %d\n",i,x);
  101. spl(x,);
  102. printf("%d%c",t[lc].size,i<n?' ':'\n');
  103. rever(i,t[lc].size);
  104. }
  105. }
  106. return ;
  107. }
 
 
 

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. Entity Framework 数据库先行、模型先行、代码先行

    数据库先行(Database First):基于已存在的数据库,利用某些工具(如Vs提供的EF设计器)创建实体类,数据库对象与实体类的匹配关系等,你也可以手动修改这些自动生成的代码及匹配文件. 模型先 ...

  2. bzoj1202--带权并查集+前缀和

    http://www.lydsy.com/JudgeOnline/problem.php?id=1202 记s[i]=a[1]+a[2]+...+a[i],即s[i]为前缀和.再令v[i]=s[f[i ...

  3. 新建 ASP.NET Core Web API 项目 -- RESTFul 风格 Hello World!

    一.创建一个空项目 请查看 新建 .NET Core 项目 -- Hello World! 一节,新建一个项目:    二.添加引用并修改配置为 Web API (.NET Core 已将 MVC/W ...

  4. 工行ATM转账——事务操作

    今儿去工行ATM给已朋友转账,遇到这么个情况: 选择对外转账后输入转入账号(输入两次),接着提示输入转入金额(输入一次金额),按确定,系统提示交易中,3秒左右,提示“输入账号无效”,系统自动中断了操作 ...

  5. html5手机端遮罩弹出菜单代码

    效果体验:http://hovertree.com/texiao/html5/17/ 效果图: 代码如下: <!doctype html> <html lang="zh&q ...

  6. javascript中concat方法深入理解

    最近在恶补js知识的时候,总是会因为js强大的语法而感到震撼.因为以前对前端方面的疏忽,导致了一些理解的错误.因此痛改前非,下定决心,不管做什么事情,都要有专研的精神. 在介绍前,抛出一个问题:如何将 ...

  7. Struts2(1) —— 概述

    1.Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发,框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2.Struts2框架的流程原理 ...

  8. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(四)地图导航控件模块

    config.xml文件的配置如下: <widget left="10" top="50" config="widgets/Navigation ...

  9. IOS开发基础知识--碎片45

    1:iOS SEL的简单总结 SEL就是对方法的一种包装.包装的SEL类型数据它对应相应的方法地址,找到方法地址就可以调用方法 a.方法的存储位置 在内存中每个类的方法都存储在类对象中 每个方法都有一 ...

  10. Thrift-java学习小结

    ➠更多技术干货请戳:听云博客 Thrift是什么?什么情况下使用thrift Thrift源于大名鼎鼎的facebook之手,在2007年facebook提交Apache基金会将Thrift作为一个开 ...