CodeForces - 655C (2020.5.17训练B题)

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

题意:有n个房间,每个房间1代表满了,0代表空可住。有k个奶牛和一个人,房间在一维方向上线性排列,一牛或一人均可选择一个空房入住,求min(人离最远的牛的距离)

先用a[cnt]数组存储空房间的位置,然后要明确一点欲使值更小,则这k+1个对象必然是相连入住的,问题就转化为长为cnt的数组中一个长为k+1的滑动窗口,每个窗口需要输出一个该区间内的最小值,问题就又能简化为长为k+1的区间内min(人离最远的牛的距离),可以用二分法去寻找,nlogn不会超时

AC代码

#include<iostream>
#include<algorithm>
using namespace std;

int n, k;
char s[100005];
int a[100005]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值