BZOJ 1650 [Usaco2006 Dec]River Hopscotch 跳石子

2017.09.14

题目大意

给你一个数轴上的一堆点,问你任意两石子距离最小值的最大值。


最小值最大……这不就是二分嘛qwq

然后写了一发结果不会贪心策略了……直接对于每一个石子前面不行就搬走,类似这样子就可以了。

#include <cstdio>
#include <algorithm>
using namespace std;
int l,n,m,pos[50010];
bool check(int tx)
{
    int ret=0,i=1,j=1;
    while(i<n)
    {
        ++j;
        while(j<=n&&pos[j]-pos[i]<tx) j++,ret++;
        i=j;
    }
    return ret<=m;
}
int bisection()
{
    int l=0,r=1000000000,mid;
    while(l<r)
    {
        mid=(l+r)>>1;
        if(check(mid)) l=mid+1;
        else r=mid;
    }
    return l-1;
}
int main()
{
    scanf("%d%d%d",&l,&n,&m);
    for(int i=2;i<=n+1;++i) scanf("%d",pos+i);
    pos[n+2]=l;
    n+=2;
    sort(pos+1,pos+n+1);
    printf("%d\n",bisection());
    return 0;
}