您的位置:首页 > 其它

JZOJ3599【CQOI2014】排序机械臂

2017-05-15 15:37 288 查看

Description:



Input:

第一行包含正整数n,表示需要排序的物品数量。

第二行包含n个空格分隔的整数ai,表示每个物品的高度。

Output:

输出一行包含n个空格分隔的整数pi。

Sample Input:

输入1:

6

3 4 5 1 6 2

输入2:

4

3 3 2 1

Sample Output

输出1:

4 6 4 5 6 6

输出2:

4 2 4 4

Data Constraint:

对于30%的数据 1<=n<1000

对于100%的数据 1<=n<=100000 ,1<=ai<=2,000,000,000

题目大意:

给一个长度为n的序列,共有i次操作(1<=i<=n),第i次操作找出第i个数到第n数中最小的那一个,设它的位置是x,输出x,之后将区间[i,x]反序。

题解:

裸的splay啊。

先排个序,然后直接做就行了,区间取反的操作一样可以下传打标记。

一开始没有想到排序,想着用splay找最小值,我也是醉了。

Code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

const int Maxn = 100005, INF = 1 << 30;

int n, a[Maxn], b[Maxn], t[Maxn][2], fa[Maxn], lz[Maxn], s[Maxn], d[Maxn];

bool rank_b(int x, int y) {
if(a[x] < a[y]) return 1;
if(a[x] > a[y]) return 0;
return x < y;
}

void update(int x) {s[x] = s[t[x][0]] + s[t[x][1]] + 1;}
int lr(int x) {return t[fa[x]][1] == x;}
void chan(int x) {if(x) swap(t[x][0], t[x][1]), update(x), lz[x] ^= 1;}
void down(int x) {if(lz[x]) chan(t[x][0]), chan(t[x][1]), lz[x] = 0;}
void xc(int x) {
while(x) d[++ d[0]] = x, x = fa[x];
for(; d[0]; d[0] --) down(d[d[0]]);
}
void rotate(int x) {
int y = fa[x], k = lr(x);
t[y][k] = t[x][!k];
if(t[x][!k]) fa[t[x][!k]] = y;
fa[x] = fa[y];
if(fa[y]) t[fa[y]][lr(y)] = x;
t[x][!k] = y; fa[y] = x;
update(y); update(x);
}
void splay(int x, int y) {
xc(x);
while(fa[x] != y) {
if(fa[fa[x]] != y)
if(lr(x) == lr(fa[x])) rotate(fa[x]); else rotate(x);
rotate(x);
}
}
int find(int x) {
down(x);
return t[x][0] == 0 ? x : find(t[x][0]);
}

void Init() {
scanf("%d", &n);
s[1] = 1;
fo(i, 1, n) {
scanf("%d", &a[i + 1]);
fa[i] = i + 1; t[i + 1][0] = i; update(i + 1);
}
fa[n + 1] = n + 2; t[n + 2][0] = n + 1; update(n + 2);
fo(i, 1, n) b[i] = i + 1;
sort(b + 1, b + n + 1, rank_b);
}

void Work() {
b[0] = 1;
fo(i, 1, n) {
int x = b[i];
splay(x, 0);
printf("%d ", s[t[x][0]]);
int y = find(t[x][1]);
splay(b[i - 1], 0);
splay(y, b[i - 1]);
chan(t[y][0]);
}
}

int main() {
Init();
Work();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: