#include <bits/stdc++.h>
using namespace std;
#include "wiring.h"
long long ara(long long a, long long b){
if (a>b) return a-b;
return b-a;
}
long long subtask1(vector<int> &r, vector<int> &b){
if (r.size()<b.size()) swap(r,b);
int n = r.size();
int m = b.size();
vector<vector<long long>> dp(n, vector<long long>(m,-1));
function<long long(int,int)> f;
f = [&](int x, int y)->long long{
if (dp[x][y]!=-1) return dp[x][y];
if (x==n-1 && y==m-1){
return ara(r[x],b[y]);
}
if (x==n-1){
return dp[x][y]=ara(r[x],b[y])+f(x,y+1);
}
if (y==m-1){
return dp[x][y]=ara(r[x],b[y])+f(x+1,y);
}
dp[x][y]=ara(r[x],b[y]);
long long mava = f(x+1,y+1);
mava=min(mava,f(x+1,y));
return dp[x][y]+=mava;
};
return f(0,0);
}
long long subtask2(vector<int> &r, vector<int> &b){
long long ans = 0;
reverse(r.begin(), r.end());
if (r.size()>b.size()){
for (int i = 0; i < b.size(); i++){
ans+=ara(r[i],b[i]);
}
for (int i = b.size(); i < r.size(); i++){
ans+=ara(b[0],r[i]);
}
}
else {
for (int i = 0; i < r.size(); i++){
ans+=ara(r[i],b[i]);
}
for (int i = r.size(); i < b.size(); i++){
ans+=ara(r[0],b[i]);
}
}
return ans;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
if (r.size()<=200 && b.size()<=200) return subtask1(r,b);
if (*max_element(r.begin(), r.end())<*min_element(b.begin(), b.end())) return subtask2(r,b);
}
Compilation message
wiring.cpp: In function 'long long int subtask2(std::vector<int>&, std::vector<int>&)':
wiring.cpp:37:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0; i < b.size(); i++){
| ~~^~~~~~~~~~
wiring.cpp:40:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = b.size(); i < r.size(); i++){
| ~~^~~~~~~~~~
wiring.cpp:45:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < r.size(); i++){
| ~~^~~~~~~~~~
wiring.cpp:48:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = r.size(); i < b.size(); i++){
| ~~^~~~~~~~~~
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:57:1: warning: control reaches end of non-void function [-Wreturn-type]
57 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '15280', found: '15530' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
14 ms |
1904 KB |
Output is correct |
4 |
Correct |
15 ms |
1988 KB |
Output is correct |
5 |
Correct |
15 ms |
1924 KB |
Output is correct |
6 |
Correct |
21 ms |
2360 KB |
Output is correct |
7 |
Correct |
19 ms |
2392 KB |
Output is correct |
8 |
Correct |
19 ms |
2252 KB |
Output is correct |
9 |
Correct |
19 ms |
2372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '17703', found: '19052' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '27', found: '30' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '15280', found: '15530' |
4 |
Halted |
0 ms |
0 KB |
- |