poj 3258River Hopscotch

本文探讨了一条长为l的河流中,0和l处各有石头,中间n个石头的情况下,去掉m个石头后,剩余石头之间的最小最大距离的计算方法。通过二分查找算法实现解决方案。

题意:一条长l的河,0和l各有一个石头,中间有n个石头,从中间n个石头中去掉m个石头,求所有剩余石头之间距离的最小的最大值

分析:二分

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int c[maxn];
int lo,n,m;

bool judge(int x){
    int t=0,last=0;
    for(int i=0;i<n;i++)
      if(c[i]-last>=x){
          t++;
          last=c[i];
          if(t==n-m)
            break;
      }
    return (t==(n-m))&&(lo-last>=x);
}

int main(){
    while(~scanf("%d%d%d",&lo,&n,&m)){
        for(int i=0;i<n;i++)
          scanf("%d",c+i);
        sort(c,c+n);
        int l=0,r=lo;
        while(r-l>0){
            int mid=l+(r-l+1)/2;
            if(judge(mid))
              l=mid;
            else
              r=mid-1;
        }
        printf("%d\n",l);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/jihe/p/5572168.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值