Submission #321238

#TimeUsernameProblemLanguageResultExecution timeMemory
321238shivensinha4Simfonija (COCI19_simfonija)C++17
44 / 110
36 ms11728 KiB
#include "bits/stdc++.h" using namespace std; #define for_(i, s, e) for (int i = s; i < (int) e; i++) #define for__(i, s, e) for (ll i = s; i < e; i++) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; #define endl '\n' int main() { #ifdef shiven freopen("test.in", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<ll> nums(n); for_(i, 0, n) cin >> nums[i]; ll init = 0; for_(i, 0, n) { int v; cin >> v; nums[i] -= v; init += abs(nums[i]); } sort(nums.begin(), nums.end()); vector<ll> neg, pos, geq, leq, changeNeg, changePos, prefNeg, prefPos, origNeg, origPos, sufNeg, sufPos; for_(i, 0, n) { if (nums[i] == 0) continue; if (nums[i] < 0) { neg.push_back(-nums[i]); leq.push_back(i+1); } else { pos.push_back(nums[i]); geq.push_back(n-i); } } reverse(neg.begin(), neg.end()); reverse(leq.begin(), leq.end()); prefNeg = sufNeg = origNeg = neg; prefPos = sufPos = origPos = pos; for_(i, 1, prefNeg.size()) prefNeg[i] += prefNeg[i-1]-neg[i-1]; for_(i, 1, prefPos.size()) prefPos[i] += prefPos[i-1]-pos[i-1]; for (int i = neg.size()-2; i >= 0; i--) sufNeg[i] += sufNeg[i+1]; for (int i = pos.size()-2; i >= 0; i--) sufPos[i] += sufPos[i+1]; int negRem = min(k, (int) neg.size()), posRem = min(k-negRem, (int) pos.size()); int lt = min((int) pos.size()-posRem, negRem)+1; for_(i, 0, neg.size()) { if (i > 0) origNeg[i] -= neg[i-1]; origNeg[i] *= 2*leq[i]-n; if (i > 0) origNeg[i] += origNeg[i-1]; } for_(i, 0, pos.size()) { if (i > 0) origPos[i] -= pos[i-1]; origPos[i] *= 2*geq[i]-n; if (i > 0) origPos[i] += origPos[i-1]; } ll ans = init; for_(iter, 0, lt) { ll curr = ans - (posRem > 0 ? sufPos[n-posRem] : 0) - (negRem > 0 ? sufNeg[n-negRem] : 0); // cout << curr << endl; int negLeft = neg.size()-negRem, posLeft = pos.size()-posRem; if (negLeft > posLeft) { int l = 0, r = neg.size()-negRem, idx = -1; while (l < r) { int mid = (l+r)/2; if (2*leq[mid]-n+2*iter >= 0) { idx = mid; l = mid+1; } else r = mid; } // cout << idx << " ... " << origNeg[idx] << endl; if (idx != -1) curr -= origNeg[idx] + prefNeg[idx]*2*iter; } else if (posLeft > negLeft) { int l = 0, r = pos.size()-posRem, idx = -1; while (l < r) { int mid = (l+r)/2; if (2*geq[mid]-n-2*iter >= 0) { idx = mid; l = mid+1; } else r = mid; } if (idx != -1) curr -= origPos[idx] - prefPos[idx]*2*iter; // what really happened here? } ans = min(ans, curr); negRem -= 1; posRem += 1; } cout << ans << endl; return 0; }
#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...