CSES - NOI 2024 - Results
Submission details
Task:Chair Game
Sender:Erik Hedin
Submission time:2024-03-12 00:07:39 +0200
Language:C++ (C++17)
Status:READY
Result:37
Feedback
groupverdictscore
#1ACCEPTED8
#2ACCEPTED5
#3ACCEPTED4
#40
#50
#60
#7ACCEPTED20
#80
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 7, 8details
#2ACCEPTED0.01 s1, 7, 8details
#3ACCEPTED0.01 s1, 7, 8details
#4ACCEPTED0.01 s1, 7, 8details
#5ACCEPTED0.01 s1, 7, 8details
#6ACCEPTED0.01 s7, 8details
#7ACCEPTED0.05 s7, 8details
#8ACCEPTED0.76 s2, 8details
#9ACCEPTED0.01 s3, 4, 5, 6, 8details
#10ACCEPTED0.01 s3, 4, 5, 6, 8details
#11ACCEPTED0.01 s3, 4, 5, 6, 8details
#12ACCEPTED0.12 s3, 4, 5, 6, 8details
#13ACCEPTED0.01 s4, 5, 6, 7, 8details
#14--4, 5, 6, 8details
#15--4, 5, 6, 8details
#16--4, 5, 6, 8details
#17ACCEPTED0.01 s5, 6, 7, 8details
#18ACCEPTED0.40 s5, 6, 8details
#19ACCEPTED0.01 s5, 6, 8details
#20--5, 6, 8details
#21ACCEPTED0.00 s1, 6, 7, 8details
#22ACCEPTED0.02 s6, 7, 8details
#23--6, 8details
#24--6, 8details
#25ACCEPTED0.05 s8details
#26--8details
#27ACCEPTED0.05 s3, 4, 5, 6, 8details
#28ACCEPTED0.06 s8details
#29--8details
#30--8details

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <numeric>
#include <functional>
#include <queue>
#include <random>

using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define sz(a) (ll) a.size()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define trav(itr, a, t) for (t::iterator itr = a.begin(); itr != a.end())

const ll MAXN = 100;

mt19937 mt;

ll n;
deque<ll> chairs;
bool hit[MAXN];
bool used[MAXN];
ll answ[MAXN];
set<pll> pq;
ll cnt[MAXN + 1]; // how many of each chair is left
ll options[MAXN]; // number of options left for chair from pos
bool open[MAXN][MAXN + 1]; // if chair is usable from pos

ll DFS_reps;

bool DFS(const ll& rdepth) {
    if (rdepth == 0) return true;
    if (pq.begin()->first == 0) return false;

    const ll p = pq.begin()->second;
    pq.erase(pq.begin());

    //cout << "DFS(" << p << ", " << rdepth << ")" << endl;

    // check all options in chairs, then
    rep(i, 0, rdepth) {

        ll chair = chairs.front();
        chairs.pop_front();

        if (!hit[(p + chair) % n]) {
            // apply (p, chair)
            hit[(p + chair) % n] = true;
            used[p] = true;

            // using chair
            if (--cnt[chair] == 0) {
                rep(i, 0, n) {
                    if (open[i][chair] && !used[i]) {
                        pq.erase(mp(options[i], i));
                        options[i]--;
                        open[i][chair] = false;
                        pq.insert(mp(options[i], i));
                    }
                }
            }

            // jumping to (p + chair) % n
            rep(i, 0, rdepth - 1) {
                ll c = chairs.front();
                chairs.pop_front();
                ll oth = (p + chair + n - c) % n;
                //cout << oth << " " << c << " " << p << " " << chair << endl;
                if (open[oth][c] && !used[oth]) {
                    pq.erase(mp(options[oth], oth));
                    options[oth]--;
                    open[oth][c] = false;
                    pq.insert(mp(options[oth], oth));
                }
                chairs.pb(c);
            }

            // check for solution
            if (DFS(rdepth - 1)) {
                answ[p] = chair;
                return true;
            }
            // check if we may be stuck and should return
            if (++DFS_reps > 20 * n) return false;

            // undo using chair
            if (cnt[chair]++ == 0) {
                rep(i, 0, n) {
                    if (!hit[(i + chair) % n] && !used[i]) {
                        pq.erase(mp(options[i], i));
                        options[i]++;
                        open[i][chair] = true;
                        pq.insert(mp(options[i], i));
                    }
                }
            }

            // undo jumping to (p + chair) % n
            rep(i, 0, rdepth - 1) {
                ll c = chairs.front();
                chairs.pop_front();
                ll oth = (p + chair + n - c) % n;
                if (!open[oth][c] && !used[oth]) {
                    pq.erase(mp(options[oth], oth));
                    options[oth]++;
                    open[oth][c] = true;
                    pq.insert(mp(options[oth], oth));
                }
                chairs.pb(c);
            }

            // undo (p, chair)
            hit[(p + chair) % n] = false;
            used[p] = false;
        }

        chairs.pb(chair);
    }

    //cout << "leave DFS(" << p << ", " << rdepth << ")" << endl;

    pq.insert(mp(options[p], p));
    return false;
}

