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);
vector<ll>prv={-1,-1};
ll s=0;
lst[n]=0;
for(int i=1;i<=n;i++)
{
pr[i]=pr[i-1]+(2*g[i].second-1)*g[i].first;
s+=2*g[i].second-1;
prv[g[i].second]=i;
if(prv[1-g[i].second]!=-1)
{
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]]));
}
}
else dp[i]=10000000000000000;
lst[n+s]=i;
}
return dp[n];
}
/*int main()
{
cout<<min_total_length({1, 2, 3, 7}, {0, 4, 5, 9, 10});
}
*/
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... |