River Hopscotch POJ - 3258 二分

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxx = 50000 + 500;
int ll,n,m;
int pos[maxx];
bool check(int num) //判断当最小距离为num的时候是否要拿走的石头多于M
{
	int cnt = 0;
	int last = 0;
	pos[0] = 0;
	pos[n+1] = ll;
	for(int i=1; i<=n+1; i++)
		//如果之间的距离,小于num了,就要把这个拿掉 
		if(pos[i]-pos[last]<num)
			cnt++;
		else
			last = i;
	//距离都大于等于m,要拿掉的块数, 
	if(cnt>m)
		return false;
	else 
		return true;
}
int main()
{
	while(cin>>ll>>n>>m)
	{
		for(int i=1; i<=n; i++)
			cin>>pos[i];
		sort(pos+1,pos+n+1);
		int l = 1,r = ll,mid = 0;
		while(l<r)
		{
			mid = (r+l+1)/2;
			if(check(mid))
				l = mid;
			else 
				r = mid - 1;
		}
		cout<<l<<endl;
	}
	return 0;
}
posted @ 2020-05-07 01:12  晴屿  阅读(93)  评论(0编辑  收藏  举报