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<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define MottoHayaku ios::sync_with_stdio(0);cin.tie(0);
#define rep1(i,n) for(int i=1;i<=n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
#define pb push_back
//#define int ll
const int MOD=1e9+7;
const ll INF=1e18;
const int N=200005;
vector<pair<ll,pll> > edge;
vector<pll> ed[N];
ll p[N];
ll find_par(ll u){return u==p[u]?u:p[u]=find_par(p[u]);}
void unite(ll u,ll v){p[find_par(u)]=find_par(v);}
ll kruskal(ll n)
{
ll ret=0;
for(int i=1;i<=n;i++) p[i]=i;
sort(edge.begin(),edge.end());
for(auto e:edge)
{
if(find_par(e.S.F)==find_par(e.S.S)) continue;
unite(e.S.F,e.S.S);
ret+=e.F;
}
return ret;
}
ll min_total_length(vector<int> r,vector<int> b)
{
vector<pll> v;
ll ans=0,lastr=-INF,lastb=-INF,posr=-1,posb=-1;
for(int i=0;i<r.size();i++) v.pb({r[i],0});
for(int i=0;i<b.size();i++) v.pb({b[i],1});
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
if(v[i].S==0)
{
if(posb!=-1) edge.pb({v[i].F-lastb,{i+1,posb}});
lastr=v[i].F,posr=i+1;
}
else
{
if(posr!=-1) edge.pb({v[i].F-lastr,{i+1,posr}});
lastb=v[i].F,posb=i+1;
}
}
lastr=INF,lastb=INF,posb=-1,posr=-1;
for(int i=v.size()-1;i>=0;i--)
{
if(v[i].S==0)
{
if(posb!=-1) edge.pb({lastb-v[i].F,{i+1,posb}});
lastr=v[i].F,posb=i+1;
}
else
{
if(posr!=-1) edge.pb({lastr-v[i].F,{i+1,posr}});
lastb=v[i].F,posr=i+1;
}
}
//for(auto p:edge) cout<<p.F<<" "<<p.S.F<<" "<<p.S.S<<"\n";
return kruskal(v.size());
}
/*int main()
{
int a[4]={1,2,3,7},b[5]={0,4,5,9,10};
cout<<min_total_length(a,b);
}
*/
Compilation message (stderr)
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i=0;i<r.size();i++) v.pb({r[i],0});
| ~^~~~~~~~~
wiring.cpp:38:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0;i<b.size();i++) v.pb({b[i],1});
| ~^~~~~~~~~
wiring.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
wiring.cpp:36:8: warning: unused variable 'ans' [-Wunused-variable]
36 | ll ans=0,lastr=-INF,lastb=-INF,posr=-1,posb=-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... |