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>
using namespace std;
#define int long long
const int N = 5000 + 7;
const int INF = (int) 1e18 + 7;
int n, dmax, a[N], dp[N], ndp[N];
signed main()
{
#ifdef ONPC
freopen ("input.txt", "r", stdin);
#endif // ONPC
#ifndef ONPC
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC
cin >> n >> dmax;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
int big = *max_element(a + 1, a + n + 1);
if (*max_element(a + 1, a + n + 1) <= dmax)
{
cout << "0\n";
return 0;
}
assert(n < N && dmax < N);
for (int x = 0; x <= big; x++)
{
dp[x] = abs(x - a[1]);
}
for (int step = 2; step <= n; step++)
{
deque<int> d;
int y = 0;
for (int x = 0; x <= big; x++)
{
while (y <= big && y <= x + dmax)
{
while (!d.empty() && dp[d.back()] > dp[y])
{
d.pop_back();
}
d.push_back(y);
y++;
}
assert(!d.empty());
if (!d.empty() && d.front() < x - dmax)
{
d.pop_front();
}
assert(!d.empty());
assert(abs(x - d.front()) <= dmax);
ndp[x] = dp[d.front()] + abs(a[step] - x);
}
for (int x = 0; x <= big; x++)
{
dp[x] = ndp[x];
}
}
int best = dp[0];
for (int x = 0; x <= big; x++)
{
best = min(best, dp[x]);
}
cout << best << "\n";
return 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |