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;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) {
return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) {
return y > x ? (x = y, true) : false;
}
void debug_out() {
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
cerr << ' ' << A;
debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
for (int i = 0; i < n; ++i) {
cerr << a[i] << ' ';
}
cerr << endl;
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T> &v) {
for (auto& x : v) {
stream << x << ' ';
}
return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
return stream << p.first << ' ' << p.second;
}
const ll INF64 = 1e18;
const int INF32 = 1e9;
const int maxN = 2e5 + 5;
int last[maxN], ptr[maxN];
ll sumA[maxN], sumB[maxN];
ll min_total_length(vector<int> A, vector<int> B) {
if (A.back() < B.back()) swap(A, B);
int n = A.size(), m = B.size();
vector<int> minDistA(n, INF32);
for (int i = 0; i < n; ++i) {
int j = lower_bound(all(B), A[i]) - B.begin();
if (j < m) {
chkmin(minDistA[i], labs(B[j] - A[i]));
}
if (j > 0) {
chkmin(minDistA[i], labs(B[j - 1] - A[i]));
}
}
vector<int> minDistB(m, INF32);
for (int j = 0; j < m; ++j) {
int i = lower_bound(all(A), B[j]) - A.begin();
if (i < n) {
chkmin(minDistB[j], labs(B[j] - A[i]));
}
if (i > 0) {
chkmin(minDistB[j], labs(B[j] - A[i - 1]));
}
}
vector<pii> a;
for (int i = 0; i < n; ++i) {
a.emplace_back(A[i], 0);
}
for (int j = 0; j < m; ++j) {
a.emplace_back(B[j], 1);
}
sort(all(a));
int N = a.size();
int bal = m;
memset(last, 255, sizeof last);
memset(ptr, 255, sizeof ptr);
last[bal] = 0;
for (int i = 0; i < N; ++i) {
sumA[i + 1] = sumA[i];
sumB[i + 1] = sumB[i];
if (a[i].se == 0) {
sumA[i + 1] += a[i].fi;
bal++;
} else {
sumB[i + 1] += a[i].fi;
bal--;
}
ptr[i + 1] = last[bal];
last[bal] = i + 1;
}
//mdebug(sumA, N + 1);
//mdebug(sumB, N + 1);
vector<ll> dp(N + 1, INF64);
dp[0] = 0;
for (int i = 0; i < N; ++i) {
dp[i + 1] = dp[i] + (a[i].se == 0 ? minDistA[lower_bound(all(A), a[i].fi) - A.begin()] : minDistB[lower_bound(all(B), a[i].fi) - B.begin()]);
int j = ptr[i + 1];
if (j != -1) {
//debug(i, j, dp[j]);
chkmin(dp[i + 1], dp[j] + labs((sumA[i + 1] - sumA[j]) - (sumB[i + 1] - sumB[j])));
}
//debug(i, dp[i + 1]);
}
return dp[N];
}
#ifdef DEBUG
int32_t main() {
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> A(n), B(m);
for (int& x : A) {
cin >> x;
}
for (int& x : B) {
cin >> x;
}
cout << min_total_length(A, B) << '\n';
return 0;
}
#endif
# | 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... |