Submission #225196

#TimeUsernameProblemLanguageResultExecution timeMemory
225196VimmerSimfonija (COCI19_simfonija)C++14
44 / 110
49 ms3736 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("fast-math") //#pragma GCC optimize("no-stack-protector") #define F first #define S second #define sz(x) int(x.size()) #define pb push_back #define N 200005 #define MOD ll(998244353) using namespace std; typedef long long ll; typedef long double ld; int main() { ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, k; cin >> n >> k; ll a[n], b[n], pr[n + 1]; pr[n] = 0; for (ll i = 0; i < n; i++) cin >> a[i]; for (ll i = 0; i < n; i++) cin >> b[i]; vector <ll> g(n); for (ll i = 0; i < n; i++) g[i] = a[i] - b[i]; sort(g.begin(), g.end()); g.pb(1e18); for (ll i = 0; i < n; i++) { pr[i] = 0; if (i != 0) pr[i] = pr[i - 1]; pr[i] += g[i]; } ll ans = 1e18, l = 0, r = n - k - 1; while (r < n) { ll mdr = (r - l + 1) / 2; ll mdl = mdr - 1; if (mdr >= 0 && mdr < n) { ll x = g[mdr]; ll pos = upper_bound(g.begin(), g.end(), x) - g.begin(); ll posr = lower_bound(g.begin(), g.end(), 0) - g.begin(); if (x < 0) { ll sum = 0; if (posr <= r) sum = abs((r - max(posr, l) + 1) * x) + (pr[r] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1])); if (pos >= l) sum += abs(pr[min(pos - 1, r)] - (l == 0 ? 0 : pr[l - 1]) - x * (min(pos - 1, r) - l + 1)); sum += pr[min(r, posr - 1)] - (max(pos, l) == 0 ? 0 : pr[max(pos, l) - 1]) + abs(x * (min(posr - 1, r) - max(pos, l) + 1)); ans = min(ans, sum); } else { ll sum = 0; if (posr - 1 >= l) sum = (posr - l) * x + abs(pr[posr - 1] - (l == 0 ? 0 : pr[l - 1])); if (pos <= r) sum += pr[r] - (pos == 0 ? 0 : pr[pos - 1]) - (r - pos + 1) * x; sum += abs(pr[min(r, pos - 1)] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1]) - x * (min(r, pos - 1) - max(posr, l) + 1)); ans = min(ans, sum); } } if (mdl >= 0 && mdl < n) { ll x = g[mdl]; ll pos = upper_bound(g.begin(), g.end(), x) - g.begin(); ll posr = lower_bound(g.begin(), g.end(), 0) - g.begin(); if (x < 0) { ll sum = 0; if (posr <= r) sum = abs((r - max(posr, l) + 1) * x) + (pr[r] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1])); if (pos >= l) sum += abs(pr[min(pos - 1, r)] - (l == 0 ? 0 : pr[l - 1]) - x * (min(pos - 1, r) - l + 1)); sum += pr[min(r, posr - 1)] - (max(pos, l) == 0 ? 0 : pr[max(pos, l) - 1]) + abs(x * (min(posr - 1, r) - max(pos, l) + 1)); ans = min(ans, sum); } else { ll sum = 0; if (posr - 1 >= l) sum = (posr - l) * x + abs(pr[posr - 1] - (l == 0 ? 0 : pr[l - 1])); if (pos <= r) sum += pr[r] - (pos == 0 ? 0 : pr[pos - 1]) - (r - pos + 1) * x; sum += abs(pr[min(r, pos - 1)] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1]) - x * (min(r, pos - 1) - max(posr, l) + 1)); ans = min(ans, sum); } } l++; r++; } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...