Enduring Exodus

戳一戳-》原题链接

原题

B. Enduring Exodus
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In an attempt to escape the Mischievous Mess Makers’ antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of n rooms located in a row, some of which are occupied.

Farmer John wants to book a set of k + 1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms i and j is defined as |j - i|. Help Farmer John protect his cows by calculating this minimum possible distance.

Input
The first line of the input contains two integers n and k (1 ≤ k < n ≤ 100 000) — the number of rooms in the hotel and the number of cows travelling with Farmer John.

The second line contains a string of length n describing the rooms. The i-th character of the string will be ‘0’ if the i-th room is free, and ‘1’ if the i-th room is occupied. It is guaranteed that at least k + 1 characters of this string are ‘0’, so there exists at least one possible choice of k + 1 rooms for Farmer John and his cows to stay in.

Output
Print the minimum possible distance between Farmer John’s room and his farthest cow.

Examples
input
7 2
0100100
output
2
input
5 1
01010
output
2
input
3 2
000
output
1
Note
In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.

In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2.

In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1.

题意:

就是说一个人有k头牛,需要住有n个房间的宾馆(这个宾馆为一行排列),一头牛住一间,一个人住一间,给你一个字符串,1表示该房间已经有人住,0表示空房间。怎样住可以让那个人离牛的最远距离最短,求出该最短距离。

思路:

