自爆魂

博客园 首页 新随笔 联系 订阅 管理

http://acm.hdu.edu.cn/showproblem.php?pid=4004

一条线段长度为L,线段上有n个点,最多选取 m-1 个点,使得包括线段端点在内的相邻点之间的最大距离值最小。

最大值最小化问题,显然二分

judge每次选最远点,选出点数不超过m即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
#include<queue>
using namespace std;

int st[500010],len,n,m,i,j,maxx,fuck,x,flag,left,right,mid;
bool judge(int cao)
{
    int dis = 0, count = 1, i;
    if(st[0] - dis > cao)
        return 0;
    for(i = 1; i <= n; i++)
    {
        if(st[i] - dis > cao)
        {
            dis = st[i-1];
            if(st[i]-dis > cao)
                return 0;
            count++;
        }
    }
    if(count > m)
        return 0;
    return 1;
}

int main()
{
    while(~scanf("%d%d%d",&len,&n,&m))
    {
    memset(st,0,sizeof(st));
    for(i = 1;i <= n;i++) scanf("%d",&st[i]);
    sort(st,st+n+1);
    n++;
    fuck = n - m;

    st[n] = len;
    int left = 0,right = len;
    while(left < right)
    {
        mid = (left + right)/2;
        if(judge(mid)) right = mid;
        else left = mid+1;
    }
    printf("%d\n",right);
    }
    return 0;
}


posted on 2014-10-19 15:08  自爆魂  阅读(148)  评论(0编辑  收藏  举报