import java.io.*;
import java.util.*;
// how this will work
// recursion: dp[l][r][#stamps][l/r endpoint? (0/1)] = min time
// dp[l][r][#stamps][l] = dp[l+1-->r][r][#stamps/#stamps-1][l/r]+dist(l/r point, this l)
// dp[l][r][#stamps][r] = dp[l][l-->r-1][#stamps/#stamps-1][l/r]+dist(l/r point, this r)
// base case: dp[0 (mid)][r] and dp[l][0 (mid)]
class ho_t3 {
int ans = 0;
public static void main(String[] args) throws IOException {
ho_t3 obj = new ho_t3();
obj.doStuff();
}
void process(int l, int r, int end) {
if (vis[l][r][end]) return;
vis[l][r][end] = true;
if (l==r) {
dp[l][l][0][end] = Math.abs(len-grid[l]);
if (Math.abs(len-grid[l]) <= time[l])
dp[l][l][1][end] = Math.abs(len-grid[l]);
} else if (end == 0) {
for (int i = l+1; i <= r; i++) {
process(i, r, 0); process(i, r, 1);
long distl = grid[i]-grid[l];
long distr = grid[r]-grid[l];
for (int j = 0; j < dp[0][0].length; j++) {
if (dp[i][r][j][0]+distl <= time[l]) {
if (j != dp[0][0].length-1)
dp[l][r][j+1][0] = Math.min(dp[l][r][j+1][0],
dp[i][r][j][0]+distl);
} else
dp[l][r][j][0] = Math.min(dp[l][r][j][0],
dp[i][r][j][0]+distl);
if (dp[i][r][j][1]+distr <= time[l]) {
if (j != dp[0][0].length-1)
dp[l][r][j+1][0] = Math.min(dp[l][r][j+1][0],
dp[i][r][j][1]+distr);
} else
dp[l][r][j][0] = Math.min(dp[l][r][j][0],
dp[i][r][j][1]+distr);
}
for (int j = dp[l][r].length-2; j >= 0; j--) {
dp[l][r][j][0] = Math.min(dp[l][r][j][0],
dp[l][r][j+1][0]);
}
}
} else {
for (int i = r-1; i >= l; i--) {
process(l, i, 0); process(l, i, 1);
long distl = grid[r]-grid[l];
long distr = grid[r]-grid[i];
for (int j = 0; j < dp[0][0].length; j++) {
if (dp[l][i][j][0]+distl <= time[r]) {
if (j != dp[0][0].length-1)
dp[l][r][j+1][1] = Math.min(dp[l][r][j+1][1],
dp[l][i][j][0]+distl);
} else
dp[l][r][j][1] = Math.min(dp[l][r][j][1],
dp[l][i][j][0]+distl);
if (dp[l][i][j][1]+distr <= time[r]) {
if (j != dp[0][0].length-1)
dp[l][r][j+1][1] = Math.min(dp[l][r][j+1][1],
dp[l][i][j][1]+distr);
} else
dp[l][r][j][1] = Math.min(dp[l][r][j][1],
dp[l][i][j][1]+distr);
}
for (int j = dp[l][r].length-2; j >= 0; j--) {
dp[l][r][j][1] = Math.min(dp[l][r][j][1],
dp[l][r][j+1][1]);
}
}
}
for (int i = 0; i < dp[0][0].length; i++) {
if (dp[l][r][i][end] != (((long) Integer.MAX_VALUE)*3)) {
ans = Math.max(ans, i);
}
}
}
int nums; long len;
long[] grid, time;
long[][][][] dp; // l r #stamps endpoint
boolean[][][] vis;
private void doStuff() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
nums = Integer.parseInt(st.nextToken());
len = Integer.parseInt(st.nextToken());
grid = new long[nums*2];
time = new long[nums*2];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < nums; i++) {
int val = Integer.parseInt(st.nextToken());
grid[i] = val; grid[nums+i] = val+len;
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < nums; i++) {
int val = Integer.parseInt(st.nextToken());
time[i] = val; time[nums+i] = val;
}
br.close();
dp = new long[grid.length][grid.length][nums+1][2];
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
for (int k = 0; k < dp[0][0].length; k++) {
for (int l = 0; l < dp[0][0][0].length; l++) {
dp[i][j][k][l] = ((long) Integer.MAX_VALUE)*3;
}
}
}
}
vis = new boolean[grid.length][grid.length][2];
/*
int sum = 0;
for (int i = nums; i < grid.length; i++) {
if (time[i] >= grid[i]-len) sum++;
dp[nums][i][0] = new long[] {0, 0};
for (int j = 1; j <= sum; j++) {
dp[nums][i][j][1] = grid[i]-len;
dp[nums][i][j][0] = (grid[i]-len)*2;
}
vis[nums][i] = new boolean[] {true, true};
}
sum = 0;
for (int i = nums-1; i >= 0; i--) {
if (time[i] >= len-grid[i]) sum++;
dp[nums][i][0] = new long[] {0, 0};
for (int j = 1; j <= sum; j++) {
dp[i][nums-1][j][0] = len-grid[i];
dp[i][nums-1][j][1] = (len-grid[i])*2;
}
vis[i][nums-1] = new boolean[] {true, true};
}
*/
for (int i = 0; i <= nums; i++) {
process(i, i+nums-1, 0);
process(i, i+nums-1, 1);
}
System.out.println(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
8808 KB |
Output is correct |
2 |
Correct |
72 ms |
8556 KB |
Output is correct |
3 |
Correct |
151 ms |
14656 KB |
Output is correct |
4 |
Correct |
74 ms |
8428 KB |
Output is correct |
5 |
Correct |
79 ms |
8556 KB |
Output is correct |
6 |
Correct |
287 ms |
17444 KB |
Output is correct |
7 |
Correct |
174 ms |
15964 KB |
Output is correct |
8 |
Correct |
213 ms |
16468 KB |
Output is correct |
9 |
Correct |
222 ms |
16516 KB |
Output is correct |
10 |
Correct |
72 ms |
8412 KB |
Output is correct |
11 |
Correct |
75 ms |
8556 KB |
Output is correct |
12 |
Correct |
304 ms |
17964 KB |
Output is correct |
13 |
Correct |
254 ms |
17716 KB |
Output is correct |
14 |
Correct |
75 ms |
8556 KB |
Output is correct |
15 |
Correct |
131 ms |
13928 KB |
Output is correct |
16 |
Correct |
276 ms |
17440 KB |
Output is correct |
17 |
Correct |
277 ms |
17440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
8808 KB |
Output is correct |
2 |
Correct |
72 ms |
8556 KB |
Output is correct |
3 |
Correct |
151 ms |
14656 KB |
Output is correct |
4 |
Correct |
74 ms |
8428 KB |
Output is correct |
5 |
Correct |
79 ms |
8556 KB |
Output is correct |
6 |
Correct |
287 ms |
17444 KB |
Output is correct |
7 |
Correct |
174 ms |
15964 KB |
Output is correct |
8 |
Correct |
213 ms |
16468 KB |
Output is correct |
9 |
Correct |
222 ms |
16516 KB |
Output is correct |
10 |
Correct |
72 ms |
8412 KB |
Output is correct |
11 |
Correct |
75 ms |
8556 KB |
Output is correct |
12 |
Correct |
304 ms |
17964 KB |
Output is correct |
13 |
Correct |
254 ms |
17716 KB |
Output is correct |
14 |
Correct |
75 ms |
8556 KB |
Output is correct |
15 |
Correct |
131 ms |
13928 KB |
Output is correct |
16 |
Correct |
276 ms |
17440 KB |
Output is correct |
17 |
Correct |
277 ms |
17440 KB |
Output is correct |
18 |
Correct |
252 ms |
16628 KB |
Output is correct |
19 |
Correct |
167 ms |
16088 KB |
Output is correct |
20 |
Correct |
245 ms |
16548 KB |
Output is correct |
21 |
Correct |
165 ms |
16368 KB |
Output is correct |
22 |
Correct |
256 ms |
17728 KB |
Output is correct |
23 |
Correct |
256 ms |
17612 KB |
Output is correct |
24 |
Correct |
316 ms |
16916 KB |
Output is correct |
25 |
Correct |
177 ms |
16584 KB |
Output is correct |
26 |
Correct |
300 ms |
16916 KB |
Output is correct |
27 |
Correct |
175 ms |
15676 KB |
Output is correct |
28 |
Correct |
138 ms |
14732 KB |
Output is correct |
29 |
Correct |
250 ms |
17692 KB |
Output is correct |
30 |
Correct |
257 ms |
17712 KB |
Output is correct |
31 |
Correct |
148 ms |
14276 KB |
Output is correct |
32 |
Correct |
146 ms |
14276 KB |
Output is correct |
33 |
Correct |
245 ms |
17628 KB |
Output is correct |
34 |
Correct |
254 ms |
17732 KB |
Output is correct |
35 |
Correct |
260 ms |
17848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
8808 KB |
Output is correct |
2 |
Correct |
72 ms |
8556 KB |
Output is correct |
3 |
Correct |
151 ms |
14656 KB |
Output is correct |
4 |
Correct |
74 ms |
8428 KB |
Output is correct |
5 |
Correct |
79 ms |
8556 KB |
Output is correct |
6 |
Correct |
287 ms |
17444 KB |
Output is correct |
7 |
Correct |
174 ms |
15964 KB |
Output is correct |
8 |
Correct |
213 ms |
16468 KB |
Output is correct |
9 |
Correct |
222 ms |
16516 KB |
Output is correct |
10 |
Correct |
72 ms |
8412 KB |
Output is correct |
11 |
Correct |
75 ms |
8556 KB |
Output is correct |
12 |
Correct |
304 ms |
17964 KB |
Output is correct |
13 |
Correct |
254 ms |
17716 KB |
Output is correct |
14 |
Correct |
75 ms |
8556 KB |
Output is correct |
15 |
Correct |
131 ms |
13928 KB |
Output is correct |
16 |
Correct |
276 ms |
17440 KB |
Output is correct |
17 |
Correct |
277 ms |
17440 KB |
Output is correct |
18 |
Execution timed out |
2129 ms |
498216 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
8808 KB |
Output is correct |
2 |
Correct |
72 ms |
8556 KB |
Output is correct |
3 |
Correct |
151 ms |
14656 KB |
Output is correct |
4 |
Correct |
74 ms |
8428 KB |
Output is correct |
5 |
Correct |
79 ms |
8556 KB |
Output is correct |
6 |
Correct |
287 ms |
17444 KB |
Output is correct |
7 |
Correct |
174 ms |
15964 KB |
Output is correct |
8 |
Correct |
213 ms |
16468 KB |
Output is correct |
9 |
Correct |
222 ms |
16516 KB |
Output is correct |
10 |
Correct |
72 ms |
8412 KB |
Output is correct |
11 |
Correct |
75 ms |
8556 KB |
Output is correct |
12 |
Correct |
304 ms |
17964 KB |
Output is correct |
13 |
Correct |
254 ms |
17716 KB |
Output is correct |
14 |
Correct |
75 ms |
8556 KB |
Output is correct |
15 |
Correct |
131 ms |
13928 KB |
Output is correct |
16 |
Correct |
276 ms |
17440 KB |
Output is correct |
17 |
Correct |
277 ms |
17440 KB |
Output is correct |
18 |
Correct |
252 ms |
16628 KB |
Output is correct |
19 |
Correct |
167 ms |
16088 KB |
Output is correct |
20 |
Correct |
245 ms |
16548 KB |
Output is correct |
21 |
Correct |
165 ms |
16368 KB |
Output is correct |
22 |
Correct |
256 ms |
17728 KB |
Output is correct |
23 |
Correct |
256 ms |
17612 KB |
Output is correct |
24 |
Correct |
316 ms |
16916 KB |
Output is correct |
25 |
Correct |
177 ms |
16584 KB |
Output is correct |
26 |
Correct |
300 ms |
16916 KB |
Output is correct |
27 |
Correct |
175 ms |
15676 KB |
Output is correct |
28 |
Correct |
138 ms |
14732 KB |
Output is correct |
29 |
Correct |
250 ms |
17692 KB |
Output is correct |
30 |
Correct |
257 ms |
17712 KB |
Output is correct |
31 |
Correct |
148 ms |
14276 KB |
Output is correct |
32 |
Correct |
146 ms |
14276 KB |
Output is correct |
33 |
Correct |
245 ms |
17628 KB |
Output is correct |
34 |
Correct |
254 ms |
17732 KB |
Output is correct |
35 |
Correct |
260 ms |
17848 KB |
Output is correct |
36 |
Execution timed out |
2129 ms |
498216 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |