码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4004(二分)

时间:2014-11-25 01:33:52      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004

 

大致题意 二分最大跳跃能力,判断是否可以在m次内到达对岸!

 

分析:由于求青蛙最小弹跳能力,所以二分不断枚举能力x,再对x判断是否在m次内到达对岸。。。

 

 

bubuko.com,布布扣
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define inf 1<<30
using namespace std;

int n,m,l,a[500010];
int judge(int x)
{
    int res=0,temp=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]-temp>x)return 0;
        while(a[i]-temp<=x&&i<=n)i++;
        temp=a[i-1];i--;
        res++;
    }
    return res<=m;
}
int main()
{
    while(scanf("%d%d%d",&l,&n,&m)>0)
    {
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        a[++n]=l;
        sort(a+1,a+n+1);
        int left=0,right=l,mid,ans;
        while(left<=right)
        {
            mid=(left+right)/2;
            if(judge(mid))
                right=mid-1,ans=mid;
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
}
View Code

 

hdu4004(二分)

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/lienus/p/4120014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!