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
- The $l$-th ball and the $l+N$-th ball must be of different colors.
- The $l$-th ball must be blue and the $l+N$-th ball must be red.
- 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的更多相关文章
- 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; ...
- IOCCC(The International Obfuscated C Code Contest)
国际 C 语言混乱代码大赛(IOCCC, The International Obfuscated C Code Contest)是一项国际编程赛事,从 1984 年开始,每年举办一次(1997年.1 ...
- 【AtCoder】CODE FESTIVAL 2016 qual A
CODE FESTIVAL 2016 qual A A - CODEFESTIVAL 2016 -- #include <bits/stdc++.h> #define fi first # ...
- 【AtCoder】CODE FESTIVAL 2016 qual B
CODE FESTIVAL 2016 qual B A - Signboard -- #include <bits/stdc++.h> #define fi first #define s ...
- 【AtCoder】CODE FESTIVAL 2016 qual C
CODE FESTIVAL 2016 qual C A - CF -- #include <bits/stdc++.h> #define fi first #define se secon ...
- CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】
CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...
- CODE FESTIVAL 2017 qual B C - 3 Steps【二分图】
CODE FESTIVAL 2017 qual B C - 3 Steps 题意:给定一个n个结点m条边的无向图,若两点间走三步可以到,那么两点间可以直接连一条边,已经有边的不能连,问一共最多能连多少 ...
- M-SOLUTIONS Programming Contest 2020 题解
M-SOLUTIONS Programming Contest 2020 题解 目录 M-SOLUTIONS Programming Contest 2020 题解 A - Kyu in AtCode ...
- Atcoder CODE FESTIVAL 2017 qual B D - 101 to 010 dp
题目链接 题意 对于一个\(01\)串,如果其中存在子串\(101\),则可以将它变成\(010\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...
随机推荐
- Spring Boot and Rabbit MQ 异常的时候消息的状态
我们有一个处理消息的方法. 在处理消息的时候出现了异常,那出现异常后这个消息会怎么处理呢. 根据我们的实际情况的观察,如果出现了异常. 但是你没有捕获或者处理异常,这个消息会一直存在,并且你的系统会持 ...
- Gym - 101955K Let the Flames Begin 约瑟夫环
Gym - 101955KLet the Flames Begin 说实话,没怎么搞懂,直接挂两博客. 小飞_Xiaofei的约瑟夫问题(Josephus Problem)3:谁最后一个出列 小飞_ ...
- Codevs 1038 一元三次方程求解 NOIP 2001(导数 牛顿迭代)
1038 一元三次方程求解 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 有形如:ax3+b ...
- Ubuntu 16.04 下Redis Cluster集群搭建
实际操作如下: 准备工作 版本:4.0.2 下载地址:https://redis.io/download 离线版本:(链接: https://pan.baidu.com/s/1bpwDtOr 密码: ...
- Luogu5339 [TJOI2019]唱、跳、rap和篮球 【生成函数,NTT】
当时看到这道题的时候我的脑子可能是这样的: My left brain has nothing right, and my right brain has nothing left. 总之,看到&qu ...
- mysql 进程
--------------------查看mysql所有进程-----------show full processlist -------------------查看mysql 账号权限----- ...
- mysql 运行中 偶尔 报错 2002 也许是这个问题,内存不足导致的
配置都是正常,最终发现内存使用率高达90%多,加配置后解决问题(升级服务器配置) 加之前是2核4g ,升级配置后 4核8g
- asyncio之异步上下文管理器
异步上下文管理器 前面文章我们提到了上下文管理器,但是这个上下文管理器只适用于同步代码,不能用于异步代码(async def形式),不过不用担心今天我们就来讨论在异步中如何使用上下文管理器. 特别提醒 ...
- 令人窒息的操作,nodejs面向对象。
// async函数返回一个 Promise 对象,可以使用then方法添加回调函数. // 当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句. clas ...
- 关于OpenCL中三重循环的执行次序
源自OpenGPU社区的一个帖子的讨论: 一个有意思的openCL问题