题目大意:一串数字,使用如下方式排序: 先找到最小的数的位置$P_1$,将区间$[1,P_1]$反转,再找到第二小的数的位置$P_2$,将区间$[2,P_2]$反转,知道排序完成。输出每次操作的$P_i$,要求稳定排序(在括号外的是多组数据,括号内的是单组数据,四倍经验)

题解:可以用平衡数维护序列,只要维护求一个数的位置和区间翻转即可

卡点:查询位置时未$pushdown$

C++ Code:

  1. #include <cstdio>
  2. #include <algorithm>
  3. #define maxn 100010
  4. int n;
  5. int s[maxn], rnk[maxn], mp[maxn];
  6. inline bool cmp(int a, int b) {
  7. if (s[a] == s[b]) return a < b;
  8. return s[a] < s[b];
  9. }
  10. namespace Treap {
  11. int rc[maxn], lc[maxn], pri[maxn], V[maxn], sz[maxn];
  12. int fa[maxn], tg[maxn];
  13. int root, idx, ta, tb, tmp, res;
  14. int seed = 20040826;
  15. inline int rand() {return seed *= 48271;}
  16. inline int nw(int x) {
  17. V[++idx] = x;
  18. sz[idx] = 1;
  19. pri[idx] = rand();
  20. lc[idx] = rc[idx] = fa[idx] = tg[idx] = 0;
  21. return idx;
  22. }
  23. inline int update(int rt) {
  24. sz[rt] = sz[lc[rt]] + sz[rc[rt]] + 1;
  25. fa[lc[rt]] = fa[rc[rt]] = rt;
  26. return rt;
  27. }
  28. inline void pushdown(int rt) {
  29. if (tg[rt]) {
  30. tg[rt] = 0;
  31. std::swap(lc[rt], rc[rt]);
  32. tg[lc[rt]] ^= 1, tg[rc[rt]] ^= 1;
  33. }
  34. }
  35. void split(int rt, int k, int &x, int &y) {
  36. if (!rt) x = y = 0;
  37. else {
  38. pushdown(rt);
  39. if (sz[lc[rt]] < k) split(rc[rt], k - sz[lc[rt]] - 1, rc[rt], y), x = update(rt);
  40. else split(lc[rt], k, x, lc[rt]), y = update(rt);
  41. }
  42. }
  43. int merge(int x, int y) {
  44. if (!x || !y) return x | y;
  45. pushdown(x), pushdown(y);
  46. if (pri[x] < pri[y]) {rc[x] = merge(rc[x], y); return update(x);}
  47. else {lc[y] = merge(x, lc[y]); return update(y);}
  48. }
  49.  
  50. void debug(int rt) {
  51. if (lc[rt]) debug(lc[rt]);
  52. printf("%d\n", V[rt]);
  53. if (rc[rt]) debug(rc[rt]);
  54. }
  55.  
  56. inline void insert(int x) {
  57. if (!root) root = nw(x);
  58. else root = merge(root, nw(x));
  59. }
  60. int S[maxn], top;
  61. inline int gtrnk(int x) {
  62. top = 0;
  63. for (int i = x; i; i = fa[i]) S[++top] = i;
  64. for (; top; top--) pushdown(S[top]);
  65. res = sz[lc[x]] + 1;
  66. while (fa[x]) {
  67. if (rc[fa[x]] == x) res += sz[lc[fa[x]]] + 1;
  68. x = fa[x];
  69. }
  70. return res;
  71. }
  72. inline void reverse(int l, int r) {
  73. if (l > r) std::swap(l, r);
  74. split(root, r, tmp, tb);
  75. split(tmp, l - 1, ta, tmp);
  76. tg[tmp] ^= 1;
  77. root = merge(ta, merge(tmp, tb));
  78. }
  79. inline void clear() {
  80. idx = root = 0;
  81. }
  82. }
  83. int main() {
  84. scanf("%d", &n);
  85. while (true) {
  86. if (!n) break;
  87. for (int i = 1; i <= n; i++) {
  88. scanf("%d", s + i);
  89. rnk[i] = i;
  90. }
  91. std::sort(rnk + 1, rnk + n + 1, cmp);
  92. for (int i = 1; i <= n; i++) mp[rnk[i]] = i;
  93. for (int i = 1; i <= n; i++) Treap::insert(s[i]);
  94. for (int I = 1, i = rnk[I]; I <= n; i = rnk[++I]) {
  95. int res = Treap::gtrnk(i);
  96. printf("%d", res);
  97. putchar(I == n ? '\n' : ' ');
  98. Treap::reverse(I, res);
  99. }
  100. scanf("%d", &n);
  101. if (n) {
  102. Treap::clear();
  103. }
  104. }
  105. return 0;
  106. }

  

