CSES - NOI 2024 - Results
Submission details
Task:Chair Game
Sender:Vladyslav Levchenko
Submission time:2024-03-06 19:55:07 +0200
Language:C++ (C++20)
Status:COMPILE ERROR

Compiler report

input/code.cpp:1:10: fatal error: main.hpp: No such file or directory
    1 | #include "main.hpp"
      |          ^~~~~~~~~~
compilation terminated.

Code

#include "main.hpp"
// #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int inf = 1e9;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int t; cin>>t;
    while (t--){
        int n; cin>>n;
        vector<int> s(n + 1);
        int sum = 0;
        for (int i = 1; i <= n; i++){
            cin>>s[i];
            sum += s[i];
        }
        if (sum % n != 0){
            cout<<"NO"<<"\n";
            continue;
        }
        if (n <= 8){
            vector<int> p;
            for (int i = 1; i <= n; i++){
                p.push_back(i);
            }
            auto f = [&](int v){
                return v - n * (v > n);
            };
            auto check = [&](){
                vector<bool> used(n + 1);
                for (int i = 1; i <= n; i++){
                    int v = f(i + s[p[i - 1]]);
                    if (used[v]) return false;
                    used[v] = true;
                }
                cout<<"YES"<<"\n";
                for (int i: p){
                    cout<<s[i]<<" ";
                }
                cout<<"\n";
                return true;
            };
            if (check()) continue;
            bool ind = false;
            while (next_permutation(p.begin(), p.end())){
                if (check()){
                    ind = true;
                    break;
                }
            }
            if (ind) continue;
            cout<<"NO"<<"\n";
        }
        else {
            auto f = [&](int k, int j){
                int v = k + s[j];
                if (v > n) v -= n;
                return v;
            };
            int x[n + 1][n + 1];
            for (int j = 1; j <= n; j++){
                for (int k = 1; k <= n; k++){
                    x[f(k, j)][j] = k;
                }
            }
            vector<int> cnt(n + 1);
            for (int i = 1; i <= n; i++){
                vector<bool> vis(n + 1);
                for (int j = 1; j <= n; j++){
                    vis[x[i][j]] = true;
                }
                for (int j = 1; j <= n; j++){
                    cnt[j] += vis[j];
                }
            }
            set<int> p, mp;
            for (int i = 1; i <= n; i++){
                p.insert(i);
            }
            vector<int> out;
            bool ind = false;
            for (int i = 1; i <= n; i++){
                vector<bool> vis(n + 1);
                for (int j = 1; j <= n; j++){
                    vis[x[i][j]] = true;
                }
                for (int j = 1; j <= n; j++){
                    cnt[j] -= vis[j];
                }
                int mini = inf, k = 0;
                for (int j: p){
                    int v = x[i][j];
                    if (mp.find(v) != mp.end()) continue;
                    if (cnt[v] < mini){
                        mini = cnt[v];
                        k = j;
                    }
                }
                if (!k){
                    ind = true;
                    break;
                }
                for (int j = i + 1; j <= n; j++){
                    cnt[x[j][k]]--;
                }
                p.erase(k);
                out.push_back(k);
                mp.insert(x[i][k]);
            }
            if (ind){
                cout<<"NO"<<"\n";
                continue;
            }
            cout<<"YES"<<"\n";
            for (int i: out) cout<<s[i]<<" ";
            cout<<"\n";
        }
    }
}