이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
#include "wiring.h"
const long long inf = 1e17;
long long min_total_length(vector<int> r, vector<int> b) {
int n = r.size(), m = b.size();
vector<long long> mndisr(n), mndisb(m);
int indb = 0, indr = 0;
for(int i = 0; i < n; i++){
while(indb + 1 < m && b[indb+1] < r[i]) indb++;
mndisr[i] = min((long long)abs(r[i] - b[indb]), (indb + 1 < m ? (long long)abs(r[i] - b[indb+1]) : inf));
}
for(int i = 0; i < m; i++){
while(indr + 1 < n && r[indr+1] < b[i]) indr++;
mndisb[i] = min((long long)abs(b[i] - r[indr]), (indr + 1 < n ? (long long)abs(b[i] - r[indr+1]) : inf));
}
vector<vector<long long> > dp(n+1, vector<long long>(m+1, inf));
dp[0][0] = 0;
for(int i = 0; i <= n; i++){
for(int j = 0; j <= m; j++){
if(i == n && j == m) continue;
if(j == m) dp[i+1][j] = min(dp[i+1][j], dp[i][j] + mndisr[i]);
else if(i == n) dp[i][j+1] = min(dp[i][j+1], dp[i][j] + mndisb[j]);
else if(i < n && j < m) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + abs(r[i] - b[j]));
}
}
return dp[n][m];
}
# | 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... |