This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Sylwia Sapkowska
#include <bits/stdc++.h>
#include "wiring.h"
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
typedef pair<int, int> T;
const long long oo = 1e18, oo2 = 1e9+7;
long long min_total_length(vector<int>a, vector<int>b) {
vector<T>tab;
vector<vector<int>>curr(2);
curr[0] = a;
curr[1] = b;
for (int i = 0; i<(int)a.size(); i++) tab.emplace_back(a[i], 0);
for (int i = 0; i<(int)b.size(); i++) tab.emplace_back(b[i], 1);
sort(tab.begin(), tab.end());
vector<int>paired((int)tab.size());
long long ans = 0;
vector<stack<int>>s(2);
for (int i = 0; i<(int)tab.size(); i++){
auto [pos, what] = tab[i];
if (s[what^1].empty()){
s[what].push(i);
} else {
ans += (long long)(pos-tab[s[what^1].top()].first);
paired[s[what^1].top()] = 1;
paired[i] = 1;
debug(pos, tab[s[what^1].top()].first);
s[what^1].pop();
}
}
debug(paired, ans);
for (int i = 0; i<(int)tab.size(); i++){
if (paired[i]) continue;
auto [pos, what] = tab[i];
long long tmp = oo;
int p = (int)(lower_bound(curr[what^1].begin(), curr[what^1].end(), pos) - curr[what^1].begin());
if (p < (int)curr[what^1].size()) tmp = min(tmp, (long long)curr[what^1][p] - pos);
if (p) tmp = min(tmp, (long long)pos-curr[what^1][p-1]);
ans += tmp;
}
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... |