CSES - Datatähti Open 2021 - Results
Submission details
Task:Sorting
Sender:MAKMED1337
Submission time:2021-01-30 21:30:24 +0200
Language:C++ (C++11)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED36
#2ACCEPTED64
Test results
testverdicttimegroup
#1ACCEPTED0.05 s1, 2details
#2ACCEPTED0.05 s2details
#3ACCEPTED0.05 s1, 2details
#4ACCEPTED0.05 s1, 2details

Code

#include <bits/stdc++.h>
#include <climits>

using namespace std;

template<class T>
using V = vector<T>;
template<class T>
using VV = V<V<T>>;

using ld = long double;
#define ll long long
using ull = unsigned ll;
using PLL = pair<ll, ll>;
using VLL = V<ll>;
using VB = V<bool>;
using VVB = VV<bool>;
using VVLL = VV<ll>;
using Gr = VVLL;
using MLL = map<ll, ll>;
#define UMLL unordered_map<ll, ll, custom_hash>

//using int128 = __int128;
//using double128 = __float128;

#define fast ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr);

#define INF LONG_MAX
#define MINF LONG_MIN

#define R &
#define CR const R

#define FORI(i, a, b) for(ll i = a, max##i = b; i < max##i; ++i)
#define FOR(i, n) FORI(i, 0, n)
#define RFORI(i, a, b) for(ll i = a, min##i = b; i >= min##i; --i)
#define RFOR(i, n) RFORI(i, n, 0)
#define FORA(i, a) for(auto i : a)
#define FORAR(i, a) for(auto R i : a)
#define FORACR(i, a) for(auto CR i : a)
#define ALL(obj) begin(obj), end(obj)
#define Count(q) while(q--)
#define OK cerr << "OK\n";

#define mp make_pair
#define pb push_back

//#define DEBUG

template<class T>
T sqr(T x)
{
    return x * x;
}

void YES(bool g, ostream R os, bool upper = true)
{
    if(g)
        if(upper)
            os << "YES";
        else
            os << "Yes";
    else
        if(upper)
            os << "NO";
        else
            os << "No";

    os << "\n";
}

template<class T>
void show(T CR t, ostream R os = cerr)
{
    FORACR(i, t)
        os << i << " ";
    os << "\n";
}

template<class T>
void show2d(T CR t, ostream R os = cerr)
{
    FORACR(i, t)
        show(i, os);
    os << "\n";
}

constexpr ll MOD = 1e9 + 7;
constexpr ll len = 'z' - 'a' + 1;

constexpr ld PI = atanl(1.0L) * 4;

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator() (uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

ll _mergeSort(VLL R arr, VLL R temp, ll left, ll right);
ll merge(VLL R arr, VLL R temp, ll left, ll mid, ll right);

ll getInv(VLL R a)
{
    ll n = a.size();

	VLL temp(n);
	return _mergeSort(a, temp, 0, n - 1);
}

ll _mergeSort(VLL R a, VLL R temp, ll left, ll right)
{
	ll mid, inv_count = 0;
	if (right > left)
	{
		mid = (right + left) / 2;

		inv_count = _mergeSort(a, temp, left, mid);
		inv_count += _mergeSort(a, temp, mid + 1, right);

		inv_count += merge(a, temp, left, mid + 1, right);
	}

	return inv_count;
}

ll merge(VLL R a, VLL R temp, ll left, ll mid, ll right)
{
	ll i, j, k;
	ll inv_count = 0;

	i = left;
	j = mid;
	k = left;
	while ((i <= mid - 1) && (j <= right))
	{
		if (a[i] <= a[j])
		{
			temp[k++] = a[i++];
		}
		else
		{
			temp[k++] = a[j++];

			inv_count = inv_count + (mid - i);
		}
	}

	while (i <= mid - 1)
		temp[k++] = a[i++];

	while (j <= right)
		temp[k++] = a[j++];

	for (i = left; i <= right; i++)
		a[i] = temp[i];

	return inv_count;
}

//arr
unordered_set<string> dp;

void BFS(string s)
{
    ll n = s.size();

    queue<string> q;

    auto add = [&q](string CR t)
    {
        q.push(t);
        dp.insert(t);
    };

    add(s);
    while(!q.empty())
    {
        s = q.front(); q.pop();

        FOR(i, n - 1)
            FORI(j, i + 2, n - 1)
            {
                string t = s;
                swap(t[i], t[j]);
                swap(t[i + 1], t[j + 1]);

                if(dp.find(t) == dp.end())
                    add(t);
            }
    }
}

void init()
{
    string s;
    FOR(i, 9)
    {
        //cerr << s << "\n";
        BFS(s);

        s += char(i + '1');
    }
}

void solve(istream R is, ostream R os)
{
    ll n;
    is >> n;

    VLL a(n);
    FORAR(i, a)
        is >> i;

    if(n <= 8)
    {
        string s;
        FORACR(i, a)
            s += char(i + '0');

        YES(dp.find(s) != dp.end(), os, 1);
        return;
    }

    YES(getInv(a) % 2 == 0, os, 1);
}

void tester(istream R is, ostream R os)
{
    fast
    init();
    ll q = 1;
    is >> q;
    os << setprecision(999);

    Count(q)
        solve(is, os);
}

int main()
{
    //ifstream in("input.txt");
    //ofstream out("output.txt");

    tester(cin, cout);
}

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
...
Truncated

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
...
Truncated

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
...
Truncated

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
...
Truncated