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;
struct node{
long long u,v,cost;
};
struct DSU{
vector<int>parent;
vector<int>sz;
void build(int n){
parent.resize(n);
sz.resize(n);
for (int i = 0;i<n;++i){
parent[i] = i;
sz[i] = 1;
}
}
int findsets(int v){
if (v == parent[v])return v;
parent[v] = findsets(parent[v]);
return parent[v];
}
bool unionset(int a,int b){
int u = findsets(a);
int v = findsets(b);
if (u == v)return false;
if (sz[u] < sz[v])swap(u,v);
parent[v] = u;
sz[u]+=sz[v];
return true;
}
};
long long min_total_length(std::vector<int> r, std::vector<int> b) {
vector<node>edge;
map<int,int>deg;
long long ans = 0;
for (auto x:r){
for (auto y:b){
edge.push_back({x,y,abs(x - y)});
deg[x]++;
deg[y]++;
ans+=abs(x - y);
}
}
sort(edge.begin(),edge.end(),[&](auto x,auto y){
return x.cost > y.cost;
});
for (auto x:edge){
if (deg[x.u] > 1 && deg[x.v] > 1){
deg[x.u]--;
deg[x.v]--;
ans-=x.cost;
}
}
return ans;
}
# | 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... |