poj3258 (二分之最大化最小值)

链接:https://cn.vjudge.net/problem/POJ-3258

题意:给定n块石头,第i块石头距离起点的距离为di,最多可以去掉m块石头,求问怎样操作可以让每块石头间距离最小

#include
#include
#include
#include
using namespace std;
typedef long long ll;

ll l,n,m,a[50050],inf = 1e9+1;

int solve(ll mid){
	ll last = 0,cnt = last+1,num = 0;
		while(cnt <= n+1){
			cnt = last+1;
		while(cnt <= n+1&&a[cnt]-a[last] < mid){
			//cout< m){
				return 0;
			}
		}
		last = cnt;
	}
	return 1;
}

int main(){
	cin>>l>>n>>m;
	for(int i = 1;i<=n;++i)
	cin>>a[i];
	a[0] = 0;a[n+1] = l;
	sort(a,a+n+1);
	ll lb = 0,ub = inf;
	while(ub-lb > 1){
		ll mid = (ub+lb)/2;
		if(solve(mid) == 1) lb = mid;
		else ub = mid;
	}
	cout<

 

你可能感兴趣的:(二分法)