#ifdef EVAL
#include "boxes.h"
#endif // EVAL
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e7 + 5;
const long long inf = 1e18;
int pos[MaxN], n, k, len;
long long dp[MaxN];
int cost(int le, int ri){
if (pos[ri] <= len / 2) return pos[ri] * 2;
if (pos[le] > len / 2) return (len - pos[le]) * 2;
return len;
}
struct dq{
int a[MaxN], ptrL = 1, ptrR = 0;
void fix(int i){
while (ptrL <= ptrR && i - a[ptrL] > k)
ptrL ++;
}
long long val[MaxN];
void add(int i, long long v){
val[i] = v;
while (ptrR >= ptrL && val[a[ptrR]] >= v)
ptrR --;
a[++ ptrR] = i;
}
long long get(){
return val[a[ptrR]];
}
} h1, h2;
long long delivery(int N, int K, int L, int position[]){
n = N, k = K, len = L;
for (int i = n - 1; i >= 0; -- i)
pos[i + 1] = position[i];
dp[0] = 0;
for (int i = 1; i <= n; ++ i){
dp[i] = dp[max(0, i - k)] + len;
h1.fix(i); h2.fix(i);
h1.add(i - 1, dp[i - 1]);
if (pos[i] > len / 2) h2.add(i - 1, dp[i - 1] + (len - pos[i]) * 2);
if (pos[i] <= len / 2) dp[i] = min(dp[i], h1.get() + pos[i] * 2);
else dp[i] = min(dp[i], h2.get());
}
return dp[n];
}
#ifndef EVAL
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k >> len;
for (int i = 0; i < n; ++ i)
cin >> pos[i];
cout << delivery(n, k, len, pos);
}
/*
5 1 5
1 3 3 3 4
*/
#endif // EVAL
# | 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... |