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 <bits/stdc++.h>
using namespace std;
const int maxn = 200003;
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];
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);
dp[0] = 0;
dp[1] = inf;
for (int i = 2; i <= (int)seg.size(); i++)
{
dp[i] = f(seg[i-2], seg[i-1]) + dp[i-2];
long long sum = 0;
for (auto j: seg[i-1])
sum += j - seg[i-2].back();
dp[i] = min(dp[i], sum + dp[i-1]);
}
return dp[(int)seg.size()];
}
/*
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... |