[HDU] [4004] The Frog's Games

Problem Description

Loading...

Problem Analysis

Because it is convenient to compare
whether mid is greater than res
(by calculating the number of stones that need to be skipped for mid)
so we can perform Binary Search on the answer

cpp
 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;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...