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;
ll min_total_length(vector<int> r, vector<int> b) {
int n=r.size()+b.size();
vector<pair<ll,ll> > g;
g.push_back({-1,-1});
for(int i=0;i<r.size();i++)g.push_back({r[i],0});
for(int i=0;i<b.size();i++)g.push_back({b[i],1});
sort(g.begin(),g.end());
vector<ll>dp(n+1),pr(n+1),lst(3*n,-1),nx(n+1);
vector<ll>prv={-1,-1};
for(int i=n;i>=1;i--)
{
nx[i]=prv[1-g[i].second];
prv[g[i].second]=i;
}
prv={-1,-1};
ll s=0;
lst[n]=0;
for(int i=1;i<=n;i++)
{
dp[i]=10000000000000000;
pr[i]=pr[i-1]+(2*g[i].second-1)*g[i].first;
s+=2*g[i].second-1;
if(nx[i]!=-1)
dp[i]=dp[i-1]+g[nx[i]].first-g[i].first;
prv[g[i].second]=i;
if(prv[1-g[i].second]!=-1)
{
dp[i]=min(dp[i],dp[i-1]+g[i].first-g[prv[1-g[i].second]].first);
if(lst[n+s]!=-1)
{
dp[i]=min(dp[i],dp[lst[n+s]]+abs(pr[i]-pr[lst[s+n]]));
}
}
//cout<<dp[i]<<" ";
lst[n+s]=i;
}
//cout<<endl;
return dp[n];
}
/*int main()
{
cout<<min_total_length({0,1,2,3}, {4,5,6,7});
}
*/
Compilation message (stderr)
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:9:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for(int i=0;i<r.size();i++)g.push_back({r[i],0});
| ~^~~~~~~~~
wiring.cpp:10:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for(int i=0;i<b.size();i++)g.push_back({b[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... |