CSES - Datatähti Open 2021 - Results
Submission details
Task:Sorting
Sender:meatrow
Submission time:2021-01-29 21:55:31 +0200
Language:C++17
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED36
#2ACCEPTED64
Test results
testverdicttimegroup
#1ACCEPTED0.15 s1, 2details
#2ACCEPTED0.16 s2details
#3ACCEPTED0.15 s1, 2details
#4ACCEPTED0.15 s1, 2details

Compiler report

input/code.cpp: In member function 'void Fenwick::add(int)':
input/code.cpp:33:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (; pos < data.size(); pos |= pos + 1) {
                ~~~~^~~~~~~~~~~~~

Code

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
 
const int mod = 1e9 + 7;
 
ll binpow(ll a, ll p) {
    ll res = 1;
    while (p) {
        if (p & 1) {
            (res *= a) %= mod;
        }
        p >>= 1;
        (a *= a) %= mod;
    }
    return res;
}

class Fenwick {
private:
    vector<int> data;
public:
    Fenwick(int n) {
        data.assign(n, 0);
    }

    void add(int pos) {
        for (; pos < data.size(); pos |= pos + 1) {
            data[pos]++;
        }
    }
    int get(int pos) {
        int res = 0;
        for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
            res += data[pos];
        }
        return res;
    }
};

map<vector<int>, int> mp;

void solve() {
    int n;
    cin >> n;
    Fenwick f(n + 1);
    vector<int> a(n);
    int inv = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        inv += f.get(n) - f.get(a[i]);
        f.add(a[i]);
    }
    if (inv % 2) {
        cout << "NO\n";
        return;
    }
    if (n > 8) {
        cout << "YES\n";
        return;
    }
    if (mp.count(a)) {
        cout << "YES\n";
    } else {
        cout << "NO\n";
    }
}

void precalc() {
    for (int n = 1; n <= 8; n++) {
        vector<int> a(n);
        iota(a.begin(), a.end(), 1);
        mp[a] = 1;
        queue<vector<int>> q;
        q.push(a);
        while (!q.empty()) {
            a = q.front();
            q.pop();
            for (int i = 0; i + 1 < n; i++) {
                for (int j = i + 2; j + 1 < n; j++) {
                    swap(a[i], a[j]);
                    swap(a[i + 1], a[j + 1]);
                    if (!mp.count(a)) {
                        mp[a] = 1;
                        q.push(a);
                    }
                }
            }
        }
    }
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T = 1;
    cin >> T;
    precalc();
    for (int tc = 1; tc <= T; tc++) {
        // cout << "Case #" << tc << ": ";
        solve();
    }
    return 0;
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
153
1
1
2
1 2
...

correct output
YES
YES
NO
NO
NO
...

user output
YES
YES
NO
NO
NO
...

Test 2

Group: 2

Verdict: ACCEPTED

input
1000
59
35 29 32 50 11 15 9 21 19 45 2...

correct output
YES
NO
YES
NO
YES
...

user output
YES
NO
YES
NO
YES
...

Test 3

Group: 1, 2

Verdict: ACCEPTED

input
720
6
1 6 4 5 2 3
6
6 3 2 1 5 4
...

correct output
YES
NO
NO
NO
YES
...

user output
YES
NO
NO
NO
YES
...

Test 4

Group: 1, 2

Verdict: ACCEPTED

input
1000
8
7 4 2 8 6 3 5 1
8
3 8 2 7 5 4 6 1
...

correct output
NO
NO
YES
NO
YES
...

user output
NO
NO
YES
NO
YES
...