void solve() {
    cin >> n;
    vll s(n);
    rep(i, 0, n) cin >> s[i];

    if (accumulate(s.begin(), s.end(), 0LL) % n) {
        cout << "NO\n";
        return;
    }

    // clear previos data
    do {
        chairs.clear();
        chairs.shrink_to_fit();
        rep(i, 0, n) {
            hit[i] = false;
            used[i] = false;
            cnt[i + 1] = 0;
            options[i] = 0;
        }
        pq.clear();

        // initialize data
        shuffle(s.begin(), s.end(), mt);
        rep(i, 0, n) chairs.pb(s[i]);

        rep(i, 0, n) cnt[s[i]]++;
        rep(i, 0, n) rep(j, 1, n + 1) open[i][j] = cnt[j];
        rep(i, 0, n) rep(j, 1, n + 1) options[i] += open[i][j];

        rep(i, 0, n) pq.insert(mp(options[i], i));

        // DFS
        DFS_reps = 0;
    } while (!DFS(n));

    // answer
    cout << "YES\n";
    rep(i, 0, n) cout << answ[i] << " ";
    cout << "\n";
}

int main() {
    cin.tie(NULL)->sync_with_stdio(false);

    random_device rd;
    //mt.seed(rd());
    mt.seed(0);

    ll testcases;
    cin >> testcases;
    rep(testcase, 0, testcases) {
        solve();
    }
    cout << flush;

    return 0;
}

/*


*/

Test details

Test 1

Group: 1, 7, 8

Verdict: ACCEPTED

input
637
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 2

Group: 1, 7, 8

Verdict: ACCEPTED

input
246
7
1 1 1 1 1 1 1
7
1 1 2 1 1 7 1
...

correct output
YES
1 1 1 1 1 1 1 
YES
1 1 1 1 2 7 1 
YES
...

user output
YES
1 1 1 1 1 1 1 
YES
1 1 1 2 7 1 1 
YES
...
Truncated

Test 3

Group: 1, 7, 8

Verdict: ACCEPTED

input
810
8
1 1 1 1 1 1 1 1
8
1 1 1 8 1 1 2 1
...

correct output
YES
1 1 1 1 1 1 1 1 
YES
1 1 2 8 1 1 1 1 
YES
...

user output
YES
1 1 1 1 1 1 1 1 
YES
1 1 1 1 1 2 8 1 
YES
...
Truncated

Test 4

Group: 1, 7, 8

Verdict: ACCEPTED

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

correct output
NO
YES
8 2 2 6 2 5 1 6 
NO
NO
...

user output
NO
YES
6 2 2 6 1 5 2 8 
NO
NO
...
Truncated

Test 5

Group: 1, 7, 8

Verdict: ACCEPTED

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

correct output
YES
7 2 2 7 1 3 8 2 
YES
4 4 7 3 3 5 5 1 
YES
...

user output
YES
2 7 2 8 1 2 3 7 
YES
4 1 3 4 5 3 5 7 
YES
...
Truncated

Test 6

Group: 7, 8

Verdict: ACCEPTED

input
1000
16
15 16 6 4 14 2 1 6 2 16 10 2 9...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...
Truncated

Test 7

Group: 7, 8

Verdict: ACCEPTED

input
1000
16
2 4 13 6 8 16 12 8 16 5 9 5 9 ...

