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>
#include "wiring.h"
#define ll long long
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb push_back
using namespace std;
long long min_total_length(vector<int> r,vector<int> b) {
int n=r.size(),m=b.size();
ll ans=0;
vector <int> used1(n),used2(m);
for(int i=0;i<n;i++){
used1[i]=1;
ll mn=1e18,pos=-1,mn2=1e18;
for(int j=0;j<m;j++){
if(!used2[j] && abs(r[i]-b[j])<mn){
mn=abs(r[i]-b[j]);
pos=j;
}
mn2=min(mn2,(ll)abs(r[i]-b[j]));
}
if(pos==-1)ans+=mn2;
else{
used2[pos]=1;
ans+=mn;
}
}
for(int i=0;i<m;i++){
if(!used2[i]){
used2[i]=1;
ll mn=1e18,pos=-1,mn2=1e18;
for(int j=0;j<n;j++){
if(!used1[j] && abs(b[i]-r[j])<mn){
mn=abs(b[i]-r[j]);
pos=j;
}
mn2=min(mn2,(ll)abs(b[i]-r[j]));
}
if(pos==-1)ans+=mn2;
else{
used1[pos]=1;
ans+=mn;
}
}
}
return ans;
}
/*signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
vector <int> a(n),b(m);
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<m;i++)cin>>b[i];
cout<<min_total_length(a,b);
}*/
/*
*/
# | 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... |