#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vt vector
#define nl " \n"
#define pb emplace_back
#define all(x) x.begin(), x.end()
template <class F, class S>
void chmin(F &a, S b) {
if(a > b) a = b;
}
template <class F, class S>
void chmax(F &a, S b) {
if(a < b) a = b;
}
void run_case() {
int n, k;
cin >> n >> k;
vt<int> a(n);
for(auto &i : a) {
cin >> i;
}
auto f = [&](int x) -> pair<int, int> {
int tot = 0, cnt = 1, j = 0, mx = 0;
for(int i = 0; i < n; i++) {
if(a[i] - a[j] > x) {
tot += mx;
mx = 0;
j = i;
cnt++;
}
chmax(mx, a[i] - a[j] + 1);
}
return {cnt, tot + mx};
};
int l = 0, r = *max_element(all(a));
while(l < r) {
int mid = (l + r) / 2;
if(f(mid).first <= k) {
r = mid;
} else {
l = mid + 1;
}
}
cout << f(r).second << nl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
// cin >> tt;
for(int i = 0; i < tt; i++) {
run_case();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |