#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
safety.cpp: In function 'int main()':
safety.cpp:24:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &x);
~~~~~^~~~~~~~~~~~~~~~~
safety.cpp:25:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= n; i++) scanf("%d", &v[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
764 KB |
Output is correct |
5 |
Correct |
6 ms |
636 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
888 KB |
Output is correct |
2 |
Correct |
6 ms |
888 KB |
Output is correct |
3 |
Correct |
6 ms |
888 KB |
Output is correct |
4 |
Correct |
6 ms |
888 KB |
Output is correct |
5 |
Correct |
6 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6 ms |
376 KB |
Time limit exceeded (wall clock) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
764 KB |
Output is correct |
5 |
Correct |
6 ms |
636 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
6 ms |
888 KB |
Output is correct |
9 |
Correct |
6 ms |
888 KB |
Output is correct |
10 |
Correct |
6 ms |
888 KB |
Output is correct |
11 |
Correct |
6 ms |
888 KB |
Output is correct |
12 |
Correct |
6 ms |
888 KB |
Output is correct |
13 |
Correct |
47 ms |
19960 KB |
Output is correct |
14 |
Correct |
43 ms |
19192 KB |
Output is correct |
15 |
Correct |
44 ms |
19448 KB |
Output is correct |
16 |
Correct |
47 ms |
19960 KB |
Output is correct |
17 |
Correct |
46 ms |
17656 KB |
Output is correct |
18 |
Correct |
36 ms |
14736 KB |
Output is correct |
19 |
Correct |
48 ms |
19064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
764 KB |
Output is correct |
5 |
Correct |
6 ms |
636 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
6 ms |
888 KB |
Output is correct |
9 |
Correct |
6 ms |
888 KB |
Output is correct |
10 |
Correct |
6 ms |
888 KB |
Output is correct |
11 |
Correct |
6 ms |
888 KB |
Output is correct |
12 |
Correct |
6 ms |
888 KB |
Output is correct |
13 |
Correct |
47 ms |
19960 KB |
Output is correct |
14 |
Correct |
43 ms |
19192 KB |
Output is correct |
15 |
Correct |
44 ms |
19448 KB |
Output is correct |
16 |
Correct |
47 ms |
19960 KB |
Output is correct |
17 |
Correct |
46 ms |
17656 KB |
Output is correct |
18 |
Correct |
36 ms |
14736 KB |
Output is correct |
19 |
Correct |
48 ms |
19064 KB |
Output is correct |
20 |
Incorrect |
43 ms |
18680 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
764 KB |
Output is correct |
5 |
Correct |
6 ms |
636 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
6 ms |
888 KB |
Output is correct |
9 |
Correct |
6 ms |
888 KB |
Output is correct |
10 |
Correct |
6 ms |
888 KB |
Output is correct |
11 |
Correct |
6 ms |
888 KB |
Output is correct |
12 |
Correct |
6 ms |
888 KB |
Output is correct |
13 |
Correct |
47 ms |
19960 KB |
Output is correct |
14 |
Correct |
43 ms |
19192 KB |
Output is correct |
15 |
Correct |
44 ms |
19448 KB |
Output is correct |
16 |
Correct |
47 ms |
19960 KB |
Output is correct |
17 |
Correct |
46 ms |
17656 KB |
Output is correct |
18 |
Correct |
36 ms |
14736 KB |
Output is correct |
19 |
Correct |
48 ms |
19064 KB |
Output is correct |
20 |
Incorrect |
43 ms |
18680 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
764 KB |
Output is correct |
5 |
Correct |
6 ms |
636 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
6 ms |
888 KB |
Output is correct |
9 |
Correct |
6 ms |
888 KB |
Output is correct |
10 |
Correct |
6 ms |
888 KB |
Output is correct |
11 |
Correct |
6 ms |
888 KB |
Output is correct |
12 |
Correct |
6 ms |
888 KB |
Output is correct |
13 |
Incorrect |
5 ms |
632 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
764 KB |
Output is correct |
5 |
Correct |
6 ms |
636 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
6 ms |
888 KB |
Output is correct |
9 |
Correct |
6 ms |
888 KB |
Output is correct |
10 |
Correct |
6 ms |
888 KB |
Output is correct |
11 |
Correct |
6 ms |
888 KB |
Output is correct |
12 |
Correct |
6 ms |
888 KB |
Output is correct |
13 |
Incorrect |
5 ms |
632 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |