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\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...
随机推荐
- I Hate It (HDU 1754)
Problem 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师 ...
- 爬虫之python3用execjs执行JS代码
JS运行环境 Node.js 是Javascript语言服务器端运行环境 安装和配置:https://www.runoob.com/nodejs/nodejs-install-setup.html 测 ...
- vue的一些随笔
一.点击路由后的样式,可以在路由文件index.js中设置 再在样式里面设置active的类名对应的样式. ———————————————————————————————————————————— 二 ...
- slax linux的定制
由于数据结构教学的需要,需要用到linux,要求就是小,启动快,可定制性强,恰好slax正好满足要求,以下就是定制slax linux的过程记录: 什么是Slax Slax是一个基于Linux的Liv ...
- nginx 配置简单反向代理
假设端口号是 3000 server { listen ; server_name your.domain; location / { proxy_pass http://127.0.0.1:3000 ...
- OAuth2.0的四种授权模式
1.什么是OAuth2 OAuth(开放授权)是一个开放标准,允许用户授权第三方移动应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方移动应用或分享他们数据的所有内容,OA ...
- Flutter点击两次返回键退出APP
在APP中一些页面为了防止用户操作失误点击到返回键导致退出APP,可以设置其一定时间内点击两次返回键才允许退出APP,完成这个功能可以通过WillPopScope和SystemNavigator.po ...
- jQuery源码解读----part 1
来源:慕课网 https://www.imooc.com/video/4392 jQuery整体架构 jQuery按我的理解分为五大块,选择器.DOM操作.事件.AJAX与动画, 那么为什么有13个模 ...
- SQL-W3School-基础:SQL ORDER BY 子句
ylbtech-SQL-W3School-基础:SQL ORDER BY 子句 1.返回顶部 1. ORDER BY 语句用于对结果集进行排序. ORDER BY 语句 ORDER BY 语句用于根据 ...
- 思科常见的防病毒ACL
SW(config-ext-nacl)# 10 deny tcp any any eq 27665 SW(config-ext-nacl)# 20 deny tcp any any eq 16660 ...