JZOJ3599【CQOI2014】排序机械臂

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();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值