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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e15;
ll dp[200001],mn[200001],mn2[200001],val[200002],sum[200001];
pair <ll, int> position[200001];
int id,cur[2],l,r,lo;
ll min_total_length(vector <int> a, vector <int> b){
for (int i:a)
position[++id]={i,0};
for (int i:b)
position[++id]={i,1};
sort(position+1,position+id+1);
for (int i=id;i;i--){
val[i]=cur[position[i].second^1]-position[i].first;
if (i<id&&position[i+1].second==position[i].second)
val[i]+=val[i+1];
if (position[i].second!=position[i-1].second)
cur[position[i].second]=position[i].first;
}
for (int i=1;i<=id;i++)
sum[i]=sum[i-1]+position[i].first;
mn[0]=val[1];
for (int i=1;i<=id;i++){
dp[i]=INF;
if (l){
if (l==r)
dp[i]=min(dp[r-1],dp[r])+sum[i]-sum[r]-position[r].first*(i-r);
else{
dp[i]=mn2[max(r*2-i,l-1)]+sum[i]-sum[r]-position[r].first*(i-r);
if (i-r<r-l+1)
dp[i]=min(dp[i],mn[r*2-i]-val[r*2-i+1]+sum[i]-sum[r]*2+sum[r*2-i]);
}
}
mn[i]=min(mn[i-1],dp[i]+val[i+1]);
if (i==1||position[i-1].second!=position[i].second){
lo=i;
mn[i]=min(dp[i-1]+val[i],dp[i]+val[i+1]);
}
if (position[i].second!=position[i+1].second){
l=lo;
r=i;
mn2[r]=INF;
for (int i=r-1;i>=l-1;i--)
mn2[i]=min(mn2[i+1],dp[i]+position[r].first*(r-i)-sum[r]+sum[i]);
}
}
return dp[id];
}
# | 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... |