correct output
YES
13 5 2 8 12 2 8 5 16 16 9 6 9 ...

user output
YES
2 2 2 11 8 16 9 4 5 16 6 12 5 ...
Truncated

Test 8

Group: 2, 8

Verdict: ACCEPTED

input
1000
1
1
2
1 2
...

correct output
YES

NO
YES
3 1 2 
...

user output
YES

NO
YES
2 3 1 
...
Truncated

Test 9

Group: 3, 4, 5, 6, 8

Verdict: ACCEPTED

input
988
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 10

Group: 3, 4, 5, 6, 8

Verdict: ACCEPTED

input
199
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 11

Group: 3, 4, 5, 6, 8

Verdict: ACCEPTED

input
1000
100
1 1 1 2 1 1 2 2 1 1 1 1 1 2 1 ...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...
Truncated

Test 12

Group: 3, 4, 5, 6, 8

Verdict: ACCEPTED

input
1000
100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
YES
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
YES
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
Truncated

Test 13

Group: 4, 5, 6, 7, 8

Verdict: ACCEPTED

input
963
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 14

Group: 4, 5, 6, 8

Verdict:

input
979
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
(empty)

Test 15

Group: 4, 5, 6, 8

Verdict:

input
1000
100
3 3 1 2 1 1 2 3 1 3 2 1 1 3 1 ...

correct output
NO
NO
NO
NO
NO
...

user output
(empty)

Test 16

Group: 4, 5, 6, 8

Verdict:

input
1000
100
1 2 2 2 2 1 1 1 2 3 1 1 3 2 1 ...

correct output
YES
2 2 2 3 1 2 3 1 2 3 1 3 1 3 1 ...

user output
(empty)

Test 17

Group: 5, 6, 7, 8

Verdict: ACCEPTED

input
980
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 18

Group: 5, 6, 8

Verdict: ACCEPTED

input
947
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 19

Group: 5, 6, 8

Verdict: ACCEPTED

input
1000
100
1 2 4 2 1 3 1 2 2 3 1 1 3 1 4 ...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...
Truncated

Test 20

Group: 5, 6, 8

Verdict:

input
1000
100
3 4 4 4 4 4 4 3 3 3 4 4 2 3 3 ...

correct output
YES
4 2 4 4 1 3 4 2 4 2 3 4 2 4 4 ...

user output
(empty)

Test 21

Group: 1, 6, 7, 8

Verdict: ACCEPTED

input
715
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 22

Group: 6, 7, 8

Verdict: ACCEPTED

input
843
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 23

Group: 6, 8

Verdict:

input
1000
100
3 4 5 1 4 4 2 3 2 3 4 1 1 1 2 ...

correct output
NO
NO
NO
NO
NO
...

user output
(empty)

Test 24

Group: 6, 8

Verdict:

input
1000
100
5 3 4 3 5 3 3 5 5 4 5 5 5 5 2 ...

correct output
YES
4 4 5 5 2 4 4 5 3 5 5 2 5 5 2 ...

user output
(empty)

Test 25

Group: 8

Verdict: ACCEPTED

input
1000
100
88 70 59 44 28 10 19 19 42 16 ...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...
Truncated

Test 26

Group: 8

Verdict:

input
1000
100
31 72 52 30 77 56 79 10 88 11 ...

correct output
YES
31 62 14 10 66 63 1 82 37 92 3...

user output
(empty)

Test 27

Group: 3, 4, 5, 6, 8

Verdict: ACCEPTED

input
1000
1
1
2
1 1
...

correct output
YES

YES
1 1 
YES
...

user output
YES

YES
1 1 
YES
...
Truncated

Test 28

Group: 8

Verdict: ACCEPTED

input
1000
1
1
2
2 2
...

correct output
YES

YES
2 2 
YES
...

user output
YES

YES
2 2 
YES
...
Truncated

Test 29

Group: 8

Verdict:

input
1000
100
87 81 29 35 8 98 77 50 46 34 5...

correct output
YES
34 74 25 91 80 18 95 26 88 12 ...

user output
(empty)

Test 30

Group: 8

Verdict:

input
1000
100
65 92 39 22 67 41 17 65 97 71 ...

correct output
YES
9 38 24 59 69 24 63 3 22 35 24...

user output
(empty)