hdu 4004 二分查找
生活随笔
收集整理的這篇文章主要介紹了
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);
}
}
轉載于:https://www.cnblogs.com/moonbay/archive/2011/09/04/2166648.html
總結
以上是生活随笔為你收集整理的hdu 4004 二分查找的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不错的linux下通用的java程序启动
- 下一篇: hdu 3788 字符串