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;
#define int long long
#define X first
#define Y second
#define pb push_back
void minn(int &a,int b){
if(b<a) a=b;
}
void maxx(int &a,int b){
if(b<a) a=b;
}
using ll =long long;
const int MAX=2e5+2,inf=2e9+4;
const ll oo=1e16;
using ll =long long;
int dp[2][MAX];
int F[MAX],D[MAX];
#undef int
long long min_total_length(std::vector<int> r, std::vector<int> b)
#define int long long
{
int n=r.size(),m=b.size();
r.pb(inf);b.pb(inf);
bool tt=(r[0]>b[0]);
int ans=0;
int u=0,v=0;
int cn=0;
memset(dp,0x3f,sizeof dp);
dp[!tt][0]=0;
while(u!=n || v!=m){
if(tt){
int st=v;
while(b[v]<r[u]) v++;
int pre;
if(u==0) pre=-inf;
else pre=r[u-1];
int res=0,pl=b[st]-pre;
F[cn+1]=oo;
for(int i=cn;i>=0;i--)
F[i]=min(F[i+1]+pl,dp[!tt][i]);
D[0]=dp[!tt][0];
for(int i=1;i<=cn;i++)
D[i]=min(D[i-1],dp[!tt][i]);
for(int i=st;i<=v;i++){
dp[tt][v-i]=res+min(F[i-st],D[min(i-st,cn)]);
res+=b[i]-pre;
}
res=0;
for(int i=v-1;i>=st;i--){
res+=b[v-1]-b[i];
dp[tt][v-i]+=res;
}
for(int i=cn;i<=v-st;i++) F[i]=D[i]=oo;
cn=v-st;
}
else{
int st=u;
while(r[u]<b[v]) u++;
int pre;
if(v==0) pre=-inf;
else pre=b[v-1];
int res=0,pl=r[st]-pre;
F[cn+1]=oo;
for(int i=cn;i>=0;i--)
F[i]=min(F[i+1]+pl,dp[!tt][i]);
D[0]=dp[!tt][0];
for(int i=1;i<=cn;i++)
D[i]=min(D[i-1],dp[!tt][i]);
for(int i=st;i<=u;i++){
dp[tt][u-i]=res+min(F[i-st],D[min(i-st,cn)]);
res+=r[i]-pre;
}
res=0;
for(int i=u-1;i>=st;i--){
res+=r[u-1]-r[i];
dp[tt][u-i]+=res;
}
for(int i=cn;i<=u-st;i++) F[i]=D[i]=oo;
cn=u-st;
}
//cout<<"ST "<<tt<<' '<<cn<<' '<<dp[tt][cn]<<'\n';
//for(int i=0;i<=cn;i++) cout<<dp[tt][i]<<' ';cout<<'\n';
tt^=1;
}
ans=dp[!tt][0];
return ans;
}
# | 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... |