poj 2456

二分搜索

#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 100005;
const int INF = 1000000005;

int n, m;
int l[MAXN];

bool C(int d) {
	int last = 0;
	for(int i=1; i<m; i++) {
		int crt = last + 1;
		while(crt<n && (l[crt]-l[last])<d)
			crt++;
		if(crt == n)	return false;
		last = crt;
	}
	return true;
}

int main() {
	
	scanf("%d%d", &n, &m);
	for(int i=0; i<n; i++)
		scanf("%d", &l[i]);
		
	sort(l, l+n);
		
	int lowBound = 0;
	int upperBound = INF;
	while((upperBound - lowBound) > 1) {
		int middle = (lowBound + upperBound) / 2;
		if(C(middle))	lowBound = middle;
		else		upperBound = middle;
	}
	printf("%d\n", lowBound);
	
	return 0;
}


posted @ 2016-09-29 12:22  StevenLuke  阅读(154)  评论(0编辑  收藏  举报