Submission #321255

# Submission time Handle Problem Language Result Execution time Memory
321255 2020-11-11T18:05:16 Z shivensinha4 Simfonija (COCI19_simfonija) C++17
0 / 110
45 ms 7416 KB
#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 n, k; 

ll solve(vector<ll> nums) {
	ll init = 0;
	
	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);
		}
		init += abs(nums[i]);
	}
	
	if (!neg.size()) return init;
	
	k = min(k, (int) pos.size() + (int) neg.size());
	
	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 (int i = neg.size()-2; i >= 0; i--) sufNeg[i] += sufNeg[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()) leq[i] -= negRem;
	
	for_(i, 0, neg.size()) {
		if (i > 0) origNeg[i] -= neg[i-1];
		origNeg[i] *= 2*leq[i]-n+k;
		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+k;
		if (i > 0) origPos[i] += origPos[i-1];
	}
	
	vector<ll> incPos = pos, incNeg = neg;
	for_(i, 0, neg.size()) {
		incNeg[i] = (neg[i]-(i > 0 ? neg[i-1] : 0)) * 2;
		if (i > 0) incNeg[i] += 2*incNeg[i-1];
	}
	
	for_(i, 0, pos.size()) {
		incPos[i] = (pos[i] - (i > 0 ? pos[i-1] : 0)) * 2;
		if (i > 0) incPos[i] += 2*incPos[i-1];
	}
	
	// for (int i: neg) cout << i << " ";
	// cout << endl;
	
	ll ans = init;
	for_(iter, 0, lt) {
		ll curr = init - (posRem > 0 ? sufPos[pos.size()-posRem] : 0) - (negRem > 0 ? sufNeg[neg.size()-negRem] : 0);
		// cout << curr << " ... " << negRem << " " << posRem << 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+k+2*iter >= 0) {
					idx = mid; l = mid+1;
				} else r = mid;
			}
			
			if (idx != -1) {
				if (idx < neg.size()-negRem-2) curr -= origNeg[idx] + incNeg[idx]*((ll) neg.size()-negRem-idx-2);
				else curr -= origNeg[idx];
				// cout << idx << " " << (int) neg.size()-negRem-idx-2 << endl;
				// cout << idx << " " << origNeg[idx] << " " << prefNeg[ext] << " " << (int) neg.size()-negRem-ext-1 << endl;
			}
		}
		
		// cout << " -> " << curr << endl;
		ans = min(ans, curr);
		negRem -= 1; posRem += 1;
	}
	
	return ans;
}


int main() {
	#ifdef shiven
	freopen("test.in", "r", stdin);
	#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n >> k;
	vector<ll> nums(n);
	for_(i, 0, n) cin >> nums[i];
	
	for_(i, 0, n) {
		int v; cin >> v;
		nums[i] -= v;
	}
	
	sort(nums.begin(), nums.end());
	
	ll ans = solve(nums);
	for_(i, 0, n) nums[i] = -nums[i];
	reverse(nums.begin(), nums.end());
	ans = min(ans, solve(nums));
	// cout << "final: " << ans << endl;
	assert(ans >= 0);
	cout << ans << endl;
	
	return 0;
}

Compilation message

simfonija.cpp: In function 'll solve(std::vector<long long int>)':
simfonija.cpp:84:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     if (idx < neg.size()-negRem-2) curr -= origNeg[idx] + incNeg[idx]*((ll) neg.size()-negRem-idx-2);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 492 KB Execution killed with signal 6 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 38 ms 7140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 40 ms 7160 KB Execution killed with signal 6 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 40 ms 7180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 35 ms 7160 KB Execution killed with signal 6 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 42 ms 7160 KB Execution killed with signal 6 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 37 ms 7416 KB Execution killed with signal 6 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 45 ms 7268 KB Execution killed with signal 6 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 43 ms 7160 KB Execution killed with signal 6 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 43 ms 6884 KB Execution killed with signal 6 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -