Submission details
Task:Hamilton
Sender:Sebastian
Submission time:2026-04-17 10:00:18 +0300
Language:C++ (C++20)
Status:READY
Result:88
Feedback
subtaskverdictscore
#10
#2ACCEPTED7
#3ACCEPTED12
#4ACCEPTED69
Test results
testverdicttimescoresubtask
#1ACCEPTED0.01 s100details
#2--01details
#3ACCEPTED0.57 s1002, 3details
#4ACCEPTED4.98 s914details

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
mt19937 mt(time(0));

// --- JUDGE ---

ll judge_query_count;
vector<vector<bool>> judge_graph;

void judge_init(ll n) {
    judge_query_count = 0;
    judge_graph = vector<vector<bool>>(n, vector<bool>(n));
    for (ll i = 0; i < n; i++) {
        for (ll j = i+1; j < n; j++) {
            bool ij = (n <= 10) ? ((j - i + n) % n < n/2) : (mt() % 2);
            judge_graph[i][j] = ij;
            judge_graph[j][i] = !ij;
        }
    }

    for (ll i = 0; i < n; i++) {
        for (ll j = 0; j < n; j++) {
            if (i == j) cout << "0";
            else cout << judge_graph[i][j];
        }
        cout << endl;
    }
}

// true iff u -> v (1-indexed)
bool judge_query(ll u, ll v) {
    assert(u != v);
    judge_query_count++;
    cout << "? " << u << ' ' << v << endl;
    char ans; cin >> ans;
    return ans == '>';
}

// --- CONTESTANT ---

map<pair<ll, ll>, bool> mem;
bool ask(ll u, ll v) {
    if (u > v) return !ask(v, u);
    if (mem.count({u, v})) return mem[{u, v}];
    return mem[{u, v}] = judge_query(u+1, v+1);
}

void solve(ll n) {
    mem.clear();

    // initialize length 2 paths
    vector<vector<ll>> paths;
    for (ll i = 0; i < n/2; i++) {
        if (ask(2*i, 2*i+1)) paths.push_back({2*i, 2*i+1});
        else paths.push_back({2*i+1, 2*i});
    }
    if (n & 1) paths.push_back({n-1});

    auto split_random_path = [&]() {
        ll i;
        do i = mt() % sz(paths);
        while (sz(paths[i]) < 2);
        ll k = 1 + mt() % (sz(paths[i]) - 1);
        paths.emplace_back(paths[i].begin() + k, paths[i].end());
        paths[i].erase(paths[i].begin() + k, paths[i].end());
    };

    ll bad_iterations = 0;
    merge_paths:
    while (sz(paths) > 1) {
        ll i = mt() % sz(paths);
        ll j = mt() % sz(paths);
        if (i == j) continue;
        if (ask(paths[i].back(), paths[j][0])) {
            paths[i].insert(paths[i].end(), all(paths[j]));
            paths.erase(paths.begin() + j);
        }
        else if (++bad_iterations % n == 0) {
            split_random_path();
        }
    }
    if (!ask(paths[0].back(), paths[0][0])) {
        split_random_path();
        goto merge_paths;
    }

    // verify correctness
    for (ll i = 0; i+1 < n; i++) {
        assert(ask(paths[0][i], paths[0][i+1]));
    }
    assert(ask(paths[0].back(), paths[0][0]));
    
    cout << "!";
    for (auto &e : paths[0]) cout << ' ' << e+1;
    cout << endl;
}

// --- TESTS ---

int main() {
    ll n, t; cin >> n >> t;
    judge_init(n);
    for (ll i = 0; i < t; i++) {
        solve(n);
    }
}

Test details

Test 1

Subtask:

Verdict: ACCEPTED

input
0 5 2 fixed 1 2 3 4 5 2 4 1 5 ...

correct output
(empty)

user output
Activating encoder mode
5 2
01000
00100
10010
...

Feedback: Q = 8.000000

Test 2

Subtask: 1

Verdict:

input
01 4 200 rnd

correct output
(empty)

user output
(empty)

Test 3

Subtask: 2, 3

Verdict: ACCEPTED

input
02 50 200 rnd

correct output
(empty)

user output
Activating encoder mode
50 200
011111111100010110100100001101...

Feedback: Q = 89.465000

Test 4

Subtask: 4

Verdict: ACCEPTED

input
03 500 200 rnd

correct output
(empty)

user output
Activating encoder mode
500 200
011111111100010110100100001101...

Feedback: Q = 766.680000