#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
vector<vector<vector<int>>> dp[2];
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, l;
cin >> n >> l;
vector<int> x(n), t(n);
for (auto& el : x)
cin >> el;
for (auto& el : t)
cin >> el;
auto dist = [&](int x1, int x2) {
int res = (x1 - x2 + l) % l;
res = min(res, (x2 - x1 + l) % l);
return res;
};
dp[0].resize(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, INF)));
dp[1].resize(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, INF)));
dp[0][0][0][0] = 0;
dp[1][0][0][0] = 0;
dp[0][1][0][0] = dist(0, x[0]);
if (dp[0][1][0][0] <= t[0])
dp[0][1][0][1] = dp[0][1][0][0];
dp[1][0][1][0] = dist(0, x[n - 1]);
if (dp[1][0][1][0] <= t[n - 1])
dp[1][0][1][1] = dp[1][0][1][0];
for (int ln = 2; ln <= n; ++ln) {
for (int l = 0; l <= ln; ++l) {
int r = ln - l;
if (l != 0) {
for (int prevcnt = 0; prevcnt < ln; ++prevcnt) {
if (l - 1 != 0)
dp[0][l][r][prevcnt] = min(dp[0][l][r][prevcnt], dp[0][l - 1][r][prevcnt] + dist(x[l - 1], x[l - 2]));
if (r != 0)
dp[0][l][r][prevcnt] = min(dp[0][l][r][prevcnt], dp[1][l - 1][r][prevcnt] + dist(x[l - 1], x[n - r]));
}
for (int curcnt = ln ; curcnt > 0; --curcnt)
if (dp[0][l][r][curcnt - 1] <= t[l - 1])
dp[0][l][r][curcnt] = min(dp[0][l][r][curcnt], dp[0][l][r][curcnt - 1]);
}
if (r != 0) {
for (int prevcnt = 0; prevcnt < ln; ++prevcnt) {
if (r - 1 != 0)
dp[1][l][r][prevcnt] = min(dp[1][l][r][prevcnt], dp[1][l][r - 1][prevcnt] + dist(x[n - r], x[n - r + 1]));
if (l != 0)
dp[1][l][r][prevcnt] = min(dp[1][l][r][prevcnt], dp[0][l][r - 1][prevcnt] + dist(x[n - r], x[l - 1]));
}
for (int curcnt = ln ; curcnt > 0; --curcnt)
if (dp[1][l][r][curcnt - 1] <= t[n - r])
dp[1][l][r][curcnt] = min(dp[1][l][r][curcnt], dp[1][l][r][curcnt - 1]);
}
}
}
for (int curcnt = n; curcnt > 0; --curcnt) {
for (int l = 0; l <= n; ++l) {
int r = n - l;
if (dp[0][l][r][curcnt] != INF) {
cout << curcnt;
return 0;
}
if (dp[1][l][r][curcnt] != INF) {
cout << curcnt;
return 0;
}
}
}
cout << 0;
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 |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 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 |
456 KB |
Output is correct |
12 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 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 |
456 KB |
Output is correct |
12 |
Correct |
1 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 |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
452 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 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 |
456 KB |
Output is correct |
12 |
Correct |
1 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 |
106 ms |
94336 KB |
Output is correct |
19 |
Correct |
52 ms |
44888 KB |
Output is correct |
20 |
Correct |
21 ms |
17756 KB |
Output is correct |
21 |
Correct |
46 ms |
41304 KB |
Output is correct |
22 |
Correct |
68 ms |
60248 KB |
Output is correct |
23 |
Correct |
16 ms |
14168 KB |
Output is correct |
24 |
Correct |
11 ms |
9816 KB |
Output is correct |
25 |
Correct |
15 ms |
13660 KB |
Output is correct |
26 |
Correct |
4 ms |
3532 KB |
Output is correct |
27 |
Correct |
18 ms |
14680 KB |
Output is correct |
28 |
Correct |
10 ms |
8796 KB |
Output is correct |
29 |
Correct |
17 ms |
14940 KB |
Output is correct |
30 |
Correct |
12 ms |
10588 KB |
Output is correct |
31 |
Correct |
16 ms |
13672 KB |
Output is correct |
32 |
Correct |
6 ms |
5468 KB |
Output is correct |
33 |
Correct |
15 ms |
13664 KB |
Output is correct |
34 |
Correct |
3 ms |
3160 KB |
Output is correct |
35 |
Correct |
15 ms |
13148 KB |
Output is correct |
36 |
Correct |
5 ms |
4472 KB |
Output is correct |
37 |
Correct |
16 ms |
14424 KB |
Output is correct |
38 |
Correct |
7 ms |
6236 KB |
Output is correct |
39 |
Correct |
17 ms |
15416 KB |
Output is correct |
40 |
Correct |
8 ms |
7260 KB |
Output is correct |
41 |
Correct |
144 ms |
129108 KB |
Output is correct |
42 |
Correct |
80 ms |
71768 KB |
Output is correct |
43 |
Correct |
144 ms |
129132 KB |
Output is correct |
44 |
Correct |
81 ms |
70228 KB |
Output is correct |
45 |
Correct |
145 ms |
129104 KB |
Output is correct |
46 |
Correct |
80 ms |
71756 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 |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 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 |
456 KB |
Output is correct |
12 |
Correct |
1 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 |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
452 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 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 |
106 ms |
94336 KB |
Output is correct |
37 |
Correct |
52 ms |
44888 KB |
Output is correct |
38 |
Correct |
21 ms |
17756 KB |
Output is correct |
39 |
Correct |
46 ms |
41304 KB |
Output is correct |
40 |
Correct |
68 ms |
60248 KB |
Output is correct |
41 |
Correct |
16 ms |
14168 KB |
Output is correct |
42 |
Correct |
11 ms |
9816 KB |
Output is correct |
43 |
Correct |
15 ms |
13660 KB |
Output is correct |
44 |
Correct |
4 ms |
3532 KB |
Output is correct |
45 |
Correct |
18 ms |
14680 KB |
Output is correct |
46 |
Correct |
10 ms |
8796 KB |
Output is correct |
47 |
Correct |
17 ms |
14940 KB |
Output is correct |
48 |
Correct |
12 ms |
10588 KB |
Output is correct |
49 |
Correct |
16 ms |
13672 KB |
Output is correct |
50 |
Correct |
6 ms |
5468 KB |
Output is correct |
51 |
Correct |
15 ms |
13664 KB |
Output is correct |
52 |
Correct |
3 ms |
3160 KB |
Output is correct |
53 |
Correct |
15 ms |
13148 KB |
Output is correct |
54 |
Correct |
5 ms |
4472 KB |
Output is correct |
55 |
Correct |
16 ms |
14424 KB |
Output is correct |
56 |
Correct |
7 ms |
6236 KB |
Output is correct |
57 |
Correct |
17 ms |
15416 KB |
Output is correct |
58 |
Correct |
8 ms |
7260 KB |
Output is correct |
59 |
Correct |
144 ms |
129108 KB |
Output is correct |
60 |
Correct |
80 ms |
71768 KB |
Output is correct |
61 |
Correct |
144 ms |
129132 KB |
Output is correct |
62 |
Correct |
81 ms |
70228 KB |
Output is correct |
63 |
Correct |
145 ms |
129104 KB |
Output is correct |
64 |
Correct |
80 ms |
71756 KB |
Output is correct |
65 |
Correct |
126 ms |
110940 KB |
Output is correct |
66 |
Correct |
109 ms |
97616 KB |
Output is correct |
67 |
Correct |
102 ms |
91392 KB |
Output is correct |
68 |
Correct |
92 ms |
80708 KB |
Output is correct |
69 |
Correct |
122 ms |
108672 KB |
Output is correct |
70 |
Correct |
117 ms |
102224 KB |
Output is correct |
71 |
Correct |
117 ms |
104180 KB |
Output is correct |
72 |
Correct |
123 ms |
105300 KB |
Output is correct |
73 |
Correct |
106 ms |
94552 KB |
Output is correct |
74 |
Correct |
96 ms |
85588 KB |
Output is correct |
75 |
Correct |
112 ms |
98716 KB |
Output is correct |
76 |
Correct |
135 ms |
121656 KB |
Output is correct |
77 |
Correct |
141 ms |
121428 KB |
Output is correct |
78 |
Correct |
95 ms |
82904 KB |
Output is correct |
79 |
Correct |
96 ms |
86524 KB |
Output is correct |
80 |
Correct |
134 ms |
117840 KB |
Output is correct |
81 |
Correct |
99 ms |
88400 KB |
Output is correct |
82 |
Correct |
110 ms |
95576 KB |
Output is correct |
83 |
Correct |
143 ms |
129148 KB |
Output is correct |
84 |
Correct |
117 ms |
102000 KB |
Output is correct |
85 |
Correct |
146 ms |
115528 KB |
Output is correct |
86 |
Correct |
127 ms |
112212 KB |
Output is correct |
87 |
Correct |
112 ms |
98644 KB |
Output is correct |
88 |
Correct |
146 ms |
130436 KB |
Output is correct |
89 |
Correct |
147 ms |
130440 KB |
Output is correct |
90 |
Correct |
115 ms |
100948 KB |
Output is correct |
91 |
Correct |
147 ms |
130436 KB |
Output is correct |
92 |
Correct |
146 ms |
130388 KB |
Output is correct |
93 |
Correct |
144 ms |
126628 KB |
Output is correct |