您的位置:首页 > 其它

POJ 3258(二分求最大化最小值)

2017-03-30 16:56 405 查看
题目链接:http://poj.org/problem?id=3258

题目大意是求删除哪M块石头之后似的石头之间的最短距离最大。

这道题目感觉大致代码写起来不算困难,难点在于边界处理上。我思考边界思考许久,还是没有弄明白为什么这样写正确,另外的写法就不对。

已知的问题数据是:

12 5 4

2 4 6 8 10


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 50000 + 15;
int A[maxn];
int L, N, M;
bool C(int k){
int del = 0;
int last = 0;
for(int i = 1; i <= N + 1; i++){
if(abs(A[i] - A[last])<=k){
del++;
}
else
last = i;
}
//cout << k << " " << del << endl;
return del > M;
}
int solve(int l, int r){
while(l <= r){
int m = (l + r) >> 1;
if(C(m))r = m - 1;
else l = m + 1;
}
/*
2 4 6 8 10
12 1
*/
return l;
}
int main(){
cin >> L >> N >> M;
A[0] = 0;
for(int i = 1; i <= N; i++){
cin >> A[i];
}
A[N+1] = L;
sort(A, A + N + 2);
cout << solve(0, L) << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: