이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) ((int)x.size())
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n,m;
vector<ll>R,B;
vector<vector<ll>> dp;
ll f(int i, int j) {
if(i == n && j == m) return 0;
if(dp[i][j] != -1) return dp[i][j];
dp[i][j] = 1e18;
if(j < m) {
for(int k = 0; k < n; ++k)
dp[i][j] = min(dp[i][j], f(i, j + 1) + abs(B[j] - R[k]));
}
if(i < n) {
for(int k = 0; k < m; ++k)
dp[i][j] = min(dp[i][j], f(i + 1, j) + abs(B[k] - R[i]));
}
if(i < n && j < m)
dp[i][j] = min(dp[i][j], f(i + 1, j + 1) + abs(B[j] - R[i]));
return dp[i][j];
}
ll solveSub1() {
dp.assign(n+1, vector<ll>(m+1, -1));
return f(0, 0);
}
ll solveSub2() {
ll ans = 0;
if(n >= m) {
for(int i = 0; i < n - m; ++i)
ans += B[0] - R[i];
for(int i = 0; i < m; ++i)
ans += B[i] - R[n - m + i];
}
else {
for(int i = 0; i < n; ++i)
ans += B[i] - R[i];
for(int i = 0; i < m - n; ++i)
ans += B[n + i] - R[n-1];
}
return ans;
}
ll min_total_length(vector<int> r, vector<int> b) {
n = sz(r);
m = sz(b);
R.resize(n),B.resize(m);
for(int i=0;i<n;++i)R[i]=r[i];
for(int i=0;i<m;++i)B[i]=b[i];
if(max(n, m) <= 200) return solveSub1();
return solveSub2();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |