# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
530056 | Servus2022 | Wiring (IOI17_wiring) | C++14 | 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 "wiring.h"
#include <bits/stdc++.h>
#include <cassert>
#include <cstdio>
using namespace std;
vector < pair <int,int> > v;
vector <int> nr[200005];
long long sum;
void cupleaza (int poz)
{
int minim = 1e9;
int unde=0,j;
for (j=0;j<v.size();j++)
{
if (v[poz].second!=v[j].second)
{
if (abs(v[poz].first-v[j].first)<minim)
{
minim = abs(v[poz].first-v[j].first);
unde = j;
}
}
}
sum=sum+minim;
nr[unde].push_back(poz);
nr[poz].push_back(unde);
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int i;
for (i=0;i<r.size();i++)
{
v.push_back({r[i],0});
}
for (i=0;i<b.size();i++)
{
v.push_back({b[i],1});
}
sort (v.begin(),v.end());
for (i=0;i<v.size();i++)
{
if (nr[i].size()==0)
{
cupleaza(i);
}
}
return sum;
}
using namespace std;
int main() {
int n, m;
assert(2 == scanf("%d %d", &n, &m));
vector<int> r(n), b(m);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &r[i]));
for(int i = 0; i < m; i++)
assert(1 == scanf("%d", &b[i]));
long long res = min_total_length(r, b);
printf("%lld\n", res);
return 0;
}