Description
市政府在基地一和基地二之間修建了n個太空航站,其中基地一爲第1個航站,基地二爲第n個航站,其他航站在兩個基地之間。
我們把相鄰航站的距離定義爲“空間差”,顯然飛船的續航能力必須大於等於這個空間差才能在兩個航站間航行。
把所有相鄰航站間空間差的最大值定義爲“空間極差”,如果一艘飛船想從基地一航行至基地二,
那麼它的續航能力必須大於等於“空間極差”,這樣看能滿足航行條件的飛船太少了。
現在市政府打算在基地一和基地二之間新增最多K個太空航站,讓儘可能多的飛船可以在基地一和基地二之間航行。
簡單說就是讓所有航站間的“空間極差”儘可能地小。
輸入格式
第一行爲兩個整數n和k。(2=<n<=100000),(1=<k<=100)
第二行,n個正整數a1…an。(a1<=a2<=a3…<=an)
輸出格式
僅一行,新增最多K個太空航站後,“空間極差”的最小值。
輸入樣例
2 1
0 105
輸出樣例
53
提示
新的空間站建設在座標52或53可以得到最小空間極差。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=1e5+5;
int n,a[N],m;
int f(int x)
{
int num=0,s=a[0];
for(int i=1;i<n;i++)
{
if(a[i]-s>x)
{
s+=x;
i--;
num++;
if(num>m)
break;
}
else
s=a[i];
}
return num<=m;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=0;i<n;i++)
cin >> a[i];
int l=a[0],r=a[n-1]-a[0],mid,res;
while(l<=r)
{
mid=(l+r)>>1;
if(f(mid))
res=mid,r=mid-1;
else
l=mid+1;
}
cout << res << endl;
return 0;
}