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;
else
l=mid+1;
}
ans=min(ans,l);///更新答案
}
}
printf("%d\n",ans);
}
推荐(二分板子题):
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;
else
return 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;
}
总结:
(看博主这么辛苦,不放点个赞呗)
二分的用处非常广泛,一个非常漂亮的算法,自我感觉如果涉及到最大值最小值,或者保留到某一精度问题,都不妨往二分想想,有时候会有意想不到的收获。
告诫:
能控制住自己的人才能控制未来。