由于最短距离这个关键字眼,很多巨巨看了都会会心一笑,二分就完了,(巨巨NB,orz)。没错就是二分,先用一个数组存一下前缀零的个数,然后枚举人的房间位置,然后二分查找合适区间,并不断对答案进行更新,取答案的最小值,然后输出。(完事,哇好开心啊,又水了一道题,来自蒟蒻的自嗨)

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
char s[maxn];
int n,k,sum[maxn];
int check(int p,int dis)///二分中的判断函数
{int ll=max(1,p-dis);int rr=min(n,p+dis);return (sum[rr]-sum[ll-1])>=k+1;///如果当前0的个数不大于k+1,那么范围需要l(不是此处的ll)增大到mid+1,反之r更新到mid
}
int main()
{
scanf("%d%d",&n,&k);
scanf("%s",s+1);
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+(s[i]=='0');///存储一下零的个数用来对区间二分。
int ans=n;
for(int i=1;i<=n;i++)
{if(s[i]=='0'){int l=1;int r=n;while(l<r){int mid=(l+r)>>1;///除二(这样写更快一点啊)if(check(i,mid))r=mid;elsel=mid+1;}ans=min(ans,l);///更新答案}}
printf("%d\n",ans);
}

推荐(二分板子题):

落谷-p1577切绳子

AC代码(二分板子):
//洛谷p1577
//运用二分暴力(一位一位的尝试测试样例只跑了26次(可见二分的优秀))找出答案
//精度问题可以直接乘使它变成整数。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1e5+7;
int arr[maxn];
int n;
int k;
bool judge(int x)
{int sum=0;for(int i=0;i<n;i++){sum+=arr[i]/x;}if(sum>=k)return 1;elsereturn 0;
}
int main()
{scanf("%d%d",&n,&k);double temp;for(int i=0;i<n;i++){scanf("%lf",&temp);arr[i]=(int)(temp*100);//(int)temp*100!=(int)(temp*100);优先级不一样}int l=0,r=1e8;while(l<=r){int mid=l+((r-l))/2;//防止爆数组;if(!mid)//l=0,r=0 防止死循环break;cnt++;if(judge(mid)){l=mid+1;}else{r=mid-1;}}printf("%.2f",r/100.0);return 0;
}

总结:

(看博主这么辛苦,不放点个赞呗)
二分的用处非常广泛,一个非常漂亮的算法,自我感觉如果涉及到最大值最小值,或者保留到某一精度问题,都不妨往二分想想,有时候会有意想不到的收获。

告诫:

能控制住自己的人才能控制未来。

codeforce-Enduring Exodus(二分真好用)相关推荐

  1. 二分 - Enduring Exodus - CodeForces - 655C

    二分 - Enduring Exodus - CodeForces - 655C 题意: 首 行 输 入 包 括 两 个 整 数 n 和 m , 分 别 为 房 间 的 数 量 和 牛 的 数 量 . ...

  2. CF645C Enduring Exodus 题解

    题目:CF645C Enduring Exodus 双指针 - 单调队列 - 二分 这题有很多做法,单调队列/双指针+二分 这里讲一个最简单的,既不用双指针也不用单调队列 首先,输入时我们把可以住的房 ...

  3. 【二分查找】codeforces:Enduring Exodus

    二分查找: (百度百科)--折半查找法也称为二分查找法,它充分利用了元素间的次序关系,采用分治策略,可在最坏的情况下用O(log n)完成搜索任务.它的基本思想是:(这里假设数组元素呈升序排列)将n个 ...

  4. CodeForces - 645C Enduring Exodus(二分)

    题目链接:http://codeforces.com/problemset/problem/645/C 题意:给你n个房间, 0代表空房子, 1代表非空的房子, 一个农夫和他的k个牛要住进空房子里面, ...

  5. Enduring Exodus

    思路 题意:对于连续房间,选择k+1个房间给1个人和k个牛住,求人离牛的最大距离最小是多少 方法:枚举人的位置,然后二分即可 代码 #include <cstring> #include ...

  6. CodeForces 655C Enduring Exodus (三分)

    题意:n个房间,k头牛和1个人,有的房间已经被占了,要求选择k+1个房间,使得人住的房间离最远的牛距离最短. 题解:三分 题目给出01字符串,只考虑距离,先把0的编号提取到单独的数组中,由于人到牛的距 ...

  7. CodeForces 645C Enduring Exodus

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

  8. cf-Enduring Exodus

     Enduring Exodus 参考自点击打开链接 题目点击打开链接 题意:     一个农夫带着k头牛去住店,(人一间,每牛一间)已知该旅店共有n间房,其中部分房间已有人住,房间住宿情况由01串表 ...

  9. CodeForce 237C Primes on Interval(二分+ 素数筛法)

    题目链接:http://codeforces.com/problemset/problem/237/C Primes on Interval time limit per test 1 second ...

最新文章

  1. 站在巨人的肩膀上“思考”问题,重在思考而不是拿来主义
  2. R语言使用ggplot2包的快速可视化函数qplot绘制分组直方图(分组颜色设置)实战
  3. C++之抽象基类与纯虚函数
  4. VTK:PolyData之ImplicitModeller
  5. 新工具上线!只需2步助你轻松学爬虫!
  6. 小程序·云开发实战 - 校园约拍小程序
  7. python面向对象三大特性_深入理解Python面向对象的三大特性
  8. python contextlib closing
  9. u盘检测工具哪个好用_惠普打印机哪个型号好 惠普打印机型号介绍【推荐】
  10. python学习笔记(二)之列表
  11. 可供创业团队技术开发参考的10条经验
  12. 变量太多太复杂该怎么得出结论?——SPSS因子分析操作的详细讲解与介绍
  13. 硕士论文查重原理与快速通过的七大方法(转载)
  14. layui请求加token_琴海森林 JFinal-layui 文档、资料、学习、API,token验证
  15. HTML5之游戏DEMO - Yorhom's Game Box
  16. sklearn中的决策树(回归)
  17. kl变换简单的解释入门_2010年20个最佳入门指南解释者主题
  18. C语言程序设计课程设计题目[2023-02-11]
  19. Spring Boot自定义注解+AOP实现日志记录
  20. 【java】求平均值

热门文章

  1. java面向对象之多态
  2. 游戏链改公司 企业链改方案 链改费用
  3. 小Q大数据-Hbase数据模型的“旋风之旅”
  4. 因为一个人,厌倦一座城
  5. 祖国万岁 母校千秋
  6. 我们都是大数据时代的海狸
  7. 联想23亿美元收购IBM X86服务器硬件及服务
  8. 支付宝微信推出高速收费新服务器,高速收费站为什么不可以用支付宝或微信支付?...
  9. VUE根据出生日期显示年龄
  10. 合振动的初相位推导_合振动的初相位确定方法.doc