# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
200194 | wilwxk | Safety (NOI18_safety) | C++14 | 566 ms | 196344 KiB |
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;
typedef long long ll;
const int MAXN = 5e3+5;
int v[MAXN];
ll dp[MAXN][MAXN];
deque<pair<ll, int> > dq;
int n, x;
void update(int i, int j, int l) {
while(dq.size() && dq.back().second < l) dq.pop_back();
if(i >= MAXN || j >= MAXN) return;
ll val = dp[i][j];
while(dq.size() && dq.front().first >= val) dq.pop_front();
dq.push_front({val, j});
}
ll query() {
return dq.back().first;
}
int main() {
scanf("%d %d", &n, &x);
for(int i = 1; i <= n; i++) scanf("%d", &v[i]);
for(int i = 1; i <= n; i++) {
for(int j = 0; j < min(x, MAXN); j++) update(i-1, j, -1);
for(int j = 0; j < MAXN; j++) {
update(i-1, j+x, j-x);
ll cost = abs(j-v[i]);
cost += query();
dp[i][j] = cost;
}
while(dq.size()) dq.pop_back();
}
ll ans = 1e18;
for(int i = 0; i < MAXN; i++) ans = min(ans, dp[n][i]);
printf("%lld\n", ans);
}
Compilation message (stderr)
# | 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... |