-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13397.cpp
55 lines (48 loc) · 1014 Bytes
/
13397.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Created by kdongha on 2020/02/20.
//
#include <bits/stdc++.h>
using namespace std;
int N, M;
int arr[5000];
bool divide(int val) {
int low = arr[0];
int high = arr[0];
int count = 1;
for (int i = 1; i < N; i++) {
if (low + val < arr[i] || high - val > arr[i]) {
count++;
low = arr[i];
high = arr[i];
} else {
low = min(low, arr[i]);
high = max(high, arr[i]);
}
}
return count <= M;
}
void solve() {
cin >> N >> M;
int low = 0, high = 10000;
for (int i = 0; i < N; i++) {
cin >> arr[i];
low = min(low, arr[i]);
high = max(high, arr[i]);
}
while (low <= high) {
int mid = low + (high - low) / 2;
if (divide(mid)) {
high = mid - 1;
} else {
low = mid + 1;
}
}
cout << low;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}