이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 403;
const long long inf = 1e16;
long long f(vector <int> &v1, vector <int> &v2)
{
long long ans = 0;
ans += 1ll * max((int)v1.size(), (int)v2.size()) * (v2[0] - v1.back());
for (int i = 0; i < (int)v1.size()-1; i++)
ans += 1ll * (v1[i+1] - v1[i]) * (i + 1);
for (int i = 0; i < (int)v2.size()-1; i++)
ans += 1ll * (v2[i+1] - v2[i]) * ((int)v2.size() - i - 1);
return ans;
}
long long dp[maxn][maxn];
long long min_total_length(vector <int> r, vector <int> b)
{
vector <pair <int, bool> > v;
for (auto i: r)
v.push_back({i, false});
for (auto i: b)
v.push_back({i, true});
sort (v.begin(), v.end());
vector <vector <int> > seg;
vector <int> curr;
curr.push_back(v[0].first);
for (int i = 1; i < (int)v.size(); i++)
{
if (v[i].second != v[i-1].second)
{
seg.push_back(curr);
curr.clear();
curr.push_back(v[i].first);
}
else
curr.push_back(v[i].first);
}
seg.push_back(curr);
for (int i = 0; i < (int)seg[0].size(); i++)
dp[0][i] = inf;
dp[0][(int)seg[0].size()] = 0;
for (int i = 1; i < (int)seg.size(); i++)
{
dp[i][(int)seg[i].size()] = dp[i-1][0];
for (int j = 0; j < (int)seg[i].size(); j++)
{
dp[i][j] = inf;
if (j != 0)
dp[i][j] = dp[i][j-1];
for (int k = 1; k <= (int)seg[i-1].size(); k++)
{
int to_match = (int)seg[i].size()-j;
vector <int> v1, v2;
for (int p = 0; p < to_match; p++)
v1.push_back(seg[i][p]);
for (int p = (int)seg[i-1].size()-k; p < (int)seg[i-1].size(); p++)
v2.push_back(seg[i-1][p]);
dp[i][j] = min(dp[i][j], f(v2, v1) + dp[i-1][k]);
}
}
}
return dp[(int)seg.size()-1][0];
}
/*
int n, m;
vector <int> r, c;
int main()
{
cin >> n >> m;
r.resize(n);
c.resize(m);
for (int i = 0; i < n; i++)
cin >> r[i];
for (int i = 0; i < m; i++)
cin >> c[i];
cout << min_total_length(r, c) << endl;
}
*/
# | 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... |