C. AquaMoon and Strange Sort

C. AquaMoon and Strange Sort

思路 :

对于任意一个数\(a_i\), 它在原序列中出现在奇数位置的次数也一定等于在正确序列中出现在奇数位置的次数, 否则, 就一定会出现\(left\), 也就是相当于移动了奇数次. 同理, 在原序列中出现在偶数位置的次数也一定等于在正确序列中出现在偶数位置的次数.

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 1e5 + 7;
int a[N], b[N];
int ak[N][2], bk[N][2];

int main() {
    //freopen("in.txt", "r", stdin);
    IO;
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        memset(ak, 0, sizeof ak);
        memset(bk, 0, sizeof bk);
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            b[i] = a[i];
        }
        sort(b + 1, b + 1 + n);
        for (int i = 1; i <= n; ++i) 
            if (i % 2 != 0) {
                ak[a[i]][0]++;
                bk[b[i]][0]++;
            } else {
                ak[a[i]][1]++;
                bk[b[i]][1]++;
            }
        int f = 1;
        for (int i = 1; i <= n; ++i) 
            if (ak[a[i]][0] != bk[a[i]][0] || ak[a[i]][1] != bk[a[i]][1])
                f = 0;
        if (f) cout << "YES\n";
        else cout << "NO\n";
    } 
    return 0;
}
posted @ 2021-07-18 13:25  phr2000  阅读(54)  评论(0编辑  收藏  举报