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];
int lastop[N+M+10]; //last opposite
ll S[N+M+10];
int lastsame[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);
for(int i=1;i<vec.size();i++) lastsame[i]=(vec[i-1].se==vec[i].se?i-1:lastop[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]=ll(1e18);}
dp[0]=0;
for(int i=1;i<=n+m;i++)
{
ll pos=vec[i].fi; int ty=vec[i].se;
//spam opposite sides;
{
if(lastop[i]>0) dp[i] = min(dp[i], dp[i-1]+pos-vec[lastop[i]].fi);
ll d=0; int curb=lastop[i]; int curr=i;
while(curr>curb&&curb>0)
{
d+=vec[curr].fi-vec[curb].fi;
curr=lastsame[curr]; curb=lastsame[curb];
}
if((curb==0&&curr==0)||(curb>0&&curr>0))
{
dp[i] = min(dp[i], dp[max(curr,curb)] + 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));
}
}
*/
}
}
return dp[n+m];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:51: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:52: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);
~^~~~~~~~~~~
wiring.cpp:53:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<vec.size();i++) lastsame[i]=(vec[i-1].se==vec[i].se?i-1:lastop[i-1]);
~^~~~~~~~~~~
wiring.cpp:60:25: warning: unused variable 'ty' [-Wunused-variable]
ll pos=vec[i].fi; int ty=vec[i].se;
^~
# | 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... |