HDU 1890:Robotic Sort(Splay)
http://acm.hdu.edu.cn/showproblem.php?pid=1890
题意:有一个无序序列,经过不断地翻转,使得最后的序列是一个升序的序列,而且如果相同数字要使在原本序列靠前的在前面。对于每一个位置,输出于哪个位置翻转。
思路:想通了之后其实挺简单的,不过挺巧妙。一开始先记录每一个位的pos,并对数组升序排序,然后我们从小到大枚举数组里的元素,对于每个元素,我们可以知道:i 就是翻转后应当在序列中的位置,a[i].pos就是它在原序列的位置。这个时候,我们只要在建树的时候利用中序遍历就是原数组的样子的性质,让节点的值就是它在原数组的位置,即建一个新的节点的时候,x 就是在原数组中的编号,例如样例:3 4 5 1 6 2,那么对应的节点的值就是 1 2 3 4 5 6 了。然后对于每一个询问,我们知道了它在原序列的位置,我们将它的位置(即节点本身的值)旋转到根节点,那么它的左子树的 sz 就是它目前所在的位置(因为一开始多了一个节点),然后再对 目标位置 和 所在的位置进行区间翻转,就可以完成了。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x7fffffff
#define lson ch[x][0]
#define rson ch[x][1]
#define keytree ch[ch[root][1]][0]
#define rootkey ch[root][1]
#define N 300000
struct node
{
int val, id;
}a[N];
struct SplayTree {
int num[N], sz[N], fa[N], ch[N][], root, rev[N];
int n; void PushUp(int x) {
sz[x] = sz[lson] + sz[rson] + ;
} void PushDown(int x) {
if(rev[x]) {
swap(lson, rson);
if(lson) rev[lson] ^= ;
if(rson) rev[rson] ^= ;
rev[x] = ;
}
} int NewNode(int f, int k, int kind) {
int x = k;
fa[x] = f;
rev[x] = ch[x][] = ch[x][] = ;
sz[x] = ; ch[f][kind] = x;
return x;
} void Build(int l, int r, int &x, int f, int kind) {
if(l > r) return ;
int m = (l + r) >> ;
x = NewNode(f, m, kind);
Build(l, m - , ch[x][], x, );
Build(m + , r, ch[x][], x, );
PushUp(x);
} void Init() {
root = ;
ch[][] = ch[][] = fa[] = rev[] = sz[] = ;
root = NewNode(, n + , );
rootkey = NewNode(root, n + , );
sz[root] = ;
Build(, n, keytree, rootkey, );
PushUp(rootkey); PushUp(root);
} void Rotate(int x, int kind) {
int y = fa[x], z = fa[y];
PushDown(y); PushDown(x);
ch[y][!kind] = ch[x][kind];
if(ch[x][kind]) fa[ch[x][kind]] = y;
if(z) {
if(y == ch[z][]) ch[z][] = x;
else ch[z][] = x;
}
fa[x] = z; fa[y] = x;
ch[x][kind] = y;
PushUp(y);
} void Splay(int x, int goal) {
PushDown(x);
while(fa[x] != goal) {
int y = fa[x], z = fa[y];
PushDown(z); PushDown(y); PushDown(x);
int kind1 = x == ch[y][];
int kind2 = y == ch[z][];
if(z == goal) {
Rotate(x, kind1);
} else {
if(kind1 == kind2) Rotate(y, kind1);
else Rotate(x, kind1);
Rotate(x, kind2);
}
}
PushUp(x);
if(!goal) root = x;
} void RTO(int k, int goal) {
int x = root;
PushDown(x);
while(sz[lson] + != k) {
if(sz[lson] >= k) x = lson;
else k -= sz[lson] + , x = rson;
PushDown(x);
}
Splay(x, goal);
} void Travel(int x) {
if(lson) Travel(lson);
printf("travel : %d\n", x);
if(rson) Travel(rson);
PushUp(x);
} int Suf(int x) { // 找后继
if(rson) {
PushDown(x);
x = rson;
while(lson) {
x = lson;
PushDown(x);
}
}
return x;
} int Query(int l, int r) {
Splay(r, ); // 把原来节点的位置旋转到root位置
int ans = sz[ch[root][]]; // 因为多了一个节点,所以不用+1
RTO(l, ); // 把第l个数的前一个旋到root
Splay(Suf(r), root); // 把后继旋到ch[root][1]
rev[keytree] ^= ; //区间翻转
return ans;
} }spy; bool cmp(const node &a, const node &b) {
if(a.val == b.val) return a.id < b.id;
return a.val < b.val;
} int main() {
int n;
while(scanf("%d", &n), n) {
spy.n = n;
for(int i = ; i <= n; i++) {
scanf("%d", &spy.num[i]);
a[i].id = i;
a[i].val = spy.num[i];
}
sort(a + , a + + n, cmp);
spy.Init();
for(int i = ; i <= n; i++) {
int ans;
ans = spy.Query(i, a[i].id);
if(i == n) printf("%d\n", ans);
else printf("%d ", ans);
}
}
return ;
}
HDU 1890:Robotic Sort(Splay)的更多相关文章
- HDU 1890 Robotic Sort(splay)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...
- HDU 1890 Robotic Sort (splay tree)
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 4635:Strongly connected(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给出n个点和m条边,问最多能添加几条边使得图不是一个强连通图.如果一开始强连通就-1.思路:把图分成 ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- HDU 3487 Play with Chain(Splay)
题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...
- BZOJ 1588:营业额统计(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:中文题意. 思路:每一个点每一个点插入Splay,然后插入新的一个点之后,查这个节点的前 ...
- HDU 2767:Proving Equivalences(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给出n个点m条边,问在m条边的基础上,最小再添加多少条边可以让图变成强连通.思路:强连通分量缩点后找 ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- HDU 2062:Subset sequence(思维)
Subset sequence Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- linux 设置网卡
配置网卡 修改/etc/sysconfig/network-scripts/ifcfg-eth0文件,如果有多张网卡,则修改相应的网卡 # vim /etc/sysconfig/network-scr ...
- pscp
http://www.physics.rutgers.edu/~matilsky/documents/pscp.htm
- SQL Server字符串函数(超实用)
1. len():计算字符串长度 2. lower().upper():字符串转换为大.小写 3. ltrim().rtrim():截去字符串左.右侧空格 4. space():返回由重复的空格组成的 ...
- hadoop wordcount
Mapper // map的数量与数的分片有关系 public class WCMapper extends Mapper<LongWritable, Text, Text, LongWrita ...
- iOS自动偏移64个像素
自从iOS7开始,如果添加的scrollview是uiviewController第一个视图,系统会默认自动添加-64的偏移量,所以规避的方案就添加一个UIView之后再添加你的scrollview.
- LUA闭包概念演示
闭包的一个重要场景,形成一个自治的环境, 让操作可以封闭运行, 即函数运行时有状态的,可以从闭包创建时候的环境独立开来. 例如下面的lua闭包, genFilter 其入参parmIn是 函数的内部变 ...
- SQL 自动增长 identity
create table Users( id ,),--id 从10000开始,增加长度为1 name ), ); --执行三次这个语句 insert into Users values('小昆虫') ...
- 转:Selenium之CSS Selector定位详解
CSS selector定位 CSS(Cascading Style Sheets)是一种语言,它被用来描述 HTML 和 XML 文档的样式. 百度输入框: <input name=&quo ...
- winform 控件开发1——复合控件
哈哈是不是丑死了? 做了一个不停变色的按钮,可以通过勾选checkbox停下来,代码如下: 复合控件果然简单呀,我都能学会~ using System; using System.Collection ...
- poj: 3094
简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...