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;
using ll = long long;
#define double long double
#define endl '\n'
#define sz(a) (int)a.size()
#define pb push_back
#define fs first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
ll const INF = LONG_LONG_MAX;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
ll n=sz(r), m=sz(b);
if(n>m){swap(n, m);swap(r, b);}
set<ll, greater<>> decr;
for(int i=0;i<m;i++)
{
decr.insert(b[i]);
}
map<int, bool> mark;
ll ans=0;
for(int i=0;i<n;i++)
{
auto it=decr.lower_bound(r[i]);
if(it==decr.end()){it--;}
ll v=(*it);
ans+=abs(v-r[i]);
decr.erase(v);
mark[v]=1;
}
for(int i=0;i<m;i++)
{
if(!mark[b[i]])
{
int lo=0, hi=n-1, mid=(lo+hi)/2, ind1=-1;
while(lo<=hi)
{
if(r[mid]<b[i]){ind1=mid;lo=mid+1;mid=(lo+hi)/2;}
else{hi=mid-1;mid=(lo+hi)/2;}
}
lo=0, hi=n-1, mid=(lo+hi)/2;
int ind2=-1;
while(lo<=hi)
{
if(r[mid]>b[i]){ind2=mid;hi=mid-1;mid=(lo+hi)/2;}
else{lo=mid+1;mid=(lo+hi)/2;}
}
if(ind1==-1){ans+=abs(r[ind2]-b[i]);}
else if(ind2==-1){ans+=abs(r[ind1]-b[i]);}
else{ans+=max(abs(r[ind2]-b[i]), abs(r[ind1]-b[i]));}
}
}
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... |