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. VRRP协议的原理与配置

    VRRP出现的原因: 局域网中的用户终端通常采用配置一个默认网关的形式访问外部网络,如果此时默认网关设备发生故障,将中断所有用户终端的网络访问,这很可能会给用户带来不可预计的损失. VRRP的优点: ...

  2. Qt信号槽机制理解

    1. 信号和槽概述 > 信号槽是 Qt 框架引以为豪的机制之一.所谓信号槽,实际就是观察者模式(发布-订阅模式).当某个`事件`发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号(s ...

  3. 如何使用 Python 统计分析 access 日志?

    一.前言 性能场景中的业务模型建立是性能测试工作中非常重要的一部分.而在我们真实的项目中,业务模型跟线上的业务模型不一样的情况实在是太多了.原因可能多种多样,这些原因大大降低了性能测试的价值. 今天的 ...

  4. [非专业翻译] 高性能对象映射框架 - Mapster

    [非专业翻译] 高性能对象映射框架 - Mapster 系列介绍 [非专业翻译] 是对没有中文文档进行翻译的系列博客,文章由机翻和译者自己理解构成,和原文相比有所有不通,但意思基本一致. 因个人能力有 ...

  5. C#构造函数中:this()的作用

    通俗来说,可以说是构造函数的继承 (1) :this()用来继承无参时的构造函数,例如下面代码 static void Main(string[] args) { AA aA = new AA(&qu ...

  6. 学Java,找对圈子,跟对人

    我大学学的是机械专业,到大四才决定要学Java,以后当一名程序员. 到现在,十几年过去了,我现在已经是一家上市公司的技术总监了,管理的技术团队有100多人.很庆幸当初了选择了学Java. 还记得当初学 ...

  7. 37、linux下安装python3.6和django

    37.1.安装python: 1.python介绍: python是一种面向对象的,解释型的计算机语言,它的特点是语法简介,优雅,简单易学.1989年诞生,Guido(龟叔)开发. 编译型语言:代码在 ...

  8. Centos 8 误删/boot文件夹下文件后的恢复

    一.环境 Centos8 二.过程 1.查看/boot底下文件 ls /boot 2.模拟误删/boot底下文件 rm -rf /boot/*ls /boot 3.reboot,无法正常启动 4.在v ...

  9. AcWing 1282. 搜索关键词

    给定n个长度不超过50的由小写英文字母组成的单词,以及一篇长为m的文章. 请问,有多少个单词在文章中出现了. #include<bits/stdc++.h> using namespace ...

  10. Nginx:Nginx配置url重定向

    符号含义: 正则表达式匹配: ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配: -f和!-f用来判断是否存在文件 -d和!-d用 ...