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& ...
随机推荐
- JQuery(一)——初步总结
JQuery其实早就看过相关的视频了,但是由于当时查的资料太少,理解的不够深刻,所以没有进行总结,这次回过头来又学习了,心里感觉理解了很多,因为BS的其它知识基本学习完了,而且有了一定程度的理解.相互 ...
- 用友U8客户端连接不上服务器全攻略
用友U8客户端连接不上服务器全攻略 http://www.enet.com.cn2009年09月23日09:26 来自论坛 [导读]:如果网络不通,就让用户查找网络原因 检查步骤: 1.网络是否通? ...
- Qt中实现菜单和工具栏功能
Qt创建菜单和工具栏: 一. temp.h文件的内容 1.定义类temp继承QMainWindow 2.添加Q_OBJECT , 构造函数 . 析构函数等内容 3.$重点内容 3.1定义QActio ...
- Molecule to atoms
For a given chemical formula represented by a string, count the number of atoms of each element cont ...
- Java文件IO操作应该抛弃File拥抱Path和Files
Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream;import java.nio.file.FileSystem; ...
- MYSQL 关闭二进制日志
方法 set sql_log_bin = 0; ---------------------------------------------------------------------------- ...
- 去掉ILDasm的SuppressIldasmAttribute限制
原文:去掉ILDasm的SuppressIldasmAttribute限制 今天本打算汉化一个.Net程序的,当用ILDasm打开的时候,出现了"受保护模块—无法进行反汇编"的错误 ...
- zookeeper 手动T掉已挂节点
zjtest7-redis:/root/zk# cat test_zk.pl use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; use IO ...
- Linux进程间通信——使用消息队列
下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信——使用命名管道 一.什么是消息队列 消息队列提 ...
- linux中ctrl+z、ctrl+d和ctrl+c的区别
ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.ctrl+c是强制中断程序的执行,而ctrl+z的是将任务中断,但是此任务并没有结束,他仍然在进程中他只是维持挂起的状态,用户可以使用f ...