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>
#define N 200005
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
int n,m,k;
P a[N];
ll dp[N],mp[N],l[N],r[N],d[N],s[N],plc=1e5,rp,bp;
bool vis[N];
ll min_total_length(vector<int> R,vector<int> B)
{
n=R.size(),m=B.size(),k=n+m;
for(int i=1;i<=n;i++)
a[i]=P(R[i-1],1);
for(int i=1;i<=m;i++)
a[i+n]=P(B[i-1],0);
sort(a+1,a+1+k);
rp=bp=-1;
for(int i=1;i<=k;i++){
if(a[i].y){
rp=a[i].x;
l[i]=bp;
}
else{
bp=a[i].x;
l[i]=rp;
}
}
rp=bp=-1;
for(int i=k;i>=1;i--){
if(a[i].y){
rp=a[i].x;
r[i]=bp;
}
else{
bp=a[i].x;
r[i]=rp;
}
}
vis[plc]=true;
for(int i=1;i<=k;i++){
if(l[i]==-1)
d[i]=r[i]-a[i].x;
if(r[i]==-1)
d[i]=a[i].x-l[i];
if(l[i]!=-1&&r[i]!=-1)
d[i]=min(r[i]-a[i].x,a[i].x-l[i]);
}
for(int i=1;i<=k;i++){
dp[i]=dp[i-1]+d[i];
if(a[i].y){
plc++;
s[i]=s[i-1]+a[i].x;
}
else{
plc--;
s[i]=s[i-1]-a[i].x;
}
if(vis[plc])
dp[i]=min(dp[i],dp[mp[plc]]+abs(s[i]-s[mp[plc]]));
vis[plc]=true;
mp[plc]=i;
}
return dp[k];
}
# | 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... |