#include "wiring.h"
long long abs (long long x){
if (x > 0) return x;
return -x;
}
long long max (long long a, long long b){
if (a > b) return a;
return b;
}
long long min (long long a, long long b){
if (a > b) return b;
return a;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int n, m;
n = r.size();
m = b.size();
if (r[n-1] < b[0]){
long long ans = 0;
for (int i=0; i<n; i++){
ans += r[n-1] - r[i];
}
for (int i=0; i<m; i++){
ans += b[i] - b[0];
}
ans += max(n, m) *(b[0] - r[n-1]);
return ans;
}
long long DP[n][m];
DP[0][0] = abs(b[0] - r[0]);
for (int i=0; i<n; i++){
for (int j=0; j<m; j++){
if (i == 0 && j == 0) continue;
if (i == 0){
DP[i][j] = DP[i][j-1] + abs(r[i] - b[j]);
continue;
}
if (j == 0){
DP[i][j] = DP[i-1][j] + abs(r[i] - b[j]);
continue;
}
DP[i][j] = min(min(DP[i-1][j], DP[i][j-1]), DP[i-1][j-1]) + abs(r[i] - b[j]);
}
}
return DP[n-1][m-1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1936 KB |
Output is correct |
2 |
Correct |
0 ms |
1936 KB |
Output is correct |
3 |
Correct |
0 ms |
1936 KB |
Output is correct |
4 |
Correct |
0 ms |
1936 KB |
Output is correct |
5 |
Correct |
0 ms |
1936 KB |
Output is correct |
6 |
Correct |
0 ms |
1936 KB |
Output is correct |
7 |
Correct |
0 ms |
2124 KB |
Output is correct |
8 |
Correct |
0 ms |
2124 KB |
Output is correct |
9 |
Correct |
0 ms |
2120 KB |
Output is correct |
10 |
Correct |
1 ms |
2128 KB |
Output is correct |
11 |
Correct |
0 ms |
2124 KB |
Output is correct |
12 |
Correct |
0 ms |
2120 KB |
Output is correct |
13 |
Correct |
1 ms |
2128 KB |
Output is correct |
14 |
Correct |
1 ms |
2128 KB |
Output is correct |
15 |
Correct |
1 ms |
2128 KB |
Output is correct |
16 |
Correct |
0 ms |
2124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1936 KB |
Output is correct |
2 |
Correct |
0 ms |
1936 KB |
Output is correct |
3 |
Correct |
33 ms |
3112 KB |
Output is correct |
4 |
Correct |
34 ms |
3112 KB |
Output is correct |
5 |
Correct |
24 ms |
3112 KB |
Output is correct |
6 |
Correct |
33 ms |
3504 KB |
Output is correct |
7 |
Correct |
33 ms |
3504 KB |
Output is correct |
8 |
Correct |
59 ms |
3504 KB |
Output is correct |
9 |
Correct |
32 ms |
3504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1936 KB |
Output is correct |
2 |
Correct |
0 ms |
1936 KB |
Output is correct |
3 |
Runtime error |
36 ms |
3504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1936 KB |
Output is correct |
2 |
Runtime error |
43 ms |
3504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1936 KB |
Output is correct |
2 |
Correct |
0 ms |
1936 KB |
Output is correct |
3 |
Correct |
0 ms |
1936 KB |
Output is correct |
4 |
Correct |
0 ms |
1936 KB |
Output is correct |
5 |
Correct |
0 ms |
1936 KB |
Output is correct |
6 |
Correct |
0 ms |
1936 KB |
Output is correct |
7 |
Correct |
0 ms |
2124 KB |
Output is correct |
8 |
Correct |
0 ms |
2124 KB |
Output is correct |
9 |
Correct |
0 ms |
2120 KB |
Output is correct |
10 |
Correct |
1 ms |
2128 KB |
Output is correct |
11 |
Correct |
0 ms |
2124 KB |
Output is correct |
12 |
Correct |
0 ms |
2120 KB |
Output is correct |
13 |
Correct |
1 ms |
2128 KB |
Output is correct |
14 |
Correct |
1 ms |
2128 KB |
Output is correct |
15 |
Correct |
1 ms |
2128 KB |
Output is correct |
16 |
Correct |
0 ms |
2124 KB |
Output is correct |
17 |
Correct |
0 ms |
1936 KB |
Output is correct |
18 |
Correct |
0 ms |
1936 KB |
Output is correct |
19 |
Correct |
33 ms |
3112 KB |
Output is correct |
20 |
Correct |
34 ms |
3112 KB |
Output is correct |
21 |
Correct |
24 ms |
3112 KB |
Output is correct |
22 |
Correct |
33 ms |
3504 KB |
Output is correct |
23 |
Correct |
33 ms |
3504 KB |
Output is correct |
24 |
Correct |
59 ms |
3504 KB |
Output is correct |
25 |
Correct |
32 ms |
3504 KB |
Output is correct |
26 |
Correct |
0 ms |
1936 KB |
Output is correct |
27 |
Correct |
0 ms |
1936 KB |
Output is correct |
28 |
Runtime error |
36 ms |
3504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
29 |
Halted |
0 ms |
0 KB |
- |