跳到正文

题解

CF1081F Tricky Interactor

Scintilla约 4 分钟
原文
文章信息与版本

文章信息

发布
2021年8月14日
收录
2025年12月25日
更新
2026年11月18日
篇幅
约 4 分钟
题目
CF1081F

版本

  1. v1当前

CF1081F Tricky Interactor

Description

这是一道交互题。

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

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

n≤300n \le 300。

Solution

设这个序列为 aa。注意到连续操作 [i,i][i, i] 两次所形成的序列要么是 aa,要么是除了 aia_i 都翻转的序列。我们可以多随几次,总能碰到第二种情况,然后就能知道第 ii 个数是 00 还是 11 了,然后再还原回去。当然有一个问题就是除开 aia_i 以外剩下的 0101 数量相等,这里我们可以把 [i,i][i, i] 改为 [i,i+1][i, i + 1],这样每次能得到 ai+ai+1a_i + a_{i + 1},最后再统计一下就可以了。这样我们就得到了一个期望 8n8n 次询问的做法。

其实放 1000010000 没有必要,完全可以开到 30003000,总不至于有人这么非吧。

C++
#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;
}
原文
存档评论3 条抓取于