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"
#define fin(i,s,n) for(auto i = s; i < n; ++i)
#define fine(i,s,n) for(auto i = s; i <= n; ++i)
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define all(x) (x).begin(),(x).end()
#define chkmin(a,b) a = min(a,b)
#define chkmax(a,b) a = max(a,b)
using stdvec = vector<int>;
#define int int64_t
using ii = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vii = vector<ii>;
const int inf = 1e17;
#define sz(a) int((a).size())
ii bop(stdvec& a, int x) {
if(x<a[0]) return {0,0};
if(x>a.back()) return {sz(a)-1,sz(a)-1};
int idx = lower_bound(all(a),x)-a.begin();
return {idx-1,idx};
}
long long min_total_length(stdvec r, stdvec b) {
int n = r.size(), m = b.size();
r.pb(2e9+5);
vector<set<int>> match(n);
set<int> multi;
int i = 0, j = 0;
int ans = 0;
while(j < m) {
if(match[i].empty() || abs(r[i]-b[j])<abs(r[i+1]-b[j])) {
ans += abs(r[i]-b[j]);
match[i].insert(j++);
if(match[i].size()>1) multi.insert(i);
} else ++i;
}
while(i < n){
ii ops = bop(b,r[i]);
int best = min(abs(r[i]-b[ops.x]),abs(r[i]-b[ops.y]));
if(!multi.empty()) {
int lm = *multi.rbegin();
int lj = *match[lm].rbegin();
int cost = abs(r[i]-b[lj])-abs(r[lm]-b[lj]);
if(cost<best) {
best = cost;
match[lm].erase(lj);
if(match[lm].size()==1) multi.erase(lm);
}
}
++i;
ans += best;
}
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... |