This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "boxes.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
ll delivery(int n, int k, int L, int a[]) {
int s = 0, t = n - 1;
while (s < n && a[s] == 0) s++;
if (s > t) return 0;
auto cal = [&](int llim, int rlim)->ll {
if (L == 1) return 0;
if (L == 2) {
int cnt = t - s + 1;
return 2ll * ((cnt + k - 1) / k);
}
int sl = s, sr = s - 1;
for (int i = s; i <= t && a[i] <= llim; i++) sr = i;
int tl = t + 1, tr = t;
for (int i = t; i >= s && a[i] >= rlim; i--) tl = i;
ll sum = 0;
for (; sl + k - 1 <= sr; sl += k) sum += 2 * a[sl + k - 1];
for (; tr - k + 1 >= tl; tr -= k) sum += 2 * (L-a[tr - k + 1]);
int cnt = (sr-sl+1) + (tr-tl+1);
if (sl>sr && tl>tr) return sum;
if (sl>sr) return sum + 2 * (L-a[tl]);
if (tl>tr) return sum + 2 * a[sr];
if (cnt <= k) return sum + min(2ll * (a[sr] + L-a[tl]), 1ll * L);
ll mn = 2ll * (a[sr] + L-a[tl]);
mn = min(mn, L + 2ll * (L-a[tr - (cnt-k) + 1]));
mn = min(mn, L + 2ll * a[sl + (cnt-k) - 1]);
return sum + mn;
};
ll ans;
if (L & 1) ans = cal((L - 1) / 2, (L + 1) / 2);
else ans = min(cal(L / 2, L / 2 + 1), cal(L / 2 - 1, L / 2));
return ans;
}
#ifdef WAIMAI
int main() {
int N, K, L, i;
cin >> N >> K >> L;
int *p = (int*)malloc(sizeof(int) * (unsigned int)N);
for (i = 0; i < N; i++) cin >> p[i];
cout << delivery(N, K, L, p) << '\n';
}
#endif
/*
in1
3 2 8
1 2 5
out1
10
*/
# | 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... |