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), psa(n+1); dp[0] = 0;
vector<int> lst(n+1);
for (int i = 1; i <= n; i++) lst[i] = i == 1 || v[i].second != v[i-1].second ? i : lst[i-1];
for (int i = 1; i <= n; i++) psa[i] = psa[i-1] + v[i].first;
vector<int> des(n+1);
int j = 0, pp = 0;
for (int i = 1; i <= n; i++) {
j = lst[i] - 1;
if (lst[i] != lst[i-1]) pp = j;
for (int k = pp; k > 0 && k >= lst[j]; k--) {
long long att = min(dp[k],dp[k-1]) + psa[i] - psa[j] - (long long)v[j+1].first * (i - j) + (long long)v[j].first * (j - k + 1) - (psa[j] - psa[k-1]) + (long long)(v[j+1].first - v[j].first) * max(j-k+1,i-j);
if (att < dp[i]) {
dp[i] = att;
pp = 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... |