标签:sts john log inf hot ted lin names output
题目链接:https://vjudge.net/problem/343761/origin
参照博客:https://blog.csdn.net/qq_39060776/article/details/81302809
网上参照的这种解题方法叫做三分,它提到:二分是在有序序列找某值,而三分是在类似二次函数图像上找最值。
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 + 1characters 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
7 2
0100100
2
5 1
01010
2
3 2
000
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+1。枚举的时候二分区间,看看famer的最佳位置在哪(最开始是中点,后来不断往两边试探,就像给你一个开口向上的二元函数,给你一个最小值所在区间,让你找最值的位置所对应的函数值)
ac代码如下:
#include<stdio.h> #include<iostream> #include<stdlib.h> #include<math.h> #include<algorithm> using namespace std; const int N=1e5+7; const int inf=1e9+2; char s[N]; int a[N],k; int bis(int L,int R) { int mid,radius,ld,rd,le=L,ri=R; while(le<=ri) { mid=(le+ri)>>1;//中点 radius=max(a[R]-a[mid],a[mid]-a[L]);//中点函数值 if(mid-1>=L)ld=max(a[R]-a[mid-1],a[mid-1]-a[L]);//中点左移一个单位的函数值 else ld=radius; if(mid+1<=R)rd=max(a[R]-a[mid+1],a[mid+1]-a[L]);//中点右移一个单位的函数值 else rd=radius; if(radius<=ld&&radius<=rd)return radius;//如果左右函数值都比当前中点函数值大,那么radius就是最佳答案 else if(ld>=radius&&radius>rd)le=mid+1; else if(ld>=radius&&radius==rd)le=mid; else if(ld<radius&&radius<=rd)ri=mid-1; else if(ld==radius&&radius<=rd)ri=mid; } return radius; } int main() { int n,cnt=1; cin>>n>>k; scanf("%s",s+1);//字符串存起来地址为1开始 for(int i=1;i<=n;i++)//把空位存起来 { if(s[i]==‘0‘) { a[cnt++]=i; } } int ans=inf; for(int i=1;i+k<cnt;i++)//i列举起始地址i+k则包含k+1个人 { ans=min(ans,bis(i,i+k)); } cout<<ans<<endl; return 0; }
标签:sts john log inf hot ted lin names output
原文地址:https://www.cnblogs.com/Mingusu/p/11909273.html