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. 51nod 1086

    https://www.cnblogs.com/TnT2333333/p/6879709.html 二进制优化多重背包 怎么用二进制优化多重背包,举一个例子就明白了. 如果要放n个苹果,可以将n个苹果 ...

  2. 深入理解D3D9

    本文写的较为深入,故转载在此留以备案,呵呵~ 原文链接为:http://www.cnblogs.com/effulgent/archive/2009/02/10/1387438.html ------ ...

  3. pom.xml报错 : Missing artifact org.apache.shiro:shiro-spring:bundle:1.2.5

    添加有<type>bundle</type>标签的依赖时,都会报这个错. 需要在<build/><plugins/>里面追加标签 <plugin& ...

  4. 2015-2016 ACM ICPC Baltic Selection Contest

    这是上礼拜三的训练赛,以前做过一次,这次仅剩B题没补.题目链接:https://vjudge.net/contest/153192#overview. A题,水题. C题,树形DP,其实是一个贪心问题 ...

  5. 【spring源码分析】IOC容器初始化——查漏补缺(三)

    前言:本文分析InitializingBean和init-method方法,其实该知识点在AbstractAutowireCapableBeanFactory#initializeBean方法中有所提 ...

  6. python中使用requests模块的post()函数时形参 data和json的区别

    通常,你想要发送一些编码为表单形式的数据--非常像一个 HTML 表单.要实现这个,只需简单地传递一个字典给 data 参数.你的数据字典在发出请求时会自动编码为表单形式: >>> ...

  7. CPU、io、mem之间的关系

    https://blog.csdn.net/weixin_38250126/article/details/83412749 https://blog.csdn.net/joeyon1985/arti ...

  8. [转][C#]无法创建虚拟目录。ASP.NET 2.0 尚未在 Web服务器上注册。

    参考:http://www.bubuko.com/infodetail-997666.html C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet ...

  9. SD卡状态监听(无序广播)

    import android.content.BroadcastReceiver; import android.content.Context; import android.content.Int ...

  10. Objective-C中的self与LLVM Clang新引入的instancetype

    我们知道,大部分面向对象语言对于一个类的成员方法都有一个隐含的参数.在C++.Java.C#和JavaScript中是this,而在Objective-C中则是self.当然,由于Objective- ...