[UVA1402]Robotic Sort;[SP2059]CERC07S - Robotic Sort([洛谷P3165][CQOI2014]排序机械臂;[洛谷P4402][Cerc2007]robotic sort 机械排序)的更多相关文章

  1. 洛谷P3165 [CQOI2014]排序机械臂

    题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到摄低的物品的位置P1,并把左起第一个至P1间的物品反序:第二次找到第二低的物品 ...

  2. 洛谷P3165 [CQOI2014]排序机械臂 Splay维护区间最小值

    可以将高度定义为小数,这样就完美的解决了优先级的问题. Code: #include<cstdio> #include<algorithm> #include<cstri ...

  3. 洛谷 P4402 BZOJ1552 / 3506 [Cerc2007]robotic sort 机械排序

    FHQ_Treap 太神辣 蒟蒻初学FHQ_Treap,于是来到了这道略显板子的题目 因为Treap既满足BST的性质,又满足Heap的性质,所以,对于这道题目,我们可以将以往随机出的额外权值转化为每 ...

  4. [BZOJ1552][Cerc2007]robotic sort

    [BZOJ1552][Cerc2007]robotic sort 试题描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数 ...

  5. 【BZOJ-1552&3506】robotic sort&排序机械臂 Splay

    1552: [Cerc2007]robotic sort Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 806  Solved: 329[Submit][ ...

  6. 【BZOJ】【1552】【Cerc2007】robotic sort / 【3506】【CQOI2014】排序机械臂

    Splay 离散化+Splay维护序列…… 好吧主要说一下我做这道题遇到的几个错误点: 1.离散化 2.由于找到的这个数的位置一定是大于等于 i 的,所以其实在把它splay到根以后,i 结点只能sp ...

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

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

  8. 【BZOJ1552】[Cerc2007]robotic sort Splay

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

  9. bzoj 1552: [Cerc2007]robotic sort

    1552: [Cerc2007]robotic sort Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1198  Solved: 457[Submit] ...

随机推荐

  1. BC追踪

    项目又要开始改造了,记录一下改造过程中碰到的坑和解决思路,避免以后回头看看自己的笔记都不知道写了什么. (一)敏感信息混淆 (二)活用ComponentScan (三)Swagger配置多项目共用 ( ...

  2. Spring IoC的底层技术支持——Java反射机制

    我们知道,通过 new XmlClassPathApplicationContext("beans.xml")等方式即可启动容器.在容器启动时,Spring 根据配置文件的描述信息 ...

  3. hive自定义函数(UDF)

    首先什么是UDF,UDF的全称为user-defined function,用户定义函数,为什么有它的存在呢?有的时候 你要写的查询无法轻松地使用Hive提供的内置函数来表示,通过写UDF,Hive就 ...

  4. PLC编码规范

    PC在编码规范方面比PLC要好很多.既然它们都是编程语言,那么PC方面的规范是否可以用与PLC呢?答案是肯定的,但需要作取舍.下面规范中的大部分可以用于一般PLC,其中有些只是针对西门子博途,使用时需 ...

  5. 015---Django的forms组件

    Django form表单   Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用 ...

  6. HyperLedger Fabric 1.4 交易流程(6.3)

    区块链最主要的特性之一是去中心化,没有了中心机构的集中处理,为了达成数据的一致性,就需要网络中全民参与管理,并以某种方法达成共识,所以区块链的交易流程也就是共识的过程.       在Fabric中, ...

  7. Spring MVC怎么统一异常管理?

    1. 在类上加上@ControllerAdvice注解 2. 在方法上加上@ExceptionHandler注解 @ExceptionHandler(Exception.class) @Respons ...

  8. Java 中编码与摘要算法

    URL 编码与解码 String s = "你好,世界!"; // URL 编码 String urlEncodedString = URLEncoder.encode(s, &q ...

  9. ORB-SLAM 代码笔记(三)tracking原理

    ORB视觉里程计主体在tracking线程中

  10. Hibernate-ORM:08.Hibernate中的投影查询

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将叙述hibernate中的投影查询 一,目录: 1.解释什么是投影查询 2.返回Object单个对象 ...