AGC014_F - Strange Sorting [递推]

Description

Takahashi loves sorting.
He has a permutation ( p1 , p2 ,…, pN ) of the integers from 1 through N. Now, he will repeat the following operation until the permutation becomes ( 1 ,2,…, N ):
First, we will define high and low elements in the permutation, as follows. The i-th element in the permutation is high if the maximum element between the 1-st and i-th elements, inclusive, is the i-th element itself, and otherwise the i-th element is low.
Then, let a1, a2 ,…, ak be the values of the high elements, and b1 , b2 ,…, bNk be the values of the low elements in the current permutation, in the order they appear in it.
Lastly, rearrange the permutation into ( b1 , b2 ,…, bNk , a1 , a2 ,…, ak ).
How many operations are necessary until the permutation is sorted?

Solution

AGC好难啊,最后还是看了题解做的。。
题解的意思大概是对于一个排列,先不考虑 1 这个数。当经过T1次操作以后排列的第一个元素变成了 f ,可以发现f>2,(不然的话会在 T1 次就已经排序好,或者在 T 次还没有排序好),发现如果此时1在排列中的位置在f 2 之间,答案便是T,否则是 T+1 。另外,有一个很神奇的性质就是这三个数的 cyclic order 是不会变的,即一直是( 1 ,2, f )或(2, f ,1)或( f ,1, 2 ),只要在最后判一下这个cyclic order 就好了。那么这个怎么证明呢。考虑在操作的某时刻有一个数 x ,他是high的,并且不在第一个位置,那么必定有一个 y x之前,且满足 y high的, yx ,那么经过操作后两个数被放在了一起,那么这两个数会在 y low的, x high的时候分开,又到了最开始情形,一直循环,所以不可能。于是就有了一个结论:当 f 不是这些元素的第一个时,必然是low的。那么分类讨论一下 1 ,2, f 的位置,就可以证明了。最后问题就相当于从(i+1, i+2 ,…, N )的问题递推到(i, i+1 ,…, N )的问题。
只要一个for就好啦:

for (int i = n - 1; i; i--) {
    if (T[i + 1] == 0) {
        if (q[i] > q[i + 1]) {
            T[i] = 1; f[i] = i + 1;
        }
    } else {
        if (Check(q[f[i + 1]], q[i], q[i + 1])) {
            T[i] = T[i + 1]; f[i] = f[i + 1];
        } else {
            T[i] = T[i + 1] + 1; f[i] = i + 1;
        }
    }
}
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 202020;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; int sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

int n;
int p[N], q[N];
int f[N], T[N];

inline bool Check(int a, int b, int c) {
    return (a < b && b < c) || (b < c && c < a) || (c < a && a < b);
}

int main(void) {
    freopen("1.in", "r", stdin);
    read(n);
    for (int i = 1; i <= n; i++) {
        read(p[i]); q[p[i]] = i;
    }
    for (int i = n - 1; i; i--) {
        if (T[i + 1] == 0) {
            if (q[i] > q[i + 1]) {
                T[i] = 1; f[i] = i + 1;
            }
        } else {
            if (Check(q[f[i + 1]], q[i], q[i + 1])) {
                T[i] = T[i + 1]; f[i] = f[i + 1];
            } else {
                T[i] = T[i + 1] + 1; f[i] = i + 1;
            }
        }
    }
    cout << T[1] << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值