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 <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <assert.h>
using namespace std;
long long N,M;
vector < int > x,y;
long long DP[25][200005];
bool have2[25][200005];
set < int > have;
map < int , int > cha;
int who1[100005];
int who2[100005];
int xx=0;
long long F(long long a,long long b)
{
if(a==N&&b==M) return 0;
if(a==N||b==M||abs(who2[b]-who1[a])>10) return 1e18;
if(have2[who2[b]-who1[a]+10][a]) return DP[who2[b]-who1[a]+10][a];
have2[who2[b]-who1[a]+10][a]=1;
DP[who2[b]-who1[a]+10][a]=(long long) abs(y[b]-x[a])+min(F(a+1,b+1),min(F(a+1,b),F(a,b+1)));
return DP[who2[b]-who1[a]+10][a];
}
long long min_total_length(vector< int > r, vector < int > b)
{
long long ans=0,i,now=0;
N=r.size();
M=b.size();
x=r;
y=b;
for(auto i:r) have.insert(i);
for(auto i:b) have.insert(i);
for(auto i:have) cha[i]=now++;
for(i=0;i<N;i++) who1[i]=cha[r[i]];
for(i=0;i<M;i++) who2[i]=cha[b[i]];
if(b[0]>r[N-1])
{
if(N<=M)
{
for(i=0;i<min(N,M);i++) ans+=(long long) b[i]-r[i];
for(i=N;i<M;i++) ans+=(long long) b[i]-r[N-1];
}
else
{
for(i=1;i<=M;i++) ans+=(long long) b[M-i]-r[N-i];
for(i=0;i<N-M;i++) ans+=(long long) b[0]-r[i];
}
return ans;
}
else return F(0,0);
}
# | 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... |