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;
#define all(x) (x).begin(), (x).end()
#define ll long long int
#define pii pair <int, int>
#define pb push_back
#define ff first
#define ss second
// const int N = 1e5+5;
ll delivery(int n, int K, int L, int p[]){
// if (p[0] >= L-p[0] or L-p[n-1] >= p[n-1]){
// ll sum = 0;
// for (int i = 0; i < n; i++){
// if ((i+1)%K == 0){
// sum += min({L, p[i]*2, (L-p[i])*2});
// }
// }
// if (n%K){
// sum += min({L, p[n-1]*2, (L-p[n-1])*2});
// }
// return sum;
// }
ll sum = 0;
int l = -1, r = n;
for (int i = 0; i < n; i++){
if ((i+1)%K == 0 and p[i] <= L-p[i]){
sum += p[i]*2;
l = i;
}
}
for (int i = n-1; i >= 0; i--){
if ((n-i)%K == 0 and L-p[i] < p[i]){
sum += (L-p[i])*2;
r = i;
}
}
if (l == n-1 or !r) return sum;
const ll inf = 1e18;
ll mn = 1e18;
if ((r-l-1) <= K){
mn = min(mn, sum+min({(L-p[l+1])*2, p[r-1]*2, L}));
}
int x = l+1, y = r-1;
for (int i = l+1; i < r; i++){
if (p[i] <= L-p[i]) x = i;
}
for (int i = r-1; i > l; i--){
if (L-p[i] < p[i]) y = i;
}
return min(mn, sum+min(L, p[x]*2)+min(L, (L-p[y])*2));
}
// int main ()
// {
// ios::sync_with_stdio(false);
// cin.tie(0);
// int n, k, l;
// cin >> n >> k >> l;
// int p[n];
// for (int i = 0; i < n; i++) cin >> p[i];
// cout << delivery(n, k, l, p);
// }
Compilation message (stderr)
boxes.cpp: In function 'long long int delivery(int, int, int, int*)':
boxes.cpp:42:11: warning: unused variable 'inf' [-Wunused-variable]
42 | const ll inf = 1e18;
| ^~~
# | 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... |