0%

[題解]基地台

a141. 基地台

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
#define ll long long
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define N 50005
using namespace std;
int n,k,arr[N];

//確認直徑可否覆蓋全部(greedy)
bool check(int sum){
int max_cover = arr[0]+sum;
int cnt=1;
for(int i=1;i<n;i++){
if(max_cover < arr[i]){
max_cover = arr[i]+sum;
cnt++;
}
}
return (cnt<=k);
}

void solve(){
cin>>n>>k;
for(int i=0;i<n;i++)cin>>arr[i];
sort(arr,arr+n);
int l = 1,r = ceil((arr[n-1]-arr[0])/k);
//區間[l,r]二分搜尋直徑
while(l<r){
int mid = (l+r)/2;
if(check(mid))r = mid;
else l = mid+1;
}
cout<<l<<endl;
}

signed main(){
Orz;
int t;t = 1;
while(t--){
solve();
}
}