# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
200191 | wilwxk | Safety (NOI18_safety) | C++14 | 48 ms | 19960 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(ll val, int id, int l) {
if(id > MAXN) return;
// printf("upd %lld %d %d %d\n", val, id, l, r);
while(dq.size() && dq.back().second < l) dq.pop_back();
while(dq.size() && dq.front().first >= val) dq.pop_front();
dq.push_front({val, id});
}
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(dp[i-1][j], j, -1);
for(int j = 0; j < MAXN; j++) {
update(dp[i-1][j+x], j+x, j-x);
ll cost = abs(j-v[i]);
cost += query();
dp[i][j] = cost;
// printf("%d %d >> %lld\n", 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... |