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 <algorithm>
#include <iostream>
#include <set>
using namespace std;
vector<pair<int, int> > v;
const long long INF = 1e17;
long long dp[200010], pref[200010];
int st[200010];
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int n = r.size() + b.size();
for(auto &k : r) v.emplace_back(k, 0);
for(auto &k : b) v.emplace_back(k, 1);
v.emplace_back(-1, -1);
sort(v.begin(), v.end());
st[1] = 1;
for(int i = 2; i <= n; i++){
if(v[i].second == v[i - 1].second) st[i] = st[i - 1];
else st[i] = i;
}
for(int i = 1; i <= n; i++){
dp[i] = INF;
pref[i] = pref[i - 1] + v[i].first;
if(st[i] == 1) continue;
if(st[i] == i){
for(int k = i - 1; k >= st[i - 1]; k--){
dp[i] = min(dp[i], dp[k - 1] + 1LL * (i - k) * v[i].first - (pref[i - 1] - pref[k - 1]));
}
}
dp[i] = min(dp[i], dp[i - 1] + v[i].first - v[st[i] - 1].first);
int len = i - st[i] + 1;
if(st[i] - len > 0) dp[i] = min(dp[i], dp[st[i] - len - 1] + (pref[i] - pref[st[i] - 1]) - (pref[st[i] - 1] - pref[st[i] - len - 1]));
// cerr << i << " " << dp[i] << "\n";
}
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... |