题目链接:

http://codeforces.com/contest/645/problem/C

题意:

给定01串,将k头牛和农夫放进, 0表示可以放进,1表示不可放进,求农夫距离其牛的最大距离的最小值。

分析:

第一遍读题没看清,直接写成dp。。。然后样例都不过,我开始怀疑人生怀疑自己。。。。。

后来发现是要求中间的到两边的最大距离的最小值,而对于某个距离是否满足条件很好判断啊~~所以直接二分最大距离即可~

代码:

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 5;
int t[maxn];
int n, m;
string s;
bool judge(int dist)
{
for(int i = 0; i < n; i++){
if(s[i] == '1') continue;
int l = max(0, i - dist );
int r = min(i + dist, n - 1);
if(t[r] - t[l - 1] >= m) return true;
}
return false;
}
int main (void)
{
cin>>n>>m>>s;
m++;
for(int i = 0; i < n; i++){
t[i] = t[i - 1] + (s[i] == '0');
} int lb = 0, rb = n;
while(lb < rb - 1 ){
int mid = (lb + rb + 1) / 2;
if(judge(mid)) rb = mid;
else lb = mid;
}
cout<<rb<<endl;
return 0;
}

很多时候不会做或者做错,或许只是因为读错了题。要相信自己,然后耐心的重新读题。

Codeforces 645C Enduring Exodus【二分】的更多相关文章

  1. codeforces 645C . Enduring Exodus 三分

    题目链接 我们将所有为0的位置的下标存起来. 然后我们枚举左端点i, 那么i+k就是右端点. 然后我们三分John的位置, 找到下标为i时的最小值. 复杂度 $ O(nlogn) $ #include ...

  2. CodeForces 645C Enduring Exodus

    枚举,三分. 首先,这$n+1$个人一定是连续的放在一起的.可以枚举每一个起点$L$,然后就是在$[L,R]$中找到一个位置$p$,使得$p4最优,因为越往两边靠,距离就越大,在中间某位置取到最优解, ...

  3. codeforces 655C C. Enduring Exodus(二分)

    题目链接: C. Enduring Exodus time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. CROC 2016 - Elimination Round (Rated Unofficial Edition) C. Enduring Exodus 二分

    C. Enduring Exodus 题目连接: http://www.codeforces.com/contest/655/problem/C Description In an attempt t ...

  5. Code Forces 645C Enduring Exodus

    C. Enduring Exodus time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...

  6. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  7. Enduring Exodus CodeForces - 655C (二分)

    链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...

  8. CodeForces - 645 C.Enduring Exodus

    快乐二分 用前缀和随便搞一下 #include <cstdio> using namespace std; ; int p[N]; ; inline int msum(int a, int ...

  9. CodeForces 670D1 暴力或二分

    今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1   This problem is given in two versions that diff ...

随机推荐

  1. java 对象流的简单使用

    对象的输入输出流的作用: 用于写入对象 的信息和读取对象的信息. 使得对象持久化.   ObjectInputStream   : 对象输入流   ObjectOutPutStream  :对象输出流 ...

  2. 微软2017年预科生计划在线编程笔试 A Legendary Items

    思路: 获得第i(i = 0, 1, ..., n - 1)件物品的概率仅由公式p / (1 << i)决定,所以获得这i件物品之间是相互独立的.迭代计算获得所有i件物品的期望再求和即可. ...

  3. 51全志R58平台Android4.4下Camera的HAL层修改

    51全志R58平台Android4.4下Camera的HAL层修改 2018/11/7 15:20 版本:V1.0 开发板:SC5806 1.系统编译: (略) 2.全志R58平台Android4.4 ...

  4. swiper移动端下不能正常轮播的解决方案-----此坑没躺过估计很难找到正确姿势

    <script> var mySwiper = new Swiper('.swiper-container', { direction: 'vertical', //horizontal横 ...

  5. 虚拟机centOs Linux与Windows之间的文件传输

    一.配置环境 虚拟机Linux:Fedora 9 文件传输工具:SSHSecureShellClient-3.2.9 二.实现步骤 1. 在Windows中安装文件传输工具SSHSecureShell ...

  6. java web 学习笔记 - servlet01

    ---恢复内容开始--- 1.Servlet介绍 Servlet 是用java语言编写的服务器端小程序,属于一个CGI程序,但与传统的CGI不同的是,它是多线程实现的,并且可以多平台移植. 用户自定义 ...

  7. Matlab基础之单元数组和结构数组

    Matlab基础之单元数组和结构数组 前言: 单元数组和结构数组是一种新的数据类型,能将不同类型.不同维数的数组组合在一起,从而方便对不同的数据类型方便管理和维护. 如上图所示的2*2矩阵中,分别存储 ...

  8. flask的基本搭建

    from flask import Flask app = Flask(__name__) @app.route("/")def index(): return "ok& ...

  9. Goldengate完成Mysql到Mysql的数据同步

    文档参考地址:http://blog.csdn.net/u010587433/article/details/49305019 需求: 使用Goldengate完成Mysql到Mysql的数据同步,源 ...

  10. D1. Toy Train (Simplified)

    D1. Toy Train (Simplified) time limit per test 2 seconds memory limit per test 256 megabytes input s ...