hdu 4004 二分查找

直接二分查找答案即可,我的判断函数没有像大牛们那样优化,但是过是没问题的~

/*
* hdu4004/linux.cpp
* Created on: 2011-9-4
* Author : ben
*/
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
#include
<algorithm>
using namespace std;

const int MAXN = 500010;
int stone[MAXN], L, N, M;

int step(int dis) {
int ret, cur, next;
next
= ret = cur = 0;
while (cur <= N) {
next
= cur + 1;
while (next <= N + 1 && stone[next] - stone[cur] <= dis) {
next
++;
}
if (cur == next - 1) {
return 0x7fffffff;
}
cur
= next - 1;
ret
++;
}
return ret;
}

void work();
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

void work() {
int mid, high, low, ans;
while (scanf("%d%d%d", &L, &N, &M) == 3) {
stone[
0] = 0;
for (int i = 1; i <= N; i++) {
scanf(
"%d", &stone[i]);
}
stone[N
+ 1] = L;
sort(stone, stone
+ N + 2);
low
= 0;
high
= L;
while (low <= high) {
mid
= (low + high) / 2;
if (step(mid) > M) {
low
= mid + 1;
}
else {
high
= mid - 1;
ans
= mid;
}
}
printf(
"%d\n", ans);
}
}
posted @ 2011-09-04 19:49  moonbay  阅读(430)  评论(0编辑  收藏  举报