博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[HDU1890]RoboticSort
阅读量:5943 次
发布时间:2019-06-19

本文共 4714 字,大约阅读时间需要 15 分钟。

Problem

每次找到最小值,然后把它和它前面的数翻转,然后找第二小数······

然后输出这些数的下标。

Solution

用splay维护,每次找到最小值,然后翻转前面区间。

Notice

细节操作巨烦无比。

Code

#include
#include
#include
#include
#include
using namespace std;#define sqz main#define ll long long#define reg register int#define rep(i, a, b) for (reg i = a; i <= b; i++)#define per(i, a, b) for (reg i = a; i >= b; i--)#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)const int INF = 1e9, N = 100000;const double eps = 1e-6, phi = acos(-1.0);ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}int point = 0, root, pre, suf, ans, x[N + 5];struct Node{ int val, id;}T[N + 5];struct node{ int val[N + 5], son[2][N + 5], parent[N + 5], Min[N + 5], Size[N + 5], rev[N + 5]; inline void up(int u) { Size[u] = Size[son[0][u]] + Size[son[1][u]] + 1; Min[u] = val[u]; if (son[0][u]) Min[u] = min(Min[u], Min[son[0][u]]); if (son[1][u]) Min[u] = min(Min[u], Min[son[1][u]]); } inline void down(int u) { if (rev[u]) { swap(son[0][u], son[1][u]); rev[son[0][u]] ^= 1, rev[son[1][u]] ^= 1; rev[u] = 0; } } void Newnode(int &u, int from, int v) { u = ++point; son[0][u] = son[1][u] = 0; Min[u] = val[u] = v; Size[u] = 1, rev[u] = 0; parent[u] = from; } void Build(int &u, int l, int r, int from) { int mid = (l + r) >> 1; Newnode(u, from, x[mid]); if (l < mid) Build(son[0][u], l, mid - 1, u); if (r > mid) Build(son[1][u], mid + 1, r, u); up(u); } void Init(int n) { root = point = 0; son[0][0] = son[1][0] = parent[0] = Size[0] = rev[0] = 0; Min[0] = val[0] = N; Newnode(root, 0, N); Newnode(son[1][root], root, N); Build(son[0][son[1][root]], 1, n, son[1][root]); up(son[1][root]); up(root); } void Rotate(int x, int &rt) { int y = parent[x], z = parent[y]; int l = (son[1][y] == x), r = 1 - l; if (y == rt) rt = x; else if (son[0][z] == y) son[0][z] = x; else son[1][z] = x; parent[x] = z; parent[son[r][x]] = y, son[l][y] = son[r][x]; parent[y] = x, son[r][x] = y; up(y), up(x); } void Splay(int x, int &rt) { while (x != rt) { int y = parent[x], z = parent[y]; down(z), down(y), down(x); if (y != rt) { if ((son[0][z] == y) ^ (son[0][y] == x)) Rotate(x, rt); else Rotate(y, rt); } Rotate(x, rt); } } void Delete(int x) { Splay(x, root); if (son[0][x] * son[1][x] == 0) root = son[0][x] + son[1][x]; else { down(x); int t = son[1][x]; down(t); while (son[0][t] != 0) t = son[0][t], down(t); Splay(t, root); son[0][t] = son[0][x], parent[son[0][x]] = t; up(t); } parent[root] = 0; } int Find(int u, int num) { if (num == Size[son[0][u]] + 1) return u; else if (num <= Size[son[0][u]]) return Find(son[0][u], num); else return Find(son[1][u], num - Size[son[0][u]] - 1); } int Find_Min(int u, int tt) { down(u); if (val[u] == tt) return Size[son[0][u]] + 1; else if (Min[son[0][u]] == tt) return Find_Min(son[0][u], tt); else return Size[son[0][u]] + 1 + Find_Min(son[1][u], tt); }}Splay_tree;int cmp(Node X, Node Y){ return X.val < Y.val ||(X.val == Y.val && X.id < Y.id);}int sqz(){ int n; while (scanf("%d", &n) && n) { rep(i, 1, n) T[i].val = read(), T[i].id = i; sort(T + 1, T + n + 1, cmp); rep(i, 1, n) x[T[i].id] = i; Splay_tree.Init(n); rep(i, 1, n - 1) { int t = Splay_tree.Find_Min(root, i); printf("%d ", t + i - 2); Splay_tree.Splay(Splay_tree.Find(root, 1), root); Splay_tree.Splay(Splay_tree.Find(root, t), Splay_tree.son[1][root]); Splay_tree.rev[Splay_tree.son[0][Splay_tree.son[1][root]]] ^= 1; Splay_tree.Delete(Splay_tree.Find(root, t)); } printf("%d\n", n); }}

转载于:https://www.cnblogs.com/WizardCowboy/p/7629032.html

你可能感兴趣的文章
Activiti6.0,spring5,SSM,工作流引擎,OA
查看>>
第十三章:SpringCloud Config Client的配置
查看>>
使用 GPUImage 实现一个简单相机
查看>>
CoinWhiteBook:区块链在慈善事业中的应用
查看>>
Mac上基于Github搭建Hexo博客
查看>>
阿里云服务器ECS开放8080端口
查看>>
Spring中实现监听的方法
查看>>
使用Tooltip会出现一个问题,如果行上出现复选框
查看>>
11.03T1 DP
查看>>
P2924 [USACO08DEC]大栅栏Largest Fence
查看>>
jQuery操作table tr td
查看>>
工作总结:MFC自写排序算法(升序)
查看>>
螺旋队列问题之二
查看>>
扩展运算符和解构赋值的理解
查看>>
手机H5显示一像素的细线
查看>>
Menu 菜单栏
查看>>
Integer跟int的区别(备份回忆)
查看>>
集合解析
查看>>
详解分布式应用程序协调服务Zookeeper
查看>>
软件工程之构建之法
查看>>