#include #include "validate.h" using namespace std; const int MAX_N = 15'000; vector> query_dag; vector dag_computed; vector> reachable; const bitset& get_reachable(int x) { if (dag_computed[x]) return reachable[x]; dag_computed[x] = 1; auto& c = reachable[x]; c[x] = 1; for (auto nx : query_dag[x]) c |= get_reachable(nx); return c; } void check_case() { int n, k; judge_in >> n >> k; cout << n << ' ' << k << '\n'; cout.flush(); vector order(n); for(auto&o : order) judge_in >> o; const int MAX_QUERIES = 12*n+1000; // defines the query limit int queries = 0; query_dag.resize(n); string qtype; while (author_out >> qtype) { if (qtype == "?") { ++queries; if (queries > MAX_QUERIES) wrong_answer("Used too many queries.\n"); int a, b; if (!(author_out >> a >> b)) wrong_answer("Expected two integers for query type '?'.\n"); if (a==b) wrong_answer("Expected match participants to be distinct.\n"); --b,--a; if (a < 0 || a >= n || b < 0 || b >= n) wrong_answer("Query index out of range.\n"); if (order[b] > order[a]) cout << (b+1) << '\n', query_dag[b].insert(a); else cout << (a+1) << '\n', query_dag[a].insert(b); cout.flush(); } else if (qtype == "!") { dag_computed.assign(n,0); reachable.resize(n); unordered_set ans; { int x; while (judge_ans >> x) ans.insert(x); } for(int kit=k;kit--;) { int x; if (!(author_out >> x)) wrong_answer("Expected another integer in answer.\n"); --x; if (ans.count(x)) { ans.erase(x); if (get_reachable(x).count() <= n-k) wrong_answer("%d is not proven to be in the top k.\n", x+1); } else wrong_answer("%d is not in the top k.\n", x+1); } judge_message("Correct answer in %d queries.\n", queries); return; } else { wrong_answer("Unexpected query type %s\n", qtype.c_str()); } } wrong_answer("Expected top k output.\n"); } int main(int argc, char **argv) { init_io(argc, argv); check_case(); /* Check for trailing output. */ string trash; if (author_out >> trash) { wrong_answer("Trailing output\n"); } /* Yay! */ accept(); }