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: ??? ...
随机推荐
- flume采集MongoDB数据到Kafka中
环境说明 centos7(运行于vbox虚拟机) flume1.9.0(自定义了flume连接mongodb的source插件) jdk1.8 kafka(2.11) zookeeper(3.57) ...
- python 数据写入json文件时中文显示Unicode编码问题
一.问题描述 import json dir = { '春晓':'asfffa', '春眠不觉晓' : '处处闻啼鸟', '夜来风雨声' : 56789, 'asdga':'asdasda' } fp ...
- Centos7 安装 Keepalived
目标: Keeplaived 简单模拟测试一下Nginx 故障切换前言:C7 默认的 1.3.5 似乎有点问题,改装 keepalived-2.0.7 1:安装 Nginx 和确认 (略)2:安装配置 ...
- Docker系列——Grafana+Prometheus+Node-exporter钉钉推送(四)
近期搭建的服务器监控平台,来进行一个总结.主要分为监控平台的搭建.告警中心的配置以及消息的推送.推送的话,支持多种终端.具体详细可查看之前的博文,在这里罗列下,方便查看. Docker系列--Graf ...
- 必看!LuatOS自定义C库全新教程,一文极速上手
今天继续讲LuatOS的开发,上一期简单说了一下如何移植LuatOS,相信很多朋友已经看过了.那么今天,我就开始讲C和Lua调用的部分教程. LuatOS相关资料及Lua语言的官方定义,详见以下链接: ...
- GlusterFS更换Brick
故障环境还原 GlusterFS集群系统一共有4个节点,集群信息如下 # 分别在各个节点上配置hosts.同步好系统时间,关闭防火墙和selinux [root@glusterfs-master-8 ...
- 消失之物(背包DP)(容斥或分治)
容斥做法: 首先n^2搞出f[i][j]第i个物品,j体积的方案数. 去除每个物品贡献: 设个g[i][j]表示当i不选,j体积方案数(注意不是此时的范围相对于全局,而不是1---i) 那么我们用到一 ...
- IDEA2020.2版本设置类和方法的自定义注释模板
IDEA是目前普遍使用的Java开发编辑器,添加自定义的注释模板,一方面便捷好用,另外一方面可以规范开发.IDEA中设置模板分两种:1.创建Java类的注释,2.方法的注释. 一.Java类的注释模板 ...
- ES6中的Generator函数
今天小编发现一个es6中的新概念,同时也接触到了一个新关键字yeild,下面我就简单和大家聊聊es6中的generator函数.大家还可以关注我的微信公众号,蜗牛全栈. 一.函数声明:在functio ...
- Golang去除字符串前后空格
Golang去除字符串前后空格 实现Demo package main import "fmt" func DeletePreAndSufSpace(str string) str ...