# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
208708 | tushar_2658 | Stove (JOI18_stove) | C++14 | 5 ms | 376 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
const int maxn = 100005;
using ll = long long;
ll a[maxn];
int n, k;
ll mn = 1e18;
bool can(ll x){
ll ret = 0;
int cnt = 0;
for(int i = 1; i <= n;){
ll l = a[i];
int j = i + 1;
while(j <= n && a[j] - l + 1 <= x){
++j;
}
ret += a[j - 1] - l + 1;
cnt++;
i = j;
}
mn = ret;
return (cnt <= k);
}
int main(int argc, char const *argv[])
{
// freopen("in.txt", "r", stdin);
scanf("%d %d", &n, &k);
for(int i = 1; i <= n; i++){
scanf("%lld", &a[i]);
}
sort(a + 1, a + n + 1);
ll lo = 1, hi = 1e18, ans = -1;
while(lo <= hi){
ll mid = (lo + hi) >> 1;
if(can(mid)){
ans = mid;
hi = mid - 1;
}else {
lo = mid + 1;
}
}
can(ans);
cout << mn << endl;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |