CSES - NOI 2024 - Results
Submission details
Task:Chair Game
Sender:Þórhallur Tryggvason
Submission time:2024-03-06 19:03:19 +0200
Language:C++20
Status:COMPILE ERROR

Compiler report

In file included from /usr/include/c++/11/bits/stl_algobase.h:64,
                 from /usr/include/c++/11/bits/specfun.h:45,
                 from /usr/include/c++/11/cmath:1935,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:41,
                 from input/code.cpp:1:
/usr/include/c++/11/bits/stl_pair.h: In instantiation of 'struct std::pair<const int, memory>':
/usr/include/c++/11/ext/aligned_buffer.h:91:28:   required from 'struct __gnu_cxx::__aligned_buffer<std::pair<const int, memory> >'
/usr/include/c++/11/bits/hashtable_policy.h:234:43:   required from 'struct std::__detail::_Hash_node_value_base<std::pair<const int, memory> >'
/usr/include/c++/11/bits/hashtable_policy.h:268:12:   required from 'struct std::__detail::_Hash_node_value<std::pair<const int, memory>, false>'
/usr/include/c++/11/bits/hashtable_policy.h:277:12:   required from 'struct std::__detail::_Hash_node<std::pair<const int, memory>, false>'
/usr/include/c++/11/bits/hashtable_policy.h...

Code

#include <bits/stdc++.h>

using namespace std;

struct memory {
    vector<int> values;
    unordered_map<int, memory> mem;

    // for root
    memory() {
        this->mem = unordered_map<int, memory>();
        this->values = vector<int>();
    }
    memory(vector<int>& chairs, vector<int>& values, uint64_t idx) {
        if (idx == values.size()) {
            this->values = values;
            this->mem = unordered_map<int, memory>();
        } else {
            this->values = vector<int>();
            this->mem = unordered_map<int, memory>();
            memory n = memory(chairs, values, idx + 1);
            this->mem.insert(make_pair(chairs[idx], n));
        }
    }

    pair<int*, uint64_t> find(vector<int>& finding, uint64_t idx) {
        if (idx == finding.size()) {
            return make_pair(values.data(), values.size());
        }
        auto f = mem.find(finding[idx]);
        if (f == mem.end()) {
            return make_pair(nullptr, 0);
        } else {
            return f->second.find(finding, idx + 1);
        }
    }
    void insert(vector<int>& chairs, vector<int>& values, uint64_t idx) {
        auto f = mem.find(values[idx]);
        if (f == mem.end()) {
            memory n(chairs, values, idx + 1);
            this->mem.insert(make_pair(chairs[idx], n));
        } else {
            f->second.insert(chairs, values, idx + 1);
        }
    }
};

int main() {
    int t;
    cin >> t;

    memory root_mem = memory();

    while (t--) {
        int n;
        cin >> n;
        vector<int> chairs(n);
        vector<int> perm(n);
        for (int i = 0; i < n; i++) {
            cin >> chairs[i];
            perm[i] = i;
        }
        sort(chairs.begin(), chairs.end());
        auto f = root_mem.find(chairs, 0);
        if (f.first != nullptr) {
            if (f.second == 0) {
                cout << "NO" << endl;
            } else {
                cout << "YES" << endl;
                for (int* ptr = f.first; ptr < (f.first + f.second); ptr++) {
                    cout << chairs[*ptr] << " ";
                }
                cout << endl;
            }
            continue;
        }
        bool flag = false;
        do {
            vector<bool> marker(n, false);
            bool f = false;
            for (int i = 0; i < n; i++) {
                int pos = (i + chairs[perm[i]]) % n;
                if (marker[pos]) {
                    f = true;
                    break;
                } else {
                    marker[pos] = true;
                }
            }
            if (f) {
                continue;
            }
            flag = true;
            break;
        } while (next_permutation(perm.begin(), perm.end()));

        if (flag) {
            cout << "YES" << endl;
            root_mem.insert(chairs, perm, 0);
            for (int i = 0; i < n; i++) {
                cout << chairs[perm[i]] << " ";
            }
            cout << endl;
        } else {
            root_mem.insert(chairs, perm, 0);
            cout << "NO" << endl;
        }
    }
}