# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
738395 | Elvin_Fritl | 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;
#include "wiring.h"
long long min_total_length(vector<int> r, vector<int> b){
set<int>s,s1;
vector<int>chek((int)b.size() + (int)r.size(),0);
for(int i=0;i<(int)r.size();i++){
s.insert(r[i]);
}
long long res=0;
for(int i=0;i<(int)b.size();i++){
s1.insert(b[i]);
auto it=s.upper_bound(b[i]);
auto it2=it;
if(it!=s.begin()){
it2--;
}
int tmp1=abs(*it - b[i]);
int tmp2=abs(*it2 - b[i]));
if(tmp1>tmp2){
chek[*it2]=1;
res+=tmp2;
}
else if(tmp1<tmp2){
chek[*it]=1;
res+=tmp1;
}
else{
if(chek[*it]==1){
chek[*it2]=1;
res+=tmp2;
}
else{
chek[*it]=1;
res+=tmp;
}
}
}
for(int i=0;i<(int)r.size();i++){
if(chek[r[i]]==0){
auto it=s1.upper_bound(r[i]);
auto it2=it;
if(it!=s1.begin()){
it2--;
}
int tmp1=abs(*it - r[i]);
int tmp2=abs(*it2 - r[i]));
if(tmp1>tmp2){
chek[*it2]=1;
res+=tmp2;
}
else if(tmp1<tmp2){
chek[*it]=1;
res+=tmp1;
}
else{
if(chek[*it]==1){
chek[*it2]=1;
res+=tmp2;
}
else{
chek[*it]=1;
res+=tmp;
}
}
}
}
return res;
}