hdu 4004 The Frog's Games 二分查找

来源:互联网 发布:淘宝店新店如何推广 编辑:程序博客网 时间:2024/06/05 21:17
#include<iostream>#include<algorithm>#define a_max 500002using namespace std;int a[a_max];int main(){    int n, m, i;    int l;    while(scanf("%d%d%d", &l, &n, &m) != EOF)    {        int count;        memset(a, 0, sizeof(a));        for( i=1; i <= n; i++ )            scanf("%d", &a[i]);        a[i] = l;        n += 2;        sort(a, a+n);        int first = 0, end, temp, p1, p2;        end = l;        while(end-first > 1)        {            bool p = false;            count = 0;            p1 = 0;            p2 = 1;            temp = (end+first) / 2;            while(p2 < n)            {                while(a[p2] - a[p1] <= temp && p2 < n)                    p2++;                if(a[p2] - a[p1] > temp && p2-p1 ==1)                {                    first = temp;                    p = true;                    break;                }                p1 = p2-1;                count++;            }            if(p == false)            {                if(count > m)                    first = temp;                else                 {                    end = temp;                }            }        }        printf("%d\n", end);            }    return 0;}

原创粉丝点击