CF2226D

· · 题解

场上过了,因为明天还有模拟赛所以没去想 C 就睡觉了。

题意简述

给定 n 和序列 \{a_n\},每次可以选一个区间 [l, r],使得 \max\limits_{i\in [l,r]} a_i 与 \min\limits_{i\in [l,r]} a_i 奇偶性不同,将 [l,r] 反转。求能否排序。

思路简述

注意到如果是相邻一奇一偶,我就可以交换。这告诉我们奇数、偶数可以互相穿过。故可以将它们分开讨论(因为奇数、偶数自己之间的相对顺序才影响答案,其他的可以随便穿过)。

考虑什么时候可以交换奇数的相邻两项。考虑这样一个结构 [1, 4, 3],即两奇数夹着一偶数,并且偶数大于两个奇数或者小于两个奇数。此时可以交换相邻两项。

再考虑什么时候不满足这个条件,那就是所有偶数全都在两个奇数中间。此时不可能存在一个合法的区间全都包含她们俩,因为最大值和最小值都只能是奇数。偶数同理。

所以如果存在一个奇数组成的逆序对中间夹着所有偶数,或者偶数组成的逆序对中间夹着所有奇数,那就是不合法的。用树状数组维护即可。

然后我傻了一下,在赛后的三天提交题解的时候才想到你完全不需要树状数组,因为你的逆序对是天然保证的。大家不要学我。

代码

#include <bits/stdc++.h>
//#define int long long
using namespace std;

#define debugable

template <typename T> void prl (const T& value)  { cout << value << ", "; } 
template <typename First, typename ... Rest> 
void prl (const First& first, const Rest& ... rest) 
    { prl (first); prl (rest ...); }
template <typename ... Args> void deprf (const char* names, Args &&... args) {
#ifndef debugable
    return ;
#endif
    cout << names << ": "; prl (forward <Args> (args) ...);
    cout << endl;
}

#define debug(...) deprf (#__VA_ARGS__, __VA_ARGS__)
#define debua(a,n) ral (#a, a, n)
template <typename T> void ral (const char *names, T *a, int n) { 
#ifndef debugable
    return ;
#endif
    cout << names << ": {"; 
    for (int i = 1 ; i < n ; i ++) 
        cout << a [i] << ", "; 
    cout << a [n] << "}\n";
}


// 存不存在一个逆序对包含奇偶不相同的所有数
const int N = 1e6;
int n, m, k, a [N + 5], b [N + 5], an, bn, am, bm;

int lowbit (int x) { return x & -x; } 
struct FWT {
    int n, t [N + 5];
    FWT (int _n = 0) { init (_n); }
    void init (int _n = 0) {
        n = _n;
        for (int i = 0 ; i <= n ; i ++) t [i] = 0;
    }
    void update (int x, int k) { while (x <= n) t [x] += k, x += lowbit (x); }
    int query (int x) {
        int ans = 0;
        while (x) ans += t [x], x -= lowbit (x);
        return ans;
    }
} f;
void Turn () {
    cin >> k; n = m = 0;
    for (int i = 1 ; i <= k ; i ++) {
        int x; cin >> x;
        if (x & 1) a [++ n] = x;
        else b [++ m] = x;
        // debug (i);
    }

    // debua (a ,n);
    // debua (b, m);

    an = bn = 1e9, am = bm = -1e9;
    for (int i = 1 ; i <= n ; i ++) an = min (an, a [i]), am = max (am, a [i]);
    for (int i = 1 ; i <= m ; i ++) bn = min (bn, b [i]), bm = max (bm, b [i]);
    f. init (k + 1);
    for (int i = 1 ; i <= n ; i ++) {
        if (a [i] <= bn) {
            // debua (f. t, k);
            // debug (i, f. query (k - a [i] + 2));
            if (f. query (k - a [i]) > 0) {
                cout << "NO" << endl;
                return ;
            }
        }
        // debug (a [i]);
        if (a [i] >= bm) f. update (k - a [i] + 1, 1);
    } f. init (k + 1);
    for (int i = 1 ; i <= m ; i ++) {
        if (b [i] <= an) {
            // debug (i);
            if (f. query (k - b [i]) > 0) {
                cout << "NO" << endl;
                return ;
            }
        }
        if (b [i] >= am) f. update (k - b [i] + 1, 1);
    }
    cout << "YES" << endl;
}

int _; void Init () {
//  ios :: sync_with_stdio (0); cin. tie (0); cout. tie (0);
    _ = 1;
//  freopen (".in", "r", stdin);
//  freopen (".out", "w", stdout);
    cin >> _;
}

    signed main () { Init ();
    while (_ --> 0)
    Turn (); return 0; }