#include <iostream>
#include <vector>
using namespace std;
const int N = 200;
const long long INF = 1e18;
long long dp[1+N][1+N][2][1+N];
// dp[l][r][p][s] is minimum time where:
// - [l, r] is range of stamps left
// - p is the current position (boolean - either l-1 or r+1)
// - s is the current number of stamps collected
int main() {
int n, L;
cin >> n >> L;
vector<int> x(1+n), t(1+n);
for (int i = 1; i <= n; ++i) cin >> x[i];
for (int i = 1; i <= n; ++i) cin >> t[i];
x[0] = 0, t[0] = -1; // add stamp at x=0 that cannot be collected
auto d = [&x, &n, &L](int i, int j) {
if (i > j) swap(i, j);
return min(x[j]-x[i], x[i]+L-x[j]);
};
// fill with INF
for (int l = 0; l <= n; ++l) {
for (int r = max(0, l-1); r <= n; ++r) {
for (int p = 0; p <= 1; ++p) {
for (int s = 0; s <= n; ++s) dp[l][r][p][s] = INF;
}
}
}
// addition of extra stamp makes base case easier
dp[1][n][0][0] = dp[1][n][1][0] = 0;
/* answers will be stored at sz = 0 (r = l-1), but we do not traverse
since they are final states - they do not need to contribute to other states */
// dp - pushing answers to later states rather than pulling from previous
for (int sz = n; sz >= 1; --sz) {
for (int l = 1; l+sz-1 <= n; ++l) {
int r = l+sz-1, before = (l-1+(n+1))%(n+1), after = (r+1)%(n+1);
for (int s = 0; s <= n; ++s) {
// p = 0 means at l-1, p = 1 means at r+1
for (int p = 0; p <= 1; ++p) { // current position
for (int p1 = 0; p1 <= 1; ++p1) { // next position
int cur = (!p ? before : after); // current location
int nxt = (!p1 ? l : r); // next location
long long time = dp[l][r][p][s] + d(cur, nxt); // total time taken
bool delta = time <= t[nxt]; // extra stamps collected (0 or 1)
int l1 = l + (p1 == 0), r1 = r - (p1 == 1), s1 = s+delta;
if (l1 > n) continue; // r1 < 0 will never happen since l >= 1
dp[l1][r1][p1][s1] = min(dp[l1][r1][p1][s1], time);
}
}
}
}
}
int maxCollected = 0;
for (int s = 1; s <= n; ++s) {
for (int l = 1; l <= n; ++l) {
if (dp[l][l-1][0][s] != INF || dp[l][l-1][1][s] != INF) {
maxCollected = s;
}
}
}
cout << maxCollected << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
564 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
564 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
816 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
688 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
816 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
564 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
116 ms |
52820 KB |
Output is correct |
19 |
Correct |
59 ms |
32396 KB |
Output is correct |
20 |
Correct |
28 ms |
17492 KB |
Output is correct |
21 |
Correct |
61 ms |
30512 KB |
Output is correct |
22 |
Correct |
82 ms |
39508 KB |
Output is correct |
23 |
Correct |
22 ms |
15024 KB |
Output is correct |
24 |
Correct |
16 ms |
11860 KB |
Output is correct |
25 |
Correct |
21 ms |
14724 KB |
Output is correct |
26 |
Correct |
7 ms |
5812 KB |
Output is correct |
27 |
Correct |
23 ms |
15328 KB |
Output is correct |
28 |
Correct |
14 ms |
10964 KB |
Output is correct |
29 |
Correct |
24 ms |
15552 KB |
Output is correct |
30 |
Correct |
16 ms |
12372 KB |
Output is correct |
31 |
Correct |
21 ms |
14676 KB |
Output is correct |
32 |
Correct |
10 ms |
7940 KB |
Output is correct |
33 |
Correct |
22 ms |
14724 KB |
Output is correct |
34 |
Correct |
5 ms |
5528 KB |
Output is correct |
35 |
Correct |
21 ms |
14388 KB |
Output is correct |
36 |
Correct |
8 ms |
6996 KB |
Output is correct |
37 |
Correct |
22 ms |
15328 KB |
Output is correct |
38 |
Correct |
10 ms |
8532 KB |
Output is correct |
39 |
Correct |
24 ms |
15924 KB |
Output is correct |
40 |
Correct |
11 ms |
9556 KB |
Output is correct |
41 |
Correct |
168 ms |
65072 KB |
Output is correct |
42 |
Correct |
97 ms |
43980 KB |
Output is correct |
43 |
Correct |
175 ms |
65076 KB |
Output is correct |
44 |
Correct |
95 ms |
43684 KB |
Output is correct |
45 |
Correct |
168 ms |
65072 KB |
Output is correct |
46 |
Correct |
97 ms |
44096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
564 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
816 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
688 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
816 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
116 ms |
52820 KB |
Output is correct |
37 |
Correct |
59 ms |
32396 KB |
Output is correct |
38 |
Correct |
28 ms |
17492 KB |
Output is correct |
39 |
Correct |
61 ms |
30512 KB |
Output is correct |
40 |
Correct |
82 ms |
39508 KB |
Output is correct |
41 |
Correct |
22 ms |
15024 KB |
Output is correct |
42 |
Correct |
16 ms |
11860 KB |
Output is correct |
43 |
Correct |
21 ms |
14724 KB |
Output is correct |
44 |
Correct |
7 ms |
5812 KB |
Output is correct |
45 |
Correct |
23 ms |
15328 KB |
Output is correct |
46 |
Correct |
14 ms |
10964 KB |
Output is correct |
47 |
Correct |
24 ms |
15552 KB |
Output is correct |
48 |
Correct |
16 ms |
12372 KB |
Output is correct |
49 |
Correct |
21 ms |
14676 KB |
Output is correct |
50 |
Correct |
10 ms |
7940 KB |
Output is correct |
51 |
Correct |
22 ms |
14724 KB |
Output is correct |
52 |
Correct |
5 ms |
5528 KB |
Output is correct |
53 |
Correct |
21 ms |
14388 KB |
Output is correct |
54 |
Correct |
8 ms |
6996 KB |
Output is correct |
55 |
Correct |
22 ms |
15328 KB |
Output is correct |
56 |
Correct |
10 ms |
8532 KB |
Output is correct |
57 |
Correct |
24 ms |
15924 KB |
Output is correct |
58 |
Correct |
11 ms |
9556 KB |
Output is correct |
59 |
Correct |
168 ms |
65072 KB |
Output is correct |
60 |
Correct |
97 ms |
43980 KB |
Output is correct |
61 |
Correct |
175 ms |
65076 KB |
Output is correct |
62 |
Correct |
95 ms |
43684 KB |
Output is correct |
63 |
Correct |
168 ms |
65072 KB |
Output is correct |
64 |
Correct |
97 ms |
44096 KB |
Output is correct |
65 |
Correct |
149 ms |
58820 KB |
Output is correct |
66 |
Correct |
131 ms |
54076 KB |
Output is correct |
67 |
Correct |
123 ms |
51744 KB |
Output is correct |
68 |
Correct |
109 ms |
47852 KB |
Output is correct |
69 |
Correct |
140 ms |
58224 KB |
Output is correct |
70 |
Correct |
138 ms |
55832 KB |
Output is correct |
71 |
Correct |
144 ms |
56432 KB |
Output is correct |
72 |
Correct |
145 ms |
57020 KB |
Output is correct |
73 |
Correct |
128 ms |
52912 KB |
Output is correct |
74 |
Correct |
117 ms |
49612 KB |
Output is correct |
75 |
Correct |
135 ms |
54652 KB |
Output is correct |
76 |
Correct |
161 ms |
62536 KB |
Output is correct |
77 |
Correct |
163 ms |
62488 KB |
Output is correct |
78 |
Correct |
112 ms |
48404 KB |
Output is correct |
79 |
Correct |
118 ms |
50068 KB |
Output is correct |
80 |
Correct |
156 ms |
61232 KB |
Output is correct |
81 |
Correct |
120 ms |
50644 KB |
Output is correct |
82 |
Correct |
128 ms |
53456 KB |
Output is correct |
83 |
Correct |
169 ms |
65072 KB |
Output is correct |
84 |
Correct |
138 ms |
55764 KB |
Output is correct |
85 |
Correct |
155 ms |
60700 KB |
Output is correct |
86 |
Correct |
150 ms |
59444 KB |
Output is correct |
87 |
Correct |
131 ms |
54660 KB |
Output is correct |
88 |
Correct |
172 ms |
65700 KB |
Output is correct |
89 |
Correct |
176 ms |
65600 KB |
Output is correct |
90 |
Correct |
135 ms |
55248 KB |
Output is correct |
91 |
Correct |
165 ms |
65708 KB |
Output is correct |
92 |
Correct |
173 ms |
65704 KB |
Output is correct |
93 |
Correct |
167 ms |
64340 KB |
Output is correct |