P6982 [NEERC2015]Jump
P6982 [NEERC2015]Jump
题意
给你一个未知的 01 串,每次可以输出询问一个 01 串,如果该串中正确的个数刚好等于 \(n\) 或者 \(n/2\) ,将会返回相应的答案,否则会返回 0 。求出这个串。(询问次数不大于 \(n+500\) )
思路
先无视询问次数,我们来想一下确定性算法怎么做。
第一步,我们来试着找出 \(n/2\) 正确的串。
首先,我们设一个全 0 串,每次修改最左边的 0 为 1,在这至多 \(n\) 次询问中,我们一定能找到一个有 \(n/2\) 位正确的串。
- 正确性证明:假设全 0 时有小于 \(n/2\) 位正确,那么最糟情况,也就是变成全 1 时一定有多于 \(n/2\) 位正确;反之亦然。我们每次只改变一位的正确性,也就是说每次正确的位数只会改变 1,这样在移动的过程中一定会有一个情况恰好 \(n/2\) 位正确。
第二步,我们来找到正确的串。
我们固定一个位置,每次询问将该位置和其他一个位置取反。显然:若返回的答案为 \(n/2\) ,那么说明固定位置和这个位置的正确性是相反的。我们这样询问固定位置和其他每一个位置,就能够得到包含所有位置的两个正确性相反的集合。然后,我们将这个得到的 01 串和取反后的串询问,找到正确的输出即可。
于是我们得到一个询问次数为 \(2n\) 的确定性算法。
过不了。怎么办呢?不要伤心,不要心急!然后我们发现第一步我们随机选择的正确率是挺高的。询问499次,每次询问有 \(\frac{\tbinom{\frac{n}{2}}{n}}{2^n}\) 的几率询问到 \(n/2\) 正确的串,询问499次后,发现这个几率非常大,用电脑算出来是 \(0.99997\) ……于是我们就做完了。
实现
记得清空缓冲区。下面的代码使用了阴间的bitset实现,常数挺大。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cstring>
#include<cmath>
#include<bitset>
#include<cstdlib>
#include<ctime>
using namespace std;
inline int read(){
int w=0,x=0;char c=getchar();
while(!isdigit(c))w|=c=='-',c=getchar();
while(isdigit(c))x=x*10+(c^48),c=getchar();
return w?-x:x;
}
namespace star
{
int n,ans;
bitset<1002> a,b;
inline void write(bitset<1002>& x){
for(int i=0;i<n;i++) cout<<x[i];
cout<<endl;
}
inline void work(){
srand(time(0));
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=499;i++){
for(int j=0;j<n;j++) a[j]=rand()%2;
write(a);
cin>>ans;
if(ans==n)return;
else if(ans==n/2)break;
}
a[0]=a[0]^1;
for(int i=1;i<n;i++){
a[i]=a[i]^1;
write(a);
cin>>ans;
b[i]=a[i]^(ans==n/2);
a[i]=a[i]^1;
}
b[0]=a[0];
write(b);
cin>>ans;
if(ans==n)return;
b.flip();
write(b);
cin>>ans;
}
}
signed main(){
star::work();
return 0;
}
P6982 [NEERC2015]Jump的更多相关文章
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 55. Jump Game
我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- bug report: Jump to the invalid address stated on the next line at 0x0: ???
gdb或者vlagrind报告: ==14569== Jump to the invalid address stated on the next line ==14569== at 0x0: ??? ...
随机推荐
- 单点突破:Spring(上)
Spring概述 我们常说的 Spring 实际上是指 Spring Framework,而 Spring Framework 只是 Spring 家族中的一个分支而已.Spring 是为了解决企 ...
- SpringBoot(1)-新手入门(详细教程+理解)
前话:很多人刚学java没多久就开始学springboot,毕竟springboot屏蔽了很多框架的配置,导致搭建一个项目变得比以前简单很多.但建议还是先把基础的框架都熟悉一遍,再用springboo ...
- 【模拟8.01】big(trie树)
一道trie树的好题 首先我们发现后手对x的操作就是将x左移一位,溢出位在末尾补全 那么我们也可以理解为现将初值进行该操作,再将前i个元素异或和进行操作,与上等同. 那么我们等于转化了问题: ...
- 卢卡斯定理&&中国剩余定理
卢卡斯定理(模数较小,且是质数) 式子C(m,n)=C(m/p,n/p)*C(m%p,n%p)%p 至于证明(我也不会QAQ,只要记住公式也该就好了). 同时卢卡斯定理一般用于组合数取模上 1.首先当 ...
- leetcode5697. 检查二进制字符串字段
5697. 检查二进制字符串字段给你一个二进制字符串 s ,该字符串 不含前导零 . 如果 s 最多包含 一个由连续的 '1' 组成的字段 ,返回 true .否则,返回 false . 示例 ...
- 安卓开发(3)—1— Activity
安卓开发(3)-1- Activity 3.1 Activity是什么: 在前面安卓概述中有提到,Activity是Android开发中的四大组件,所有在app里可以看到的东西都是Activity里面 ...
- iOS-block本质是什么?
一: block的原理是怎样的?本质是什么? block本质上也是一个OC对象,因为它的内部也有个isa指针 block是封装了函数调用以及函数调用环境的OC对象 接下来我们将通过底层源码来论证上诉两 ...
- excel函数提取内容中的汉字
RIGHT(A2,LENB(A2)-LEN(A2)) 函数LENB将每个汉字(双字节字符)的字符数按2计数,LEN函数则对所有的字符都按1计数.因此"LENB(A2)-LEN(A2)&quo ...
- 7、Oracle通过客户端(sqlplus)登录认证用户的方式
select version from v$instance; #查看当前数据库的版本 192.168.31.5:1521/orcl 7.1.操作系统认证: 1.Oracle认为操作系统用户是可靠的, ...
- Jenkins之搭建部署
一.部署环境 操作系统:Centos7 软件: apache-tomcat-9.0.48--地址:https://tomcat.apache.org/download-90.cgi jdk-8u291 ...