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;
const int nmax=2e5+42;
int n,red[nmax],m,blue[nmax];
int k;
pair<int/*x*/,int/*type*/> inp[nmax];
long long dp[nmax];
long long pref_cost[nmax],suff_cost[nmax];
long long min_total_length(std::vector<int> r, std::vector<int> b)
{
n=r.size();
m=b.size();
for(int i=0;i<n;i++)
red[i+1]=r[i];
for(int j=0;j<m;j++)
blue[j+1]=b[j];
for(int i=1;i<=n;i++)
inp[i]={red[i],0};
for(int j=1;j<=m;j++)
inp[n+j]={blue[j],1};
k=n+m;
sort(inp+1,inp+k+1);
dp[0]=0;
for(int i=1;i<=k;i++)dp[i]=1e18;
int start=1;
while(inp[start].second==inp[1].second)
{
dp[start]=1e18;
start++;
}
while(start<=k)
{
long long add_right=0;
int i_left=start-1;
while(i_left>=1&&inp[i_left].second==inp[start-1].second)i_left--;
i_left++;
int j_right=start;
while(j_right<=k&&inp[j_right].second==inp[start].second)j_right++;
j_right--;
for(int j=start+1;j<=j_right;j++)
suff_cost[j]=suff_cost[j-1]+(inp[j].first-inp[start].first);
for(int i=start-2;i>=i_left;i--)
pref_cost[i]=pref_cost[i+1]+(inp[start-1].first-inp[i].first);
/*
cout<<"pref_cost ";for(int i=i_left;i<start;i++)cout<<pref_cost[i]<<" ";cout<<endl;
cout<<"suff_cost ";for(int j=start;j<=j_right;j++)cout<<suff_cost[j]<<" ";cout<<endl;
*/
//type 1 - j-start-1>=start-i
long long mini=1e18;
for(int j=start;j<=j_right;j++)
{
int i=2*start-j-1;
if(i_left<=i)mini=min(mini,min(dp[i-1],dp[i])+pref_cost[i]);
dp[j]=min(dp[j],mini+1LL*(j-start+1)*(inp[start].first-inp[start-1].first)+suff_cost[j]);
//cout<<"type1 "<<j<<" "<<i<<" "<<dp[j]<<" "<<mini<<endl;
}
//type 2 - j-start-1<start-i
mini=1e18;
int i=i_left;
for(int j=j_right;j>=start;j--)
{
long long between=0;
int i_now=2*start-j-2;
while(i<=i_now)
{
mini=min(mini,pref_cost[i]+min(dp[i-1],dp[i])+1LL*i*(inp[start-1].first-inp[start].first));
//cout<<"type2 "<<j<<" "<<i<<" "<<dp[j]<<endl;
i++;
}
dp[j]=min(dp[j],suff_cost[j]+1LL*(inp[start].first-inp[start-1].first)*start+mini);
}
/*
for(int p=start;p<=j_right;p++)
cout<<p<<" -> "<<dp[p]<<endl;
*/
start=j_right+1;
}
return dp[k];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:98:23: warning: unused variable 'between' [-Wunused-variable]
98 | long long between=0;
| ^~~~~~~
wiring.cpp:51:19: warning: unused variable 'add_right' [-Wunused-variable]
51 | long long add_right=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... |