hdu4004 二分搜索

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int l, n, m, a[500005];
bool calc(int x)
{
	int step = 0, cur = 0;
	for(int i = 0; i <= n; )
	{
		if( ++step>m || a[i]-cur>x )
			return false;
		while( i <= n && a[i]-cur <= x )
			i++;
		cur = a[i-1];
	}
	return true;
}
int main()
{
	while( scanf("%d%d%d", &l, &n, &m)!=EOF )
	{
		for(int i = 0; i<n; i++)
		{
			scanf("%d", a+i);
		}
		a[n] = l;
		sort(a, a+n);
		int mid, left = 0, r = l;
		while( r>left )
		{
			mid = (left+r) >> 1;
			if( calc(mid) )
				r = mid;
			else
				left = mid+1;
		}
		printf("%d\n", r);
	}
	return 0;
}



你可能感兴趣的:(hdu4004 二分搜索)