HDU 4004 The Frog's Games
来源:互联网 发布:租赁备案 知乎 编辑:程序博客网 时间:2024/05/16 10:09
/*-------------------------------------------------------------- stratege : 二分查找 + 贪心 time :2012.2.8 20:43 -- 21: 23 URL : http://acm.hdu.edu.cn/showproblem.php?pid=4004***************************************************************** Problem : 4004 ( The Frog's Games ) Judge Status : Accepted RunId : 5330923 Language : G++ Author : a312745658*****************************************************************--------------------------------------------------------------*/#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std ;#define MAXN 500005int L, n, m ;int a[MAXN], dist[MAXN] ;int cmp (const void *a, const void *b){ return * (int *)a - * (int *)b ;}int success (int jump) //把长度作为折半查找的尺度{ int i, j, cnt ; if (jump * m < L) return false ; i = 1 ; j = 0 ; cnt = 0 ; while (i <= n + 1) { cnt ++ ; //统计需要跳的次数 if (jump < a[i] - a[j]) return false ; while (jump >= a[i] - a[j] && i <= n + 1) //此时正在跳过i--j之间的石头距离 i ++ ; //a[i]-a[j] 表示2块石头之间的距离 j = i - 1 ; //状态更新,作为下次的起跳地点 } if (cnt > m) //超过规定的次数 return false ; return true ;}int main(){ int i, j, k ; int max, min, mid ; while (scanf ("%d%d%d", &L, &n, &m) != EOF) { a[0] = 0 ; //数组a[],记录石头所在位置,需进行排序 for (i = 1; i <= n; i ++) scanf ("%d", &a[i]) ; a[n+1] = L ; qsort (a, n+2, sizeof(a[0]), cmp) ; max = L ; min = 0 ; while (min <= max) { mid = (max + min) / 2 ; if (success (mid)) max = mid - 1 ; else min = mid + 1 ; } printf ("%d\n", min) ; } return 0 ;}