bzoj 1650: [Usaco2006 Dec]River Hopscotch Jumping Stones【Greedy + Two Points】

As soon as my brain draws and writes a heap, when I find it is wrong, I remember that it is the most worthwhile to use two points
. When judging, I greedily open up the interval that does not meet the mid, and see if the number of times of opening is less than or equal to m.

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=50005;
int L,n,m,a[N];
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
bool ok(int x)
{
    int tot=0,la=0;
    for(int i=1;i<=n;i++)
        if(a[i]-a[la]<x)
        {
            tot++;
            if(tot>m)
                return 0;
        }
        else 
            la=i;
    return 1;
}
int main()
{
    L=read(),n=read(),m=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    sort(a+1,a+n+1);
    a[++n]=L;
    int l=0,r=L,ans=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(ok(mid))
            ans=mid,l=mid+1;
        else 
            r=mid-1;
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325302350&siteId=291194637