题目链接

我们将所有为0的位置的下标存起来。 然后我们枚举左端点i, 那么i+k就是右端点。 然后我们三分John的位置, 找到下标为i时的最小值。

复杂度 $ O(nlogn) $

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
int a[maxn], k;
int cal(int x, int pos) {
return max(abs(a[x]-a[pos]), abs(a[pos+k]-a[x]));
}
int main()
{
int n, cnt = 0;
string s;
cin>>n>>k;
cin>>s;
for(int i = 0; i<n; i++) {
if(s[i] == '0')
a[cnt++] = i;
}
int ans = inf;
for(int i = 0; i < cnt - k; i++) {
int l = i, r = i + k;
while(l < r) {
int lmid = (l*2+r)/3;
int rmid = (l+r*2+2)/3;
if(cal(lmid, i) > cal(rmid, i)) {
l = lmid + 1;
} else {
r = rmid - 1;
}
}
ans = min(ans, cal(l, i));
}
cout<<ans<<endl; return 0;
}

codeforces 645C . Enduring Exodus 三分的更多相关文章

  1. CodeForces 645C Enduring Exodus

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

  2. Codeforces 645C Enduring Exodus【二分】

    题目链接: http://codeforces.com/contest/645/problem/C 题意: 给定01串,将k头牛和农夫放进, 0表示可以放进,1表示不可放进,求农夫距离其牛的最大距离的 ...

  3. Code Forces 645C Enduring Exodus

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

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

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

  5. 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 ...

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

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

  7. CodeForces - 645 C.Enduring Exodus

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

  8. codeforces 626E. Simple Skewness 三分

    题目链接 给n个数, 让你去掉一些数, 使得剩下的数的平均值-中位数的差值最大. 先将数组排序, 然后枚举每一个数作为中位数的情况, 对于每个枚举的数, 三分它的左右区间长度找到一个平均值最大的情况, ...

  9. Codeforces 8D Two Friends 三分+二分+计算几何

    题目链接:点击打开链接 题意:点击打开链接 三分house到shop的距离,二分这条斜边到cinema的距离 #include<stdio.h> #include<string.h& ...

随机推荐

  1. 详解Linq to SQL

    第一部分,什么是Linq to sql Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并 ...

  2. C# 8 函数 调用 常用类 时间 日期型

    函数:能够独立完成某个功能的模块. 好处:1.结构更清析(编写.维护方便 ).2.代码重用.3.分工开发. 四要素:名称,输入(参数),输出(返回的类型),加工(函数体) 语法: 返回类型 函数名(参 ...

  3. WPF那些事儿

    概述 感觉学习的东西必须做个记录,不然很快就忘掉了.现在把WPF学习过程中一些零碎的东西记录在下面,没有具体的主题,想到啥.看到啥都写在这里,算是复习一下并做个备忘吧. 1. 等待对话框 看到同事做的 ...

  4. 一个周末掌握IT前沿技术之node.js篇

    一个周末掌握IT前沿技术之node.js篇 http://ittechnical.sinaapp.com/node-js-and-restful-api/ NodeJS入门  http://www.n ...

  5. EC读书笔记系列之19:条款49、50、51、52

    条款49 了解new-handler的行为 记住: ★set_new_handler允许客户指定一个函数,在内存分配无法获得满足时被调用 ★Nothrow new是一个颇为局限的工具,∵其只适用于内存 ...

  6. Hive的安装配置

    Hive的安装配置 Hive的安装配置 安装前准备 下载Hive版本1.2.1: 1.[root@iZ28gvqe4biZ ~]# wget http://mirror.bit.edu.cn/apac ...

  7. Scala基础入门-1

    首先需要Scala开发环境的搭建,网上自己找教程. 声明常量与变量 val foo = 0 // 常量 var bar = 0 // 变量 在Scala中,更加鼓励使用val来进行声明,也就是推荐使用 ...

  8. jchat:linux聊天程序1:简介

    做一个linux的聊天软件,虽然没什么创意,但是它可以用来锻炼和测试我对网络编程的掌握程度,也借此机会做一些有意思的程序. 这里做的是linux下一个命令行的客户端与服务器的聊天程序,没写界面,因为对 ...

  9. (极简)linux安装QT4.7.3

    1.下载QT http://download.qt.io/ 我用的:qt-everywhere-opensource-src-4.7.3.tar.gz 2.解压安装QT 建议解压到/use/local ...

  10. SQL SERVER 2008 架构

    架构: 一个容器 包含表,视图,数据库对象等等. 相当于命名空间 如何创建一个架构: 1. 图形向导 2.命令 create schema 在sqlserver 2005中,可能大家在工作或学习的时候 ...