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: ??? ...
随机推荐
- eclipse 新建项目不可选择Java Project 解决方法
解决方法一: 鼠标点击file-new-other,弹出选项框,选中java project,点击next,接下来就是正常创建java protect的流程了,这个虽然也可以解决,但每次新建java项 ...
- jmeter的参数化实现
背景: 在实际的测试工作中,我们经常需要对多组不同的输入数据,进行同样的测试操作步骤,以验证我们的软件的功能.这种测试方式在业界称为数据驱动测试,而在实际测试工作中,测试工具中实现不同数据输入的过程称 ...
- Django基础之视图层
内容概要 小白必会三板斧 request对象方法初识 form表单上传文件 Jsonresponse FBV与CBV 内容详细 1 小白必会三板斧 HttpResponse render redire ...
- SpringCloud(1)生态与简绍
一:微服务架构简绍学习目标 1.技术架构的演变,怎么一步步到微服务的:2.什么是微服务,优点与缺点 :3.SOA(面向服务)与MicroServices(微服务)的区别 :4.Dubbo 与Spri ...
- NOIP模拟测试3「序列·熟练剖分·建造游乐园(play)」
---恢复内容开始--- 序列 刚调出来样例就A了,假装是水题. 因为是乱序,我们要求出来每两项之间最小公比,而不是直接比 求出来每两项之间最小公比,然后扫一遍就完了.(还要注意重复情况) 那么问题就 ...
- linux基础(电脑基本原理)
1.计算机体系结构:运算器 控制器 存储器 输入设备 输出设备 详解:存储即内存:编址的存储单元.即每一个存储单元在都有一个编址. 控制器告诉运算器加数在存储器的哪个存储单元. POST: ...
- Linux网络命令与脚本使用
作为系统管理员,经常需要诊断和解决网络问题,而配置.监控与保护网络有助于发现问题并在事情范围扩大前得意解决,并且网络的性能与安全也是管理与诊断网络的重要部分.这里总结一下常用与Linux网络管理的命令 ...
- Java安全之Weblogic内存马
Java安全之Weblogic内存马 0x00 前言 发现网上大部分大部分weblogic工具都是基于RMI绑定实例回显,但这种方式有个弊端,在Weblogic JNDI树里面能将打入的RMI后门查看 ...
- external-provisioner源码分析(2)-main方法与Leader选举分析
更多ceph-csi其他源码分析,请查看下面这篇博文:kubernetes ceph-csi分析目录导航 external-provisioner源码分析(2)-main方法与Leader选举分析 本 ...
- JUnit5编写基本测试
JUnit5的测试不是通过名称,而是通过注解来标识的. 测试类与方法 Test Class:测试类,必须包含至少一个test方法,包括: 最外层的class static member class @ ...