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>
#include "wiring.h"
using namespace std;
const int MN = 2e5 + 3;
long long min_total_length (vector<int> r, vector<int> b) {
sort(r.begin(),r.end()); sort(b.begin(),b.end());
if (r.back() < b[0]) {
long long lSum = 0, rSum = 0;
for (int i : r) lSum += i;
for (int i : b) rSum += i;
return rSum - (long long)b[0] * ((int)b.size()) + (long long)r.back() * ((int)r.size()) - lSum + (long long)(b[0] - r.back()) * max((int)b.size(),(int)r.size());
}
vector<pair<int,int>> v = {{-1,-1}};
for (int i : r) v.emplace_back(i,0);
for (int i : b) v.emplace_back(i,1);
int n = (int)v.size() - 1;
sort(v.begin(),v.end());
vector<long long> dp(n + 1,1e18); dp[0] = 0;
for (int i = 1; i <= n; i++) {
int j = i; long long rSum = 0, lSum = 0;
while (j > 0 && v[j].second == v[i].second) rSum += v[j--].first;
int k = j;
while (k > 0) {
lSum += v[k].first;
dp[i] = min(dp[i],min(dp[k],dp[k-1]) + rSum - (long long)v[j+1].first * (i - j) + (long long)v[j].first * (j - k + 1) - lSum + (long long)(v[j+1].first - v[j].first) * max(j-k+1,i-j));
if (k == 1 || v[k-1].second == v[i].second) break;
--k;
}
}
return dp[n];
}
# | 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... |