codeforces 645C . Enduring Exodus 三分

时间:2022-09-13 23:36:31

题目链接

我们将所有为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&period; Enduring Exodus&lpar;二分&rpar;

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

  5. CROC 2016 - Elimination Round &lpar;Rated Unofficial Edition&rpar; C&period; Enduring Exodus 二分

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

  6. Enduring Exodus CodeForces - 655C &lpar;二分&rpar;

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

  7. CodeForces - 645 C&period;Enduring Exodus

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

  8. codeforces 626E&period; Simple Skewness 三分

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

  9. Codeforces 8D Two Friends 三分&plus;二分&plus;计算几何

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

随机推荐

  1. Picture Segmentation Using A Recursive Region Splitting Method

    首先 区域分割的分割方法 首先得到一个区域,(一般已原图作为第一个区域) 然后得到该区域的直方图,选取最合适的峰值作为阈值,然后选择连接区域,然后把这些区域加入下一步要处理的区域中,然后重复第一步,直 ...

  2. EditText 监听回车事件 避免2次触发

    // 侦听回车事件 EidtText txtSN = (EditText) findViewById(R.id.txtSN); txtSN.setOnEditorActionListener(new ...

  3. dos文件批量转换成unix文件

    对于经常在windows环境下和linux环境同时使用的文件(如在windows系统下编写,在linux环境下编译的文件), 常常存在这样的问题:由于两种系统的格式文件格式不同,导致程序出现不期望的问 ...

  4. ZOJ3772 - Calculate the Function&lpar;线段树&plus;矩阵&rpar;

    题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...

  5. solr总结 第六部分:solr查询语法

    1.基本查询语法 q:全文查询.schema.xml里面定义了如下两块.eg q=ibm即表示org_name或者org_weisite里面出现ibm的document都可以被匹配到.KeyWords ...

  6. ANI功能分析

    1 ANI ANI(Adapt Noise Immunity)就是基于CCK错包率,和/或CCK错包率,自动调整抗扰等级,从而提高或降低灵敏度,达到提高整体性能的目标. 2 关键常量 firstep_ ...

  7. mysql查找某连续字段中断的编号

    查询dj_pxlb表中zwh 空缺的值 select model2.zwh-1 as kqzwh from (select model1.zwh from dj_pxlb as model1 wher ...

  8. js数组之可变函数

    在js的数组中有两个方法为数组添加元素:1.push();2.unshift(),push函数是将元素添加到数组的末尾,现在不用说大家估计也能猜出来,unshift这个函数就是把元素添加到数组的开头的 ...

  9. 洛谷 P1939 【模板】矩阵加速(数列)

    题目描述 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项对1000000007(10^9+7)取余的值. 输入输出格式 输入格式: 第一行一 ...

  10. flume-ng script should first try finding java from PATH and then try using bigtop&comma; instead of vice-versa

    [FLUME-1154] flume-ng script should first try finding java from PATH and then try using bigtop, inst ...