CF1081F Tricky Interactor

· · 题解

CF1081F Tricky Interactor

Description

这是一道交互题。

有一个长度为 n 的 01 序列,初始时 1 的个数为 t。定义一次操作为给定 l, r(l \le r),交互库会等概率地从区间 [1, r] 和 [l, n] 中选择一个,然后翻转下标在该区间内的数(翻转即 x \to 1 - x),并告诉你翻转完的序列中有多少个 1。操作之间不是独立的,即对序列的修改是永久的。

你需要在 10000 次操作内得出原始的序列。

**Solution** 设这个序列为 $a$。注意到连续操作 $[i, i]$ 两次所形成的序列要么是 $a$,要么是除了 $a_i$ 都翻转的序列。我们可以多随几次,总能碰到第二种情况,然后就能知道第 $i$ 个数是 $0$ 还是 $1$ 了,然后再还原回去。当然有一个问题就是除开 $a_i$ 以外剩下的 $01$ 数量相等,这里我们可以把 $[i, i]$ 改为 $[i, i + 1]$,这样每次能得到 $a_i + a_{i + 1}$,最后再统计一下就可以了。这样我们就得到了一个期望 $8n$ 次询问的做法。 其实放 $10000$ 没有必要,完全可以开到 $3000$~~,总不至于有人这么非吧~~。 ```cpp #include <bits/stdc++.h> using namespace std; #define il inline #define re register #define rep(i, s, e) for (re int i = s; i <= e; ++i) #define drep(i, s, e) for (re int i = s; i >= e; --i) #define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout) const int N = 300 + 10; il int read() { int x = 0; bool f = true; char c = getchar(); while (!isdigit(c)) {if (c == '-') f = false; c = getchar();} while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } int n, t, ans[N]; il int ask(int l, int r) { cout << "? " << l << ' ' << r << endl; fflush(stdout); int res; cin >> res; return res; } il int get(int l, int r) { int res; do { res = ask(l, r); res = ask(l, r); } while (res == t); int dis = res - (n - t); do { res = ask(l, r); res = ask(l, r); } while (res != t); return (r - l + 1 + dis) / 2; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> t; if (n == 2 * t + 1 || n == 2 * t - 1) { static int a[N]; rep(i, 1, n - 1) a[i] = get(i, i + 1); a[n] = 2 * t; rep(i, 1, n - 1) a[n] -= a[i]; rep(i, 1, n - 1) { if (i & 1) ans[n] -= a[i]; else ans[n] += a[i]; } ans[n] = (ans[n] + a[n]) / 2; drep(i, n - 1, 1) ans[i] = a[i] - ans[i + 1]; } else rep(i, 1, n) ans[i] = get(i, i); cout << "! "; rep(i, 1, n) cout << ans[i]; cout << endl; return 0; } ```