Hướng dẫn giải của Ba Cây Bīngqílín
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Code mẫu
#include <bits/stdc++.h> using namespace std; const int MAXN = 10000; // fixed seed for debugging mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // Irrelevant shit int rnd(int n) { return uniform_int_distribution<int>(0, n - 1)(rng); } int compare(int i, int j) { cout << "compare " << i << " " << j << endl; char res; cin >> res; return res == '<'; } int compare_k(int i) { cout << "compare_k " << i << endl; char res; cin >> res; return res == '<'; } void answer(int i, int j) { cout << "answer " << i << " " << j << endl; exit(0); } void no_answer() { cout << "no_answer" << endl; exit(0); } int N; int a[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N; iota(a, a+N, 1); shuffle(a, a+N, rng); int l = 0, r = N-1, answer_small = -1, answer_large = -1; while(l <= r) { int k = ((r - l) >> 1) + 1; nth_element(a+l, a+l+k-1, a+r+1, compare); if (compare_k(a[l + k - 1])) { answer_small = a[l + k - 1]; l += k; } else { answer_large = a[l+k-1]; r = l + k - 2; } } if (answer_small == -1 || answer_large == -1) { no_answer(); return 0; } answer(answer_small, answer_large); return 0; }
Bình luận