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. 5.3.3 自定义writable和RawComparatorWritable

    5.3.3 自定义writable (1)构造员工writable Hadoop虽然已经实现了一些非常有用的Writable,而且你可以使用他们的组合做很多事情,但是如果你想构造一些更加复杂的结果,你 ...

  2. Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤

    Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤 一.在ubuntu下尝试 1. 在虚拟机VWware Workstation 12. ...

  3. .net core 修改 Identity/AspNetUsers 数据库

    众所周知,.net core有一套完整的用户管理功能.使用它就能实现用户的管理及登录登出功能.现在问题来了,我们有时候需要添加一些字段,该怎么办呢?当然是修改他呀.修改方法参考链接:https://m ...

  4. 黑马vue---59-60、组件中的data和methods

    黑马vue---59-60.组件中的data和methods 一.总结 一句话总结: 1. 组件可以有自己的 data 数据 2. 组件的 data 和 实例的 data 有点不一样,实例中的 dat ...

  5. 性能分析 | 线上CPU100%排查

    不知道在大家面试中,有没有遇到这个问题: 生产服务器上部署了几个java程序,突然出现了CPU100%的异常告警,你如何定位出问题呢? 这个问题分为两版回答! 高调版 对不起,我是做研发的,这个问题在 ...

  6. 如何丧心病狂的使用python爬虫读小说

    写在前边 其实一直想入门python很久了,慕课网啊,菜鸟教程啊python的基础的知识被我翻了很多遍了,但是一直没有什么实践.刚好,这两天被别人一直安利一本小说<我可能修的是假仙>,还在 ...

  7. VUE数组操作方法的局限

    1.不能通过索引值直接设置一个项: vm.items[indexOfItem] = newValue 但是可以用set方法设置: Vue.set(example1.items,indexOfItem, ...

  8. Linux学习:Makefile简介及模板

    一.基本概念介绍: Makefile 文件就是告诉make命令需要怎么样的去编译和链接程序. 编写Makefile的基本规则: 1.如果这个工程没有编译过,那么我们的所有C文件都要编译并被链接. 2. ...

  9. Connection

    作用: * 获取执行sql语句对象 ** createStatement(): 获取Statement对象 ** prepareStatement(String sql): 获取预处理对象 ** pr ...

  10. JAVA 基础编程练习题24 【程序 24 根据输入求输出】

    24 [程序 24 根据输入求输出] 题目:给一个不多于 5 位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. package cskaoyan; public class cskaoya ...