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\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...
随机推荐
- robotframework出现错误:Keyword 'AppiumLibrary.Open Application' expected 1 to 2 non-keyword arguments,got 5.
robotframework官网: http://robotframework.org/#introduction -------------- 出现的场景: 由于一开始不了解robotframewo ...
- 数据结构实验之栈与队列三:后缀式求值(SDUT 2133)
题解:把每一步计算的答案再存在栈里面,直到计算结束. 如果是操作数 那么直接入栈:如果是运算符,那么把栈里面最顶部的两个操作数拿出来进行运算,运算结果再放入到栈里面,计算完所有的(#之前的长度位len ...
- CodeForces - 999C Alphabetic Removals
C - Alphabetic Removals ≤k≤n≤4⋅105) - the length of the string and the number of letters Polycarp wi ...
- NSArray 的创建和遍历
数组 用来存贮对象的有序列表,它是不可变的 不能存数C语言的基本数据类型 只支持OC对象 #pragma mark Create a array //Initialize NSArray void a ...
- 元祖(tuple)
元祖和列表几乎是一样的: 列表是可以进行修改的,它可以填加一个元素,也可以移除一个元素,但元祖是不能修改的 如果我们以后想导一个集合,这个集合以后不允许修改,我们用元祖:如何我们想让别人进行修改,我们 ...
- python项目生成及导入依赖的第三方库
requirements.txt用来记录项目所有的依赖包和版本号,只需要一个简单的pip命令就能完成. pip freeze >requirements.txt 然后就可以用 pip insta ...
- 《maven实战》笔记(2)----一个简单maven项目的搭建,测试和打包
参照<maven实战>在本地创建对应的基本项目helloworld,在本地完成后项目结构如下: 可以看到maven项目的骨架:src/main/java(javaz主代码)src/test ...
- jeecg中获取用户拥有的角色的数据权限
String roles1=""; String sql=""; //1.获取用户 TSUser user = ResourceUtil.getSessionU ...
- kotlin中抽象类
抽象类和接口很类似,抽象类不能被实例化需要使用abstract 关键字声明,抽象类实现接口后,接口中没有函数体的函数可以不重写,接口中的这些方法就自动被继承到实现接口的抽象类中,称为抽象方法 pack ...
- C之指针
什么是指针 * 指针变量:用来存储某种数据在内存中的地址.* 世面上书籍一般把指针和指针变量的概念混在一起了.市面上的书籍说的指针指的就是指针变量 Ø *号的三种含义1. 两个数相乘int i =5; ...