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 > 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 << '\n';

return 0;

}

DISCO Presents Discovery Channel Code Contest 2020 Qual Task E. Majority of Balls的更多相关文章

  1. DISCO Presents Discovery Channel Code Contest 2020 Qual题解

    传送门 \(A\) 咕咕 int x,y; int c[4]={0,300000,200000,100000}; int res; int main(){ cin>>x>>y; ...

  2. IOCCC(The International Obfuscated C Code Contest)

    国际 C 语言混乱代码大赛(IOCCC, The International Obfuscated C Code Contest)是一项国际编程赛事,从 1984 年开始,每年举办一次(1997年.1 ...

  3. 【AtCoder】CODE FESTIVAL 2016 qual A

    CODE FESTIVAL 2016 qual A A - CODEFESTIVAL 2016 -- #include <bits/stdc++.h> #define fi first # ...

  4. 【AtCoder】CODE FESTIVAL 2016 qual B

    CODE FESTIVAL 2016 qual B A - Signboard -- #include <bits/stdc++.h> #define fi first #define s ...

  5. 【AtCoder】CODE FESTIVAL 2016 qual C

    CODE FESTIVAL 2016 qual C A - CF -- #include <bits/stdc++.h> #define fi first #define se secon ...

  6. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】

    CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...

  7. CODE FESTIVAL 2017 qual B C - 3 Steps【二分图】

    CODE FESTIVAL 2017 qual B C - 3 Steps 题意:给定一个n个结点m条边的无向图,若两点间走三步可以到,那么两点间可以直接连一条边,已经有边的不能连,问一共最多能连多少 ...

  8. M-SOLUTIONS Programming Contest 2020 题解

    M-SOLUTIONS Programming Contest 2020 题解 目录 M-SOLUTIONS Programming Contest 2020 题解 A - Kyu in AtCode ...

  9. Atcoder CODE FESTIVAL 2017 qual B D - 101 to 010 dp

    题目链接 题意 对于一个\(01\)串,如果其中存在子串\(101\),则可以将它变成\(010\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...

随机推荐

  1. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  2. 使用Excel拼凑SQL语句

       快速将一列多行数据合并到一个单元格            EXCEL如何快速将一列多行数据合并到一个单元格,并加分隔符?这是批量处理由一线业务员统计的数据时的常用方法,尤其是当一列数据是wher ...

  3. codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...

  4. 在树莓派4b上安装 ROS MELODIC 源码安装

    按照以下步骤照做就可以了,很简单的,就是浪费一点点时间罢了.也可以退而求其次,买个树莓派3B+来玩,哈哈. Step 1: Install Dependecies and Download the P ...

  5. 为ubuntu安装powerline记录

    C:\Users\win10的用户名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\L ...

  6. easyui-combobox和C标签判断回显

    <td width="40%"> <select class="easyui-combobox" id="work_property ...

  7. An error occurred while starting a transaction on the provider connection. See the inner exception for details.

    用EntityFramework循环操作数据时,报了如下错误 An error occurred while starting a transaction on the provider connec ...

  8. kotlin之map委托

    fun main(arg: Array<String>) { val map = mapOf("name" to "tom", ) val user ...

  9. MongoDB作为windows服务来安装-2

    首先区官网下载对应版本的安装文件,我本地的环境是win7 bit64 我下载的版本是:mongodb-win32-x86_64-2.4.6 ok, 文件下载后,开始安装,这里要说一下,如果直接启动Mo ...

  10. Django setting 常用配置

    setting.py           这个文件包含了所有有关这个Django项目的配置信息,均大写:   TEMPLATE_DIRS , DATABASE_NAME , 等. 最重要的设置是 RO ...