This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const ll inf = 1e18;
int n, m;
ll r[maxn], b[maxn], dp[210][210];
ll min_total_length(vector<int> R, vector<int> B)
{
n = R.size();
m = B.size();
for (int i = 1; i <= n; i ++)
r[i] = R[i - 1];
for (int i = 1; i <= m; i ++)
b[i] = B[i - 1];
r[0] = - inf;
b[0] = - inf;
if (n <= 200 && m <= 200)
{
for (int i = 0; i <= n; i ++)
for (int j = 0; j <= m; j ++)
dp[i][j] = inf;
dp[0][0] = 0;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= m; j ++)
{
dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1]));
dp[i][j] = dp[i][j] + abs(r[i] - b[j]);
}
}
return dp[n][m];
}
else
{
ll ans = 0;
int mx = min(n, m);
for (int i = 1; i <= mx; i ++)
{
int id1 = n - i + 1;
int id2 = i;
ans = ans + abs(r[id1] - b[id2]);
}
if (n > m)
{
for (int i = 1; i <= n - mx; i ++)
ans = ans + abs(r[i] - b[1]);
}
else
{
for (int i = m; i > mx; i --)
ans = ans + abs(b[i] - r[n]);
}
return ans;
}
}
# | 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... |