【杭电】[4004]The Frog's Games

文章字数:166

问题描述

问题分析

因为可以方便比较
mid是否比res大
(通过计算mid下跳过石头所需的次数)
所以可以对答案进行二分查找

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[500200];
int L,n,m;
bool judge(int x) {
    int cnt=0,t=0;
    for(int i=1; i<=n; i++) {
        if(a[i]-a[i-1]>x)
            return false;
        if(a[i]-t>x) {
            cnt++;
            t=a[--i];
        }
    }
    if(cnt+1<=m)
        return true;
    else
        return false;
}
int main() {
    while(scanf("%d %d %d",&L,&n,&m)!=EOF) {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        a[0]=0,a[++n]=L;
        sort(a,a+n+1);
        int l=0,r=L,res;
        while(l<=r) {
            int mid=(l+r)/2;
            if(judge(mid)) {
                res=mid;
                r=mid-1;
            } else
                l=mid+1;
        }
        printf("%d\n",res);
    }
    return 0;
}

该内容采用 CC BY-NC-SA 4.0 许可协议。

如果对您有帮助或存在意见建议,欢迎在下方评论交流。

加载中...