Submission #100579

#TimeUsernameProblemLanguageResultExecution timeMemory
100579fbosnjakSimfonija (COCI19_simfonija)C++14
99 / 110
53 ms4596 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long ll N, K; ll a[100100], b[100100]; vector <ll> v; ll sol = 1e18; ll low, high, curr, sum, lok, bris_sum; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> K; for (int i = 0; i < N; i++) { cin >> a[i]; } for (int i = 0; i < N; i++) { cin >> b[i]; } if (K == N) {cout << 0; return 0;} for (int i = 0; i < N; i++) { v.push_back(a[i] - b[i]); } sort(v.begin(), v.end()); /*for (int i = 0; i < N; i++) { cout << v[i] << ' '; } cout << endl;*/ low = 0; high = N - K; curr = v[0]; for (int i = 0; i < N; i++) { sum += abs(curr - v[i]); } int sz = (int)v.size(); while (lok < sz && v[lok] <= curr) lok++; //cout << lok << endl; for (int i = N - K; i < N; i++) { bris_sum += (v[i] - curr); } //cout << sum << ' ' << bris_sum << endl; sol = sum - bris_sum; //cout << "high " << high << endl; for (int i = curr + 1; i <= v[sz - 1]; i++) { sum += lok; sum -= (N - lok); //cout << "sum " << sum << endl; while (lok < sz && v[lok] <= i) lok++; bris_sum += low; bris_sum -= (N - high); if (K > 0) { //cout << "uso sam " << endl; //cout << high << ' ' << N << endl; //cout << abs(v[low] - i) << ' ' << abs(v[high] - i) << endl; while (high < N && abs(v[low] - i) >= abs(v[high] - i)) { bris_sum -= abs(v[high] - i); bris_sum += abs(v[low] - i); low++; high++; } } //cout << "bris_sum " << bris_sum << endl; sol = min(sol, sum - bris_sum); } cout << sol; }
#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...