# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
363466 | Fysty | Wiring (IOI17_wiring) | C++17 | 0 ms | 0 KiB |
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(int r[],int b[])
{
vector<pll> v;
ll ans=0,lastr=-INF,lastb=-INF,posr=-1,posb=-1;
for(int i=0;i<sizeof(r);i++) v.pb({r[i],0});
for(int i=0;i<sizeof(b);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);
}
*/