zoukankan      html  css  js  c++  java
  • DISCO Presents Discovery Channel Code Contest 2020 Qual Task E. Majority of Balls

    Not able to solve this problem during the contest (virtual participation).

    The first observation is that if we can identify $N-1$ balls of which half is blue and the other half is red, then with these $N - 1$ balls we can identify the color of the rest $N+1$ balls.

    It's not hard to note that if there are more blue balls than red balls among balls numbered from $l$ to $l + N - 1$ but less among balls numbered from $l + 1$ to $l + N$, we then know that

    1. The $l$-th ball and the $l+N$-th ball must be of different colors.
    2. The $l$-th ball must be blue and the $l+N$-th ball must be red.
    3. There are equal number of blue and red balls among balls numbered from $l + 1$ to $l+N - 1$.

    The problem is whether such $l$ even exists? The answer is ,fortunately, YES.

    Here comes the second interesting observation:
    Let's assume without loss of generality, there are more blue balls than red balls among the first $N$ balls, then there must be more red balls among the last $N$ balls. So such $l$ as described above must exist, and we can find one using binary search which costs at most $log_2 N + 1$ queries. When the binary search finishes, we get $l$ and the color of the $l$-th and $l+N$-th balls.

    When $l$ is found, for each ball numbered from $1$ to $l - 1$ or from $l + N + 1$ to $2N$, we can know its color with a query. Note that exactly half of these $N - 1$ known balls are blue, so we can use these balls to identify color of balls numbered from $l + 1$ to $l + N -1$ in a similar way.

    code
    
    int main() {
      int n;
      cin >> n;
      auto ask = [&](int l) {
        cout << '?';
        for (int i = 0; i < n; i++) {
          cout << ' ' << l + i;
        }
        cout << endl;
        string res;
        cin >> res;
        return res.front();
      };
      char ml = ask(1);
      int l = 1, r = n + 1;
    

    while (r - l > 1) {
    int mid = l + (r - l) / 2;
    if (ask(mid) == ml) {
    l = mid;
    } else {
    r = mid;
    }
    }
    vector ans(2 * n + 1);
    ans[l] = ml;
    ans[l + n] = ml == 'R' ? 'B' : 'R';

    auto ask2 = [&](int pos) {
    cout << '?';
    for (int i = 1; i < n; i++) {
    cout << ' ' << l + i;
    }
    cout << ' ' << pos << endl;
    string res;
    cin >> res;
    return res.front();
    };
    // [l + 1, l + n)
    rng (i, 1, 2 * n + 1) {
    if (i < l || i > l + n) {
    ans[i] = ask2(i);
    }
    }
    auto ask3 = [&](int pos) {
    cout << '?';
    rng (i, 1, 2 * n + 1) {
    if (i < l || i > l + n) {
    cout << ' ' << i;
    }
    }
    cout << ' ' << pos << endl;
    string res;
    cin >> res;
    return res.front();
    };
    rng (i, l + 1, l + n) {
    ans[i] = ask3(i);
    }

    cout << "! ";
    for (int i = 1; i <= 2 * n; i++) {
    cout << ans[i];
    }
    cout << ' ';
    return 0;
    }

  • 相关阅读:
    【体验】在Adobe After Effects CC 2018中使用脚本创建窗口
    flask中错误使用flask.redirect('/path')导致的框架奇怪错误
    01-复杂度2 Maximum Subsequence Sum
    01-复杂度1 最大子列和问题
    02-线性结构1 两个有序链表序列的合并
    bfs—迷宫问题—poj3984
    bfs—Dungeon Master—poj2251
    bfs—Catch That Cow—poj3278
    GPTL—练习集—006树的遍历
    DB2存储过程——参数详解
  • 原文地址:https://www.cnblogs.com/Patt/p/11924674.html
Copyright © 2011-2022 走看看