Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)
<题目链接>
题目大意:
有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的 Hamming distance ,现在要求你按照步骤进行交互式操作,最终得到任意一个0、1的下标。
解题分析:
因为原串中至少存在一个0和一个1,所以一定存在一个01或者10序列,因此我们可以用二分来寻找这个序列(注意二分过程中选择区间的操作)。二分之后,一定能够得到01或10序列,然后将其按先0后1的顺序输出即可。
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 6 int main(){ 7 ios::sync_with_stdio(false); 8 int n,num;cin>>n; 9 puts("?"); 10 for(int i=1;i<=n;i++)cout<<"0"; 11 puts(""); 12 cin>>num; //第一次交互,先得到序列中1的个数 13 int l=1,r=n; 14 while(r-l>=2){ //二分寻找01或10串 15 int mid=(l+r)>>1; 16 puts("?"); 17 for(int i=1;i<=n;i++){ //这里的区间判定方法很难想 18 if(i>=l && i<=mid)cout<<"1"; 19 else cout<<"0"; 20 }puts(""); 21 int x1;cin>>x1; 22 if(abs(num-x1)==(mid-l+1))l=mid; //判断左区间是否全为0或全为1,因为我们需要查找的是01串,所以需要向含有01串的地方收敛 23 else r=mid; 24 } 25 //得到了10或01串的位置后,判断其中0、1的位置 26 puts("?"); 27 for(int i=1;i<=n;i++){ 28 if(i==l)cout<<"1"; 29 else cout<<"0"; 30 }puts(""); 31 int x2;cin>>x2; 32 if(x2<num) printf("! %d %d\n",r,l); //先输出0的位置 33 else printf("! %d %d\n",l,r); 34 }
2019-02-01
转载于:https://www.cnblogs.com/00isok/p/10346149.html
Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)相关推荐
- D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)
http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 1 #include <cstd ...
- Codeforces 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- Codeforces.959E.Mahmoud and Ehab and the xor-MST(思路)
题目链接 \(Description\) 有一张\(n\)个点的完全图,从\(0\)到\(n-1\)标号,每两点\(i,j\)间的边权为\(i\oplus j\).求其最小生成树边权之和. \(Sol ...
- Codeforces round #628 C.Ehab and Path-etic MEXs
Codeforces round #628 C.Ehab and Path-etic MEXs You are given a tree consisting of n nodes. You want ...
- Binary String Reconstruction CodeForces - 1352F(思维+构造)
For some binary string s (i.e. each character si is either '0' or '1'), all pairs of consecutive (ad ...
- Binary String Minimizing CodeForces - 1256D(贪心)
You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1'). I ...
- D1. Kirk and a Binary String (easy version)
题目链接:http://codeforces.com/contest/1204/problem/D1 D1. Kirk and a Binary String (easy version) time ...
- 题解 CF1399D 【Binary String To Subsequences】
题目链接:http://codeforces.com/contest/1399/problem/D 题目描述: You are given a binary string s consisting o ...
- Juju and Binary String(前缀和)
Juju and Binary String [Link](Problem - F - Codeforces) 题意 给你一个010101串strstrstr长度为nnn,这个串的权定为onesn(o ...
最新文章
- linux共享内存通信方式,linux下进程通信方式--共享内存
- 用双向链表实现一个栈
- [svc]数字证书基础知识
- IOS UITextView自适应高度
- 三维空间中曲线绕任意轴旋转所得的旋转曲面求法
- 深圳市南山区学计算机的视频课程,新手学电脑全套视频教程(1-27集)
- JNI的方式调用DLL(SO)(上)
- html宽度自动填充,html – 创建一个td填充表宽度
- 常见位操作及运算应用举例:1,C语言中位运算符异或“∧”的作用​​​​​​​2,异或运算的作用3,(与运算)、|(或运算)、^(异或运算)
- 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单等相关问题...
- 【马来西亚】娘惹的含义
- Html5变量游戏,第三章语法、关键保留字及变量
- Kafka学习之(四)PHP操作Kafka
- 东南大学c++非电类期末试卷 程序设计与算法语言,挑战程序设计竞赛2:算法和数据结构 中文完整pdf版[55MB] 附源码...
- activex控件无法安装解决方法
- python如何速成_怎样速成python?
- linux3.6.7在ok6410平台的移植3,ok6410移植uboot2012.10一直在Starting kernel
- linux安装包apr安装说明,Linux下安装Apr及其Apr-util的基本步骤
- 当当网按关键字搜索商品教程
- exchange发信流程
热门文章
- python高级语法装饰器_Python高级编程——装饰器Decorator超详细讲解上
- linux用户操作的日志,linux 用户操作记录并录入日志
- php getbyid,ThinkPHP查询中的魔术方法简述
- vs mysql iss_MySQL5.7与8.0的连接问题(vs2015\2017)
- mysql导入创建表空间_oracle创建表空间 用户 数据库导入和导出(转)
- dell增强保护套装还原失效_汕头长安欧尚汽车音响改装升级,还原真实音色
- 详解CSS选择器、优先级与匹配原理
- java uuid静态方法_Java UUID getLeastSignificantBits()方法与示例
- source insight使用教程
- 购物车的收货地址js php,Javascript实现购物车功能的详细代码