이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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+=min(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... |