Splay hdu1890 Robotic Sort
来源:互联网 发布:js date 设置日期 编辑:程序博客网 时间:2024/06/06 11:49
传送门:点击打开链接
题意:一种排序,找到最小的,然后讲整段翻转,把数字提前。
思路:看到旋转就应该想到用splay。问题是怎么知道第几大的数字当前是第几个。
这里有种方法,就是记录每个数字的节点编号,然后只要把这个节点Splay到根节点,那么size[son[root][0]]就是数字所在的位置了。
有个需要注意的地方,就是翻转的时候,一定要这样写,不然会出各种问题,上一道Splay题都没注意到这些细节侥幸过了,诶。。
#include<map>#include<set>#include<cmath>#include<ctime>#include<stack>#include<queue>#include<cstdio>#include<cctype>#include<string>#include<vector>#include<cstring>#include<iomanip>#include<iostream>#include<algorithm>#include<functional>#define fuck(x) cout<<"["<<x<<"]"#define FIN freopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w+",stdout)using namespace std;typedef long long LL;typedef pair<int, int>PII;const int MX = 1e5 + 5;const int mod = 1e9 + 7;const int INF = 0x3f3f3f3f;int size[MX], num[MX], col[MX], n;int son[MX][2], fa[MX], root, sz;struct Data{ int num,pos,id; bool operator<(const Data &P) const{ if(num == P.num) return pos < P.pos; return num < P.num; }}A[MX];void Link(int x, int y, int c) { fa[x] = y; son[y][c] = x;}void push_up(int rt) { size[rt] = size[son[rt][0]] + size[son[rt][1]] + 1;}void Reverse(int rt){ col[rt] ^= 1; swap(son[rt][0], son[rt][1]);}void push_down(int rt) { if(col[rt]) { Reverse(son[rt][0]); Reverse(son[rt][1]); col[rt] = 0; }}void Rotate(int x, int c) { int y = fa[x]; push_down(y); push_down(x); Link(x, fa[y], son[fa[y]][1] == y); Link(son[x][!c], y, c); Link(y, x, !c); push_up(y);}void Splay(int x, int g) { push_down(x); while(fa[x] != g) { int y = fa[x], cx = son[y][1] == x, cy = son[fa[y]][1] == y; if(fa[y] == g) Rotate(x, cx); else { if(cx == cy) Rotate(y, cy); else Rotate(x, cx); Rotate(x, cy); } } push_up(x); if(!g) root = x;}void NewNode(int f, int &rt) { rt = ++sz; fa[rt] = f, size[rt] = 1; son[rt][0] = son[rt][1] = col[rt] = 0;}int Select(int k, int g) { int rt = root; while(size[son[rt][0]] != k) { if(size[son[rt][0]] > k) rt = son[rt][0]; else k -= size[son[rt][0]] + 1, rt = son[rt][1]; push_down(rt); } Splay(rt, g); return rt;}void Build(int l, int r, int &rt, int f) { if(l > r) return; int m = (l + r) >> 1, t; NewNode(f, rt); Build(l, m - 1, son[rt][0], rt); scanf("%d", &num[rt]); A[m].pos = m; A[m].num = num[rt]; A[m].id = rt; Build(m + 1, r, son[rt][1], rt); push_up(rt);}void Prepare(int n) { sz = 0; NewNode(0, root); num[1] = 0; NewNode(root, son[root][1]); num[2] = 0; Build(1, n, son[2][0], 2); sort(A + 1, A + n + 1); Splay(3, 0);}void Flip(int l, int r) { Select(l - 1, 0); Select(r + 1, root); Reverse(son[son[root][1]][0]);}int main() { //FIN; while(~scanf("%d", &n), n) { Prepare(n); for(int i = 1; i <= n; i++) { Splay(A[i].id, 0); int p = size[son[root][0]]; Flip(i, p); printf("%d%c", p, i == n ? '\n' : ' '); } } return 0;}
0 0
- hdu1890 Robotic Sort(Splay)
- Splay hdu1890 Robotic Sort
- HDU1890 Robotic Sort(Splay)
- [hdu1890 Robotic Sort]Splay Tree
- hdu1890 Robotic Sort splay+懒惰标记+翻转
- hdu1890 Robotic Sort Splay树,区间反转,lazy标记
- hdu1890 Robotic Sort (splay+区间翻转单点更新)
- HDU1890 Robotic Sort
- 伸展树 - hdu1890 Robotic Sort
- HDU1890 Robotic Sort(slpay,区间旋转)
- 【HDU 1890】Robotic Sort【splay】
- BZOJ 3506 robotic sort (splay)
- HDU 1890 Robotic Sort [splay]
- HDU 1890 Robotic sort (splay)
- 【bzoj1552】[Cerc2007]robotic sort splay
- HDU 1890:Robotic Sort Splay
- 【bzoj3506/1552】【robotic sort】【splay】
- Robotic Sort hdu1890 (伸展树翻转+删根)
- 一人一条最新
- Android Activity活动生命周期
- 如何在移动设备上调试html5开发的网页
- Facebook创始人的老婆不够美? 扎克伯格:我自己来回答为何娶丑妻(从此你是我偶像)
- Spring Batch--jobs
- Splay hdu1890 Robotic Sort
- 如何从SharePoint Server 2016 Beta2升级到RC版本?
- jeecg中的路径问题
- 百度地图之定位+移动选址
- 整理一些常用的芯片
- URAL 1029 Ministry
- Android开发,如何使canvas画布背景色透明。
- linux下程序定时重启脚本
- ubuntu apt-get 安装 php7