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;
typedef vector<int> vi;
typedef pair<ll,ll> ii;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 100111;
const int M = 100111;
const int INF = int(1e9)+19;
ll dp[N+M+10][2];
int lastop[N+M+10]; //last opposite
ll S[N+M+10];
int nxtop[N+M+10];
ll sum(int l, int r)
{
if(l==0) return S[r];
else return S[r]-S[l-1];
}
long long min_total_length(std::vector<int> r, std::vector<int> b)
{
int n = r.size(); int m = b.size();
r.pb(INF); b.pb(INF);
vector<ii> vec;
vec.pb(mp(-INF,-INF));
int ptrr=0; int ptrb=0;
for(int i=0;i<n+m;i++)
{
if(r[ptrr]<b[ptrb])
{
vec.pb(mp(r[ptrr],0));
ptrr++;
}
else
{
vec.pb(mp(b[ptrb],1));
ptrb++;
}
}
lastop[0]=0;
S[0]=0;
for(int i=1;i<vec.size();i++) S[i]=vec[i].fi+S[i-1];
for(int i=1;i<vec.size();i++) lastop[i]=(vec[i-1].se==vec[i].se?lastop[i-1]:i-1);
nxtop[int(vec.size())-1]=INF;
for(int i=int(vec.size())-2;i>=0;i--) nxtop[i]=(vec[i].se==vec[i+1].se?nxtop[i+1]:i+1);
for(int i=0;i<N+M+10;i++){dp[i][0]=dp[i][1]=ll(1e18);}
dp[0][0]=0;
for(int i=1;i<=n+m;i++)
{
ll pos=vec[i].fi; int ty=vec[i].se;
for(int j=0;j<2;j++)
{
//spam opposite sides;
if(!j)
{
ll d=0;
for(int k=i-1;vec[k].se==(ty^1);k--)
{
d+=pos-vec[k].fi;
dp[i][j] = min(dp[i][j], dp[k-1][0] + d);
}
int p = lastop[i];
if(p>0)
{
ll d = sum(p+1,i)-ll(i-p)*vec[p].fi;
//cerr<<i<<' '<<p<<' '<<d<<'\n';
dp[i][j] = min(dp[i][j], dp[p-1][0] + d);
if(i-p>=2&&vec[p-1].se==vec[p].se)
{
dp[i][j] = min(dp[i][j], dp[p-1][1] + d - (vec[p+1].fi - vec[p].fi));
}
}
}
if(j&&nxtop[i]<n+m) //connect to next
{
ll d = vec[nxtop[i]].fi-vec[i].fi;
dp[i][j] = min(dp[i][j], dp[i-1][0] + d);
}
//cerr<<i<<' '<<j<<' '<<dp[i][j]<<'\n';
}
}
return dp[n+m][0];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<vec.size();i++) S[i]=vec[i].fi+S[i-1];
~^~~~~~~~~~~
wiring.cpp:51:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<vec.size();i++) lastop[i]=(vec[i-1].se==vec[i].se?lastop[i-1]:i-1);
~^~~~~~~~~~~
# | 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... |