-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13397.cpp
57 lines (42 loc) · 932 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
56
57
#include <bits/stdc++.h>
using namespace std;
int N, M;
bool isOk(vector<int> &v, int mid) {
int cnt = 1;
int minV, maxV;
minV = v[0];
maxV = v[0];
for(int i = 1; i < N; i++){
minV = min(minV, v[i]);
maxV = max(maxV, v[i]);
// 구간 점수 넘어가면 새로운 구간
if ((maxV - minV) > mid){
cnt++;
minV = v[i];
maxV = v[i];
}
}
// 구간 개수 M 이하이면 ok
return cnt <= M;
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N >> M;
vector<int> v(N);
for(int i = 0; i < N; i++)
cin >> v[i];
int right = *max_element(v.begin(), v.end());
int left = 0;
int res = right;
while(left <= right) {
// 구간 점수 최댓값
int mid = (left + right) / 2;
if(isOk(v, mid)) {
res = min(res, mid);
right = mid - 1;
}
else left = mid + 1;
}
cout << res << '\n';
return 0;
}