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. 2018 Nowcoder Multi-University Training Contest 1

    Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdo ...

  2. 解决Virtualbox的根分区容量不够用问题

    现在Virtualbox新建一块磁盘.容量一定要比原来的大.然后执行克隆命令. 把原来的磁盘内容克隆到新磁盘上.然后重新启动电脑. 运行相关扩容命令即可. #克隆磁盘 cd C:\Program Fi ...

  3. Po类设计

    0.承接MySQL 表设计,同样地,这篇博客中一部分内容是Deolin的个人观点和习惯. 1.一般Po类的域是和DB表字段一一对应的, 而由于每个信息表和关联表都有id.insert_time.upd ...

  4. c++ 使用类生成随机数

    // generate algorithm example #include <iostream> // cout #include <algorithm> // genera ...

  5. 三十、CentOS 7之systemd

    一.系统启动流程 POST --> bootloader  --> MBR工作 --> kernel(initramfs/initrd) --> ro rootfs --> ...

  6. Echarts案例-柱状图

    一:先在官网下载 https://www.echartsjs.com/zh/download.html 然后再建立工程,导入这两个包: 写代码: <!DOCTYPE html> <h ...

  7. Python学习日记(八)—— 模块一(sys、os、hashlib、random、time、RE)

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  8. mysql 常见面试题

    附录: https://mp.weixin.qq.com/s/pC0_Y7M7BkoUmlRwneZZdA 一.为什么用自增列作为主键 1.如果我们定义了主键(PRIMARY KEY),那么InnoD ...

  9. pwn学习日记Day22 《程序员的自我修养》读书笔记

    知识杂项 软连接 命令: ln -s 原文件 目标文件 特征: 1.相当于windows的快捷方式 2.只是一个符号连接,所以软连接文件大小都很小 3.当运行软连接的时候,会根据连接指向找到真正的文件 ...

  10. RabbitMQ JAVA客户端调用例子

    1.安装erlang 下载地址:http://www.erlang.org/downloads 设置ERLANG环境变量 2.安装RabbitMQ 下载地址: http://www.rabbitmq. ...