Guest User

102215H

a guest
Jan 1st, 2021
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef pair<int, int> ii;
  4. typedef long long ll;
  5. #define sz(v) ((int)((v).size()))
  6. #define all(v) (v).begin(),(v).end()
  7.  
  8. int solve(const int n, const function<int(int,int)>& ask) {
  9.     int highestBit = 0;
  10.     for (int i = 0; i <= 30; i++) {
  11.         if (n & (1 << i)) {
  12.             highestBit = i;
  13.         }
  14.     }
  15.  
  16.     vector<int> indices;
  17.     for (int i = 1; i <= n; i++) {
  18.         indices.push_back(i);
  19.     }
  20.  
  21.     vector<int> values;
  22.     for (int i = 0; i <= n; i++) {
  23.         values.push_back(i);    
  24.     }
  25.  
  26.     for (int bit = 0; bit <= highestBit; bit++) {
  27.         if (sz(values) == 1) break;
  28.  
  29.         vector<vector<int>> expected(2);
  30.         for (int x : values) {
  31.             expected[(x >> bit) & 1].push_back(x);
  32.         }
  33.  
  34.         vector<vector<int>> have(2);
  35.         for (int i : indices) {
  36.             int val = ask(i, bit);
  37.             have[val].push_back(i);
  38.         }
  39.  
  40.         assert(
  41.             (sz(have[0]) == sz(expected[0])) ^
  42.             (sz(have[1]) == sz(expected[1]))
  43.         );
  44.  
  45.         if (sz(have[0]) == sz(expected[0])) {
  46.             values = expected[1];
  47.             indices = have[1];
  48.         } else {
  49.             values = expected[0];
  50.             indices = have[0];
  51.         }
  52.  
  53.         //cerr << "indices: "; for (int x : indices) cerr << x << " "; cerr << endl;
  54.         //cerr << "values: "; for (int x : values) cerr << x << " "; cerr << endl;
  55.  
  56.         assert(sz(indices) + 1 == sz(values));
  57.     }
  58.  
  59.     assert(sz(indices) == 0);
  60.     assert(sz(values) == 1);
  61.     return values[0];
  62. }
  63.  
  64. void stress() {
  65.     ofstream log("log.txt");
  66.  
  67.     const int MAX_N = 1000;
  68.     vector<int> a;
  69.     for (int n = 1; n <= MAX_N; n++) {
  70.         a.resize(n);
  71.         for (int missing = 0; missing <= n; missing++) {
  72.             int now = 0;
  73.             for (int i = 0; i < n; i++) {
  74.                 if (now == missing) now++;
  75.                 a[i] = now++;
  76.             }
  77.            
  78.             int questions = 0;
  79.             function<int(int,int)> askEmulator = [&](const int index, const int bit) {
  80.                 assert(1 <= index && index <= n);
  81.                 questions++;
  82.                 int val = (a[index - 1] >> bit) & 1;
  83.                 //log << "  asking index = " << index << ", bit = " << bit << " -> val = " << val << endl;
  84.                 return val;
  85.             };
  86.            
  87.             log << "===================" << endl;
  88.             log << "n = " << n << ", missing = " << missing << endl;
  89.             //log << "arr = "; for (int x : a) log << x << " "; log << endl;
  90.            
  91.             log.flush();
  92.             const int res = solve(n, askEmulator);
  93.             log << "res = " << res << ", questions = " << questions << endl;
  94.            
  95.             if (res != missing) {
  96.                 cerr << "ERROR: n = " << n << ", missing = " << missing << ", res = " << res << endl;
  97.                 throw;
  98.             }
  99.         }
  100.         if (n % (MAX_N / 100) == 0) {
  101.             cerr << "n = " << n << " is OK" << endl;
  102.         }
  103.     }
  104.     cerr << "Stress completed" << endl;
  105. }
  106.  
  107. int main() {
  108.     //ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  109.     //cout.setf(ios::fixed); cout.precision(15);
  110.  
  111.     //stress(); return 0;
  112.  
  113.     int n;
  114.     cin >> n;
  115.  
  116.     function<int(int,int)> askIO = [](const int index, const int bit) {
  117.         cout << "? " << index << ' ' << bit << endl;
  118.         int val;
  119.         cin >> val;
  120.         return val;
  121.     };
  122.  
  123.     int res = solve(n, askIO);
  124.     cout << "! " << res << endl;
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment