【二分查找】codeforces:Enduring Exodus

1 篇文章 0 订阅
二分查找:
(百度百科)——折半查找法也称为二分查找法,它充分利用了元素间的次序关系,采用分治策略,可在最坏的情况下用O(log n)完成搜索任务。它的基本思想是:(这里假设数组元素呈升序排列)将n个元素分成个数大致相同的两半,取a[n/2]与欲查找的x作比较,如果x=a[n/2]则找到x,算法终止;如 果x<a[n/2],则我们只要在数组a的左半部继续搜索x;如果x>a[n/2],则我们只要在数组a的右 半部继续搜索x。
题目:

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
inputCopy
7 2
0100100
outputCopy
2
inputCopy
5 1
01010
outputCopy
2
inputCopy
3 2
000
outputCopy
1

题目大意:

给你房间的数量和牛的数量,只能住0的房间,农夫也要住,问农夫离距他最远的牛的距离。

思路:

既然距离最小则农夫肯定住在开始牛和结束牛距离的中间,如果距离为偶数就两个中间也就是max1,max2。如果为奇数只有一个中间max1。二分用来找农夫的位置。

二分查找农夫位置:

首先bb[]中存的是空房间的位置,每次都枚举m+1个空房间(因为最近时农夫肯定和他的牛在只考虑空房间时都挨着), 将m+1个空房间的bb[]起始位置和终止位置求出中间(这时包含1房间的影响),而我们要的是0房间,所以就看最大的<=num(num为包含1房间的中间位置)的0房间(此时二分查找),也就是最接近中间位置的空房作为农夫的房间。

0x3f3f3f3f是作为无穷大来使用,为什么推荐选用0x7f7f7f7f呢(32位int最大值)?

因为出现无穷大+无穷大的情况时,两个0x7f7f7f7f相加就超了,结果为负数
而0x3f3f3f3f则是,无穷大+无穷大=无穷大。
设置无穷大的操作为: memset(aa,0x3f,sizeof(aa));

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int aa[100001];
int bb[100001];                     //存空房间位置
int ans;                            //空房间数量
int max1;
int minn=0x3f3f3f3f;
int max2=0x3f3f3f3f;
int erfen(int num)                   //二分查找
{
    int res=-1;
    int ll=0;
    int rr=ans-1;
        while(ll<=rr)
        {
            int mid2=(ll+rr)/2;
            if(bb[mid2]<=num)
            {
                res=mid2;
                ll=mid2+1;
            }
            else
                rr=mid2-1;
        }
    return res;
}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%1d",&aa[i]);
        if(aa[i]==0)
        {
            bb[ans++]=i;
        }
    }
    m+=1;
   // cout<<m<<endl;
    for(int i=0;i+m-1<=ans-1;i++)
    {
        int l=bb[i];
        int r=bb[i+m-1];
        int mid=(l+r)/2;
        int pos=erfen(mid);
        max1=max(abs(bb[pos]-bb[i]),abs(bb[i+m-1]-bb[pos]));
        if((pos+1)<ans)
            max2=max(abs(bb[pos+1]-bb[i]),abs(bb[i+m-1]-bb[pos+1]));
        int t=min(max1,max2);
       // cout<<max1<<" "<<max2<<endl;
        minn=min(minn,t);

    }
    printf("%d\n",minn);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值