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>
#include "boxes.h"
using namespace std;
using ll = long long;
using ld = long double;
const ll INF = 1e18;
ll delivery(int n, int k, int l, int p[]) {
vector<ll> dp1(n+1), dp2(n+2);
ll ans = INF;
for(int i = 1; i <= n; i++) {
int last = max(0, i-k);
ll lastP = (last ? p[last-1] : 0);
ll lastP1 = (i-1 ? p[i-2] : 0);
dp1[i] = dp1[last] + lastP*2 + p[i-1]-lastP;
}
for(int i = n; i >= 1; i--) {
int last = min(n+1, i+k);
ll lastP = (last != n+1 ? (l-p[last]) : 0);
ll lastP1 = (last != n+1 ? p[last] : l);
dp2[i] = dp2[last] + 2*lastP + lastP1-p[i-1];
}
for(int i = 0; i <= n; i++) {
ll add1 = 0, add2 = 0;
if(i) add1 = min((ll)p[i-1], (ll)l-p[i-1]);
if(i != n) add2 = min(p[i], l-p[i]);
ans = min(ans, add1+add2+dp1[i]+dp2[i+1]);
}
return ans;
}
/*
int main()
{
int arr[5] = {1,2,5};
cout << delivery(3, 2, 8, arr);
}*/
Compilation message (stderr)
boxes.cpp: In function 'll delivery(int, int, int, int*)':
boxes.cpp:15:12: warning: unused variable 'lastP1' [-Wunused-variable]
15 | ll lastP1 = (i-1 ? p[i-2] : 0);
| ^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |