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的更多相关文章

  1. [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 ...

  2. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. Leetcode 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. Leetcode 55. Jump Game

    我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...

  6. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  7. Leetcode jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. Leetcode jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. 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: ??? ...

随机推荐

  1. fail2ban防护ssh免于暴力破解

    一.背景 开放到公网的主机无时无刻不在遭受攻击,其中ssh暴力破解频率最高,会有无数机器不分日夜地搜索公网上的猎物,然后进行弱密码尝试 如果你的公网机器恰巧设的弱密码,估计刚装上系统,没过几小时别人就 ...

  2. 技能篇:awk教程-linux命令

    前言 AWK是一门解释型的编程语言.用于文本处理,它的名字来源于它的三位作者的姓氏:Alfred Aho, Peter Weinberger 和 Brian Kernighan awk 程序结构 运行 ...

  3. 一文彻底理解Apache Hudi的多版本清理服务

    Apache Hudi提供了MVCC并发模型,保证写入端和读取端之间快照级别隔离.在本篇博客中我们将介绍如何配置来管理多个文件版本,此外还将讨论用户可使用的清理机制,以了解如何维护所需数量的旧文件版本 ...

  4. 【模板】关于vector的lower_bound和upper_bound以及vector基本用法 STL

    关于lower_bound和upper_bound 共同点 函数组成: 一个数组元素的地址(或者数组名来表示这个数组的首地址,用来表示这个数组的开头比较的元素的地址,不一定要是首地址,只是用于比较的& ...

  5. ST算法模板

    void work() { int t=log(n)/log(2); for(int j=1;j<=t;++j) { for(int i=1;i<=(n+1-(1<<j));+ ...

  6. 使用 TypeScript,React,ANTLR 和 Monaco Editor 创建一个自定义 Web 编辑器(二)

    译文来源 欢迎阅读如何使用 TypeScript, React, ANTLR4, Monaco Editor 创建一个自定义 Web 编辑器系列的第二章节, 在这之前建议您阅读使用 TypeScrip ...

  7. Windows10 上Docker 安装运行Gitlab

    准备条件 安装好Docker For Windows客户端. 配置好Docker 阿里云加速镜像地址. 检查Docker版本,大于等于v19. 拉取Gitlab镜像 docker pull gitla ...

  8. python之list列表(基础篇)

    特点:1.有序的 2.可以存放多个元素 3.每个元素可以是任何数据类型,4,通过下标值访问1,定义一个空列表  2,定义一个非空列表 3.访问列表中的元素(同str类型) 4,切片与步长(同str类型 ...

  9. This application failed to start because no Qt platform plugin could be initialized

    今天在直接运行QT生成的.exe遇到了一个错误:This application failed to start because no Qt platform plugin could be init ...

  10. 39、升级linux的内核

    39.1.什么是linux系统内核: 操作系统是一个用来和硬件打交道并为用户程序提供一个有限服务集的低级支撑软件.一个计算机 系统是一个硬件和软件的共生体,它们互相依赖,不可分割.计算机的硬件,含有外 ...