codeforces 645C . Enduring Exodus 三分
题目链接
我们将所有为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 三分的更多相关文章
- CodeForces 645C Enduring Exodus
枚举,三分. 首先,这$n+1$个人一定是连续的放在一起的.可以枚举每一个起点$L$,然后就是在$[L,R]$中找到一个位置$p$,使得$p4最优,因为越往两边靠,距离就越大,在中间某位置取到最优解, ...
- Codeforces 645C Enduring Exodus【二分】
题目链接: http://codeforces.com/contest/645/problem/C 题意: 给定01串,将k头牛和农夫放进, 0表示可以放进,1表示不可放进,求农夫距离其牛的最大距离的 ...
- Code Forces 645C Enduring Exodus
C. Enduring Exodus time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...
- codeforces 655C C. Enduring Exodus(二分)
题目链接: C. Enduring Exodus time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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 ...
- Enduring Exodus CodeForces - 655C (二分)
链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...
- CodeForces - 645 C.Enduring Exodus
快乐二分 用前缀和随便搞一下 #include <cstdio> using namespace std; ; int p[N]; ; inline int msum(int a, int ...
- codeforces 626E. Simple Skewness 三分
题目链接 给n个数, 让你去掉一些数, 使得剩下的数的平均值-中位数的差值最大. 先将数组排序, 然后枚举每一个数作为中位数的情况, 对于每个枚举的数, 三分它的左右区间长度找到一个平均值最大的情况, ...
- Codeforces 8D Two Friends 三分+二分+计算几何
题目链接:点击打开链接 题意:点击打开链接 三分house到shop的距离,二分这条斜边到cinema的距离 #include<stdio.h> #include<string.h& ...
随机推荐
- 使用VS2003 发送Email
使用VS2003发送Email与之后VS2005版本及以上VS版本不一样,记录一下, 需要引用using System.Web.Mail; public void SendEmail() { try ...
- Jquery Mobile 记录
使用的是C#语言,.Net+Jquery Mobile 框架开发 1.使用水平组切换操作 <fieldset id="Tfdset1" data-role="con ...
- 第三方浏览器内核嵌入一、Crosswalk
本篇分为三部分: 介绍Crosswalk背景 介绍Crosswalk集成步骤 为了减小体积,仅集成兼容ARM的Crosswalk(针对X86同理) PART_A Crosswalk背景介绍 Web技术 ...
- Oracle User Management FAQ翻译及学习笔记
转载 最近了解到AME 的东西,很迫切,先转载一篇 [@more@] Oracle User Management FAQ翻译及学习笔记 写在前面 本文主要是翻译的英文版的Oracle User Ma ...
- Unicode解码、URL编码/解码
+ (NSString *) stringByReplaceUnicode:(NSString *)string { NSMutableString *convertedString = [strin ...
- AOP面试遇到的问题
1.什么是AOP? 面向切面的编程,找出纸和笔,画一个箭头,两道竖线将这个箭头砍断,这就是AOP 举例来说,某个方法正在运行呢,要想在前面加个日志,加在这里,后面加个日志,加在这里,前面加transa ...
- mysqli连接数据库函数
<?php $mysqli=@new mysqli("localhost", "root", "123456", "xsph ...
- python 网络编程第三版
为服务端增加多线程解决方案 1.服务端代码如下: ***这个版本并没有真正的起到多线程的作用,主要原因在于t.join():以后的版本会改进这个问题*** #!/usr/bin/python #!co ...
- sshd服务---暴力破解应对策略
sshd服务暴力破解步骤 sshd暴力破解方法 防止暴力破解调优 1. 变更默认端口 2. 变更root用户 3. 日志监控-->防止暴力破解(fail2ban应用) fail2ban详解 在初 ...
- 论山寨手机与Android联姻 【1】MTK亮相的历史背景
[1]MTK亮相的历史背景如果说1960年代是大型机(Mainframe)的时代,1970年代是小型机(Microcomputer)的时代,那么1980年代无疑是个人电脑(PC)的时代,而1990年代 ...