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;
long long min_total_length(vector<int> r, vector<int> b){
vector<pair<long long,int>> v;
for(auto i:r){
v.push_back({i,0});
}
for(auto i:b){
v.push_back({i,1});
}
sort(v.begin(),v.end());
int n = v.size();
long long pref[n+1] = {0};
for(int i = 0;i<n;i++){
pref[i+1] = v[i].first;
pref[i+1]+=pref[i];
}
int st[n],en[n];
for(int i = 0;i<n;i++){
if(i==0||v[i].first!=v[i-1].first){
st[i] = i;
}else st[i] = st[i-1];
}
for(int i = n-1;i>=0;i--){
if(i==n-1||v[i].first!=v[i+1].first){
en[i] = i;
}else en[i] = en[i+1];
}
long long dp[n+1][2];
for(int i = 0;i<=n;i++)dp[i][0] = dp[i][1] = 1e18;
dp[0][0] = 0;
for(int i = 1;i<=n;i++){
int S = st[i-1];
int E = en[i-1];
if(S==i-1)dp[i][0] = min(dp[i][0],dp[i-1][1]);
int V = i-S;
if(S!=0)dp[i][0] = min(dp[i][0],dp[i-1][0]+abs(v[i-1].first-v[S-1].first));
if(S-V>=0)dp[i][0] = min(dp[i][0],dp[i-1][0]+pref[i]-pref[S]-(pref[S]-pref[S-V]));
if(E<n-1)dp[i][1] = min(dp[i][1],dp[i-1][0]+abs(v[i-1].first-v[E+1].first));
dp[i][0] = min(dp[i][0],dp[i][1]);
}
return dp[n][0];
}
# | 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... |