标签:
http://poj.org/problem?id=2456
Description
Input
Output
Sample Input
5 3 1 2 8 4 9
Sample Output
3
Hi
/**
poj 2456 最小值最大化
题目大意:在编号为a0~an的n个点上放m个棋子要求如何防止能使距离最近的两个棋子的距离最大
解题思路:二分查找可行的值,贪心:对于每个可行值判断是否能在n个点中挑出m个。
贪心的时候应该注意,我们只要从第一个开始就可以了,如果找不出来那么以第二个以及更后的开始就更找不出来了,因此每次贪心每个点最多考虑
1次,复杂度O(n)
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=100003;
int a[maxn];
int n,c;
bool greed(int x)
{
int cn=0;
int p=a[0];
for (int i=1; i<n; i++)
{
if (a[i]>=p+x)
{
cn++;
p=a[i];
}
}
if (cn>=c-1)
return true ;
return false ;
}
int main()
{
while(~scanf("%d%d" ,&n,&c))
{
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int l=0,r=a[n-1],mid;
while (l<=r)
{
mid=(l+r)/2;
if(greed(mid))
l=mid+1;
else
r=mid-1;
}
printf("%d\n" ,l-1);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/lvshubao1314/article/details/45021165