This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int N = 1000006;
int n, k;
LL m, l[N], r[N],pos[N];
LL ans=mod*mod;
LL delivery(int N, int K, int L, int* P)
{
n = N;
k = min(K,N);
m = L;
for (int i = 0; i < n; i++)
if (P[i])
pos[i + 1] = P[i];
pos[n + 1] = m;
for (int i = 1; i <= n; i++)
{
int id = (i - 1) / k;
l[i] = min(l[id * k], l[max(i - k, 0)]);
l[i] += abs(pos[i] - pos[0]) * 2ll;
}
reverse(pos, pos + 1 + n + 1);
for (int i = 1; i <= n; i++)
{
int id = (i - 1) / k;
r[i] = min(r[id * k], r[max(i - k, 0)]);
r[i] += abs(pos[i] - pos[0]) * 2ll;
}
reverse(pos, pos + 1 + n + 1);
for (int i = 0; i <= n; i++)
ans = min(ans, l[i] + r[n - i]);
for (int i = 0; i <= n; i++)
ans = min(ans, l[i] + r[max(0, n - i - k)] + m);
for (int i = 0; i <= n; i++)
ans = min(ans, r[i] + l[max(0, n - i - k)] + m);
return ans;
}
#ifdef death
int main()
{
int N, K, L;
int P[102];
cin >> N >> K >> L;
for (int i = 0; i < N; i++)
cin >> P[i];
cout << delivery(N, K, L, P) << endl;
return 0;
}
#endif
/*
3 2 8
1 2 5
4 3 6
0 0 4 5
2 3 7
3 4
0 1 2 3 4 5 6 0
* *
*/
Compilation message (stderr)
boxes.cpp: In function 'long long int delivery(int, int, int, int*)':
boxes.cpp:31:40: warning: declaration of 'N' shadows a global declaration [-Wshadow]
LL delivery(int N, int K, int L, int* P)
^
boxes.cpp:25:11: note: shadowed declaration is here
const int N = 1000006;
^
# | 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... |