POJ 2456(二分+最大化最小值)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#include<iomanip>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const double eps=1e-8;
const double PI = acos(-1.0);
int n,x,a[100005];
bool jud(int g)
{
ll sum=1,temp=a[0];
for(int i=1;i<n;i++)
{
if(a[i]>=temp+g)
{
temp=a[i];
sum++;
}
}
return sum>=x;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
while(cin>>n>>x)
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
ll l=1,r=1000000000,ans=1;
while(r>=l)
{
ll mid=(l+r)>>1;
if(jud(mid))
{
l=mid+1;
ans = mid;
}
else
{
r=mid-1;
}
}
cout<<ans<<endl;
}
return 0;
}