#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 (r1 < 0) continue;
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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
688 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 |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
688 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 |
596 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 |
564 KB |
Output is correct |
20 |
Correct |
0 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
688 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
692 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 |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
688 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 |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
123 ms |
52812 KB |
Output is correct |
19 |
Correct |
57 ms |
32400 KB |
Output is correct |
20 |
Correct |
22 ms |
17648 KB |
Output is correct |
21 |
Correct |
55 ms |
30616 KB |
Output is correct |
22 |
Correct |
85 ms |
39488 KB |
Output is correct |
23 |
Correct |
21 ms |
15024 KB |
Output is correct |
24 |
Correct |
15 ms |
11856 KB |
Output is correct |
25 |
Correct |
21 ms |
14676 KB |
Output is correct |
26 |
Correct |
6 ms |
5844 KB |
Output is correct |
27 |
Correct |
23 ms |
15332 KB |
Output is correct |
28 |
Correct |
15 ms |
11056 KB |
Output is correct |
29 |
Correct |
21 ms |
15572 KB |
Output is correct |
30 |
Correct |
15 ms |
12372 KB |
Output is correct |
31 |
Correct |
20 ms |
14724 KB |
Output is correct |
32 |
Correct |
9 ms |
7892 KB |
Output is correct |
33 |
Correct |
20 ms |
14732 KB |
Output is correct |
34 |
Correct |
6 ms |
5588 KB |
Output is correct |
35 |
Correct |
21 ms |
14420 KB |
Output is correct |
36 |
Correct |
7 ms |
6996 KB |
Output is correct |
37 |
Correct |
24 ms |
15328 KB |
Output is correct |
38 |
Correct |
11 ms |
8532 KB |
Output is correct |
39 |
Correct |
23 ms |
15948 KB |
Output is correct |
40 |
Correct |
12 ms |
9556 KB |
Output is correct |
41 |
Correct |
161 ms |
65088 KB |
Output is correct |
42 |
Correct |
88 ms |
44116 KB |
Output is correct |
43 |
Correct |
161 ms |
65036 KB |
Output is correct |
44 |
Correct |
122 ms |
43472 KB |
Output is correct |
45 |
Correct |
166 ms |
64980 KB |
Output is correct |
46 |
Correct |
89 ms |
44096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
688 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 |
596 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 |
564 KB |
Output is correct |
20 |
Correct |
0 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
688 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
692 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 |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
824 KB |
Output is correct |
36 |
Correct |
123 ms |
52812 KB |
Output is correct |
37 |
Correct |
57 ms |
32400 KB |
Output is correct |
38 |
Correct |
22 ms |
17648 KB |
Output is correct |
39 |
Correct |
55 ms |
30616 KB |
Output is correct |
40 |
Correct |
85 ms |
39488 KB |
Output is correct |
41 |
Correct |
21 ms |
15024 KB |
Output is correct |
42 |
Correct |
15 ms |
11856 KB |
Output is correct |
43 |
Correct |
21 ms |
14676 KB |
Output is correct |
44 |
Correct |
6 ms |
5844 KB |
Output is correct |
45 |
Correct |
23 ms |
15332 KB |
Output is correct |
46 |
Correct |
15 ms |
11056 KB |
Output is correct |
47 |
Correct |
21 ms |
15572 KB |
Output is correct |
48 |
Correct |
15 ms |
12372 KB |
Output is correct |
49 |
Correct |
20 ms |
14724 KB |
Output is correct |
50 |
Correct |
9 ms |
7892 KB |
Output is correct |
51 |
Correct |
20 ms |
14732 KB |
Output is correct |
52 |
Correct |
6 ms |
5588 KB |
Output is correct |
53 |
Correct |
21 ms |
14420 KB |
Output is correct |
54 |
Correct |
7 ms |
6996 KB |
Output is correct |
55 |
Correct |
24 ms |
15328 KB |
Output is correct |
56 |
Correct |
11 ms |
8532 KB |
Output is correct |
57 |
Correct |
23 ms |
15948 KB |
Output is correct |
58 |
Correct |
12 ms |
9556 KB |
Output is correct |
59 |
Correct |
161 ms |
65088 KB |
Output is correct |
60 |
Correct |
88 ms |
44116 KB |
Output is correct |
61 |
Correct |
161 ms |
65036 KB |
Output is correct |
62 |
Correct |
122 ms |
43472 KB |
Output is correct |
63 |
Correct |
166 ms |
64980 KB |
Output is correct |
64 |
Correct |
89 ms |
44096 KB |
Output is correct |
65 |
Correct |
142 ms |
58832 KB |
Output is correct |
66 |
Correct |
134 ms |
54148 KB |
Output is correct |
67 |
Correct |
116 ms |
51728 KB |
Output is correct |
68 |
Correct |
103 ms |
47852 KB |
Output is correct |
69 |
Correct |
140 ms |
58224 KB |
Output is correct |
70 |
Correct |
131 ms |
55832 KB |
Output is correct |
71 |
Correct |
142 ms |
56428 KB |
Output is correct |
72 |
Correct |
136 ms |
57024 KB |
Output is correct |
73 |
Correct |
122 ms |
52916 KB |
Output is correct |
74 |
Correct |
103 ms |
49528 KB |
Output is correct |
75 |
Correct |
135 ms |
54664 KB |
Output is correct |
76 |
Correct |
154 ms |
62468 KB |
Output is correct |
77 |
Correct |
150 ms |
62548 KB |
Output is correct |
78 |
Correct |
101 ms |
48404 KB |
Output is correct |
79 |
Correct |
112 ms |
50068 KB |
Output is correct |
80 |
Correct |
150 ms |
61232 KB |
Output is correct |
81 |
Correct |
112 ms |
50640 KB |
Output is correct |
82 |
Correct |
112 ms |
53492 KB |
Output is correct |
83 |
Correct |
163 ms |
65068 KB |
Output is correct |
84 |
Correct |
128 ms |
55836 KB |
Output is correct |
85 |
Correct |
158 ms |
60676 KB |
Output is correct |
86 |
Correct |
144 ms |
59448 KB |
Output is correct |
87 |
Correct |
124 ms |
54656 KB |
Output is correct |
88 |
Runtime error |
229 ms |
132904 KB |
Execution killed with signal 11 |
89 |
Halted |
0 ms |
0 KB |
- |