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"
using ll = long long;
ll myabs(ll x){
if(x<0)return x*(-1);
return x;
}
typedef std::pair<ll,ll> pll;
long long bestcusto(std::vector<int> anl,ll z){
ll best=1e9;
for(auto&x:anl){
best=std::min(best,myabs(x-z));
}
return best;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
ll ans=0;
for(auto&x:r)ans+=bestcusto(b,x);
for(auto&x:b)ans+=bestcusto(r,x);
std::priority_queue<ll,std::vector<ll>,std::greater<ll>> abrer,abreb;
std::vector<pll> vec;
for(auto&x:r)vec.push_back({x,0});
for(auto&x:b)vec.push_back({x,1});
std::sort(vec.begin(),vec.end());
for(auto&x:vec){
ll cor=x.second;
ll pos=x.first;
if(!cor){///Vermelho
ll custo = bestcusto(b,pos);
ll delta = pos-custo;
ll melhor = 1e9;
if(abreb.size()){
melhor=abreb.top();
}
if(melhor+delta<0){
long long ganha = melhor+delta;
ans+=ganha;
abreb.pop();
abrer.push(-pos-custo-ganha);
}else abrer.push(-pos-custo);
}else {///Azul
ll custo = bestcusto(r,pos);
ll delta = pos-custo;
ll melhor = 1e9;
if(abrer.size()){
melhor=abrer.top();
}
if(melhor+delta<0){
long long ganha = melhor+delta;
ans+=ganha;
abrer.pop();
abreb.push(-pos-custo-ganha);
}else abreb.push(-pos-custo);
}
}
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... |