#include <bits/stdc++.h>
#define x first
#define y second
using ll = long long;
#define int ll
using namespace std;
int32_t main() {
int n, L;
cin >> n >> L;
vector<pair<int, int>> a(n + 2);
a[0] = {0, INT64_MAX};
a[n + 1] = {L, INT64_MAX};
for (int i = 1; i <= n; i++) {
cin >> a[i].x;
}
for (int i = 1; i <= n; i++) {
cin >> a[i].y;
}
n += 2;
vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(n, vector<ll>(n, INT64_MAX / 2)));
dp[0][n - 1][0] = 0;
for (int l = n - 1; l > 1; l--) {
for (int i = 0; i + l < n; i++) {
int j = i + l;
for (int s = 0; s < n; s++) {
ll dist1 = a[i + 1].x - a[i].x, dist2 = a[i].x + L - a[j - 1].x;
if (dp[i][j][s] + dist1 <= a[i + 1].y) {
dp[i + 1][j][s + 1] = min(dp[i + 1][j][s + 1], dp[i][j][s] + dist1);
} else {
dp[i + 1][j][s] = min(dp[i + 1][j][s], dp[i][j][s] + dist1);
}
if (dp[i][j][s] + dist2 <= a[j - 1].y) {
dp[j - 1][i][s + 1] = min(dp[j - 1][i][s + 1], dp[i][j][s] + dist2);
} else {
dp[j - 1][i][s] = min(dp[j - 1][i][s], dp[i][j][s] + dist2);
}
}
}
for (int i = l; i < n; i++) {
int j = i - l;
for (int s = 0; s < n; s++) {
ll dist1 = a[i].x - a[i - 1].x, dist2 = L - a[i].x + a[j + 1].x;
if (dp[i][j][s] + dist1 <= a[i - 1].y) {
dp[i - 1][j][s + 1] = min(dp[i - 1][j][s + 1], dp[i][j][s] + dist1);
} else {
dp[i - 1][j][s] = min(dp[i - 1][j][s], dp[i][j][s] + dist1);
}
if (dp[i][j][s] + dist2 <= a[j + 1].y) {
dp[j + 1][i][s + 1] = min(dp[j + 1][i][s + 1], dp[i][j][s] + dist2);
} else {
dp[j + 1][i][s] = min(dp[j + 1][i][s], dp[i][j][s] + dist2);
}
}
}
}
for (int s = n - 1; s >= 0; s--) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j][s] < INT64_MAX / 2) {
cout << s << '\n';
return 0;
}
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
58 ms |
47968 KB |
Output is correct |
19 |
Correct |
29 ms |
23132 KB |
Output is correct |
20 |
Correct |
10 ms |
9304 KB |
Output is correct |
21 |
Correct |
26 ms |
21084 KB |
Output is correct |
22 |
Correct |
35 ms |
31200 KB |
Output is correct |
23 |
Correct |
6 ms |
7456 KB |
Output is correct |
24 |
Correct |
5 ms |
5212 KB |
Output is correct |
25 |
Correct |
6 ms |
7260 KB |
Output is correct |
26 |
Correct |
2 ms |
1880 KB |
Output is correct |
27 |
Correct |
6 ms |
7772 KB |
Output is correct |
28 |
Correct |
4 ms |
4700 KB |
Output is correct |
29 |
Correct |
7 ms |
7860 KB |
Output is correct |
30 |
Correct |
5 ms |
5724 KB |
Output is correct |
31 |
Correct |
6 ms |
7260 KB |
Output is correct |
32 |
Correct |
3 ms |
2908 KB |
Output is correct |
33 |
Correct |
7 ms |
7360 KB |
Output is correct |
34 |
Correct |
2 ms |
1884 KB |
Output is correct |
35 |
Correct |
7 ms |
7004 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
8 ms |
7760 KB |
Output is correct |
38 |
Correct |
3 ms |
3416 KB |
Output is correct |
39 |
Correct |
8 ms |
8028 KB |
Output is correct |
40 |
Correct |
3 ms |
3932 KB |
Output is correct |
41 |
Correct |
68 ms |
65372 KB |
Output is correct |
42 |
Correct |
62 ms |
36652 KB |
Output is correct |
43 |
Correct |
66 ms |
65576 KB |
Output is correct |
44 |
Correct |
56 ms |
36188 KB |
Output is correct |
45 |
Correct |
66 ms |
65368 KB |
Output is correct |
46 |
Correct |
57 ms |
36444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
58 ms |
47968 KB |
Output is correct |
37 |
Correct |
29 ms |
23132 KB |
Output is correct |
38 |
Correct |
10 ms |
9304 KB |
Output is correct |
39 |
Correct |
26 ms |
21084 KB |
Output is correct |
40 |
Correct |
35 ms |
31200 KB |
Output is correct |
41 |
Correct |
6 ms |
7456 KB |
Output is correct |
42 |
Correct |
5 ms |
5212 KB |
Output is correct |
43 |
Correct |
6 ms |
7260 KB |
Output is correct |
44 |
Correct |
2 ms |
1880 KB |
Output is correct |
45 |
Correct |
6 ms |
7772 KB |
Output is correct |
46 |
Correct |
4 ms |
4700 KB |
Output is correct |
47 |
Correct |
7 ms |
7860 KB |
Output is correct |
48 |
Correct |
5 ms |
5724 KB |
Output is correct |
49 |
Correct |
6 ms |
7260 KB |
Output is correct |
50 |
Correct |
3 ms |
2908 KB |
Output is correct |
51 |
Correct |
7 ms |
7360 KB |
Output is correct |
52 |
Correct |
2 ms |
1884 KB |
Output is correct |
53 |
Correct |
7 ms |
7004 KB |
Output is correct |
54 |
Correct |
2 ms |
2652 KB |
Output is correct |
55 |
Correct |
8 ms |
7760 KB |
Output is correct |
56 |
Correct |
3 ms |
3416 KB |
Output is correct |
57 |
Correct |
8 ms |
8028 KB |
Output is correct |
58 |
Correct |
3 ms |
3932 KB |
Output is correct |
59 |
Correct |
68 ms |
65372 KB |
Output is correct |
60 |
Correct |
62 ms |
36652 KB |
Output is correct |
61 |
Correct |
66 ms |
65576 KB |
Output is correct |
62 |
Correct |
56 ms |
36188 KB |
Output is correct |
63 |
Correct |
66 ms |
65368 KB |
Output is correct |
64 |
Correct |
57 ms |
36444 KB |
Output is correct |
65 |
Correct |
70 ms |
56412 KB |
Output is correct |
66 |
Correct |
54 ms |
49496 KB |
Output is correct |
67 |
Correct |
53 ms |
46580 KB |
Output is correct |
68 |
Correct |
51 ms |
41560 KB |
Output is correct |
69 |
Correct |
61 ms |
55696 KB |
Output is correct |
70 |
Correct |
46 ms |
52308 KB |
Output is correct |
71 |
Correct |
64 ms |
53020 KB |
Output is correct |
72 |
Correct |
46 ms |
54100 KB |
Output is correct |
73 |
Correct |
58 ms |
48124 KB |
Output is correct |
74 |
Correct |
38 ms |
43344 KB |
Output is correct |
75 |
Correct |
59 ms |
50780 KB |
Output is correct |
76 |
Correct |
60 ms |
61708 KB |
Output is correct |
77 |
Correct |
78 ms |
61792 KB |
Output is correct |
78 |
Correct |
41 ms |
42076 KB |
Output is correct |
79 |
Correct |
53 ms |
44564 KB |
Output is correct |
80 |
Correct |
57 ms |
59964 KB |
Output is correct |
81 |
Correct |
55 ms |
44884 KB |
Output is correct |
82 |
Correct |
63 ms |
49232 KB |
Output is correct |
83 |
Correct |
81 ms |
65576 KB |
Output is correct |
84 |
Correct |
65 ms |
52316 KB |
Output is correct |
85 |
Correct |
65 ms |
59348 KB |
Output is correct |
86 |
Correct |
80 ms |
57436 KB |
Output is correct |
87 |
Correct |
62 ms |
50780 KB |
Output is correct |
88 |
Correct |
57 ms |
66756 KB |
Output is correct |
89 |
Correct |
65 ms |
66640 KB |
Output is correct |
90 |
Correct |
80 ms |
51288 KB |
Output is correct |
91 |
Correct |
60 ms |
66640 KB |
Output is correct |
92 |
Correct |
68 ms |
66640 KB |
Output is correct |
93 |
Correct |
102 ms |
64860 KB |
Output is correct |