[2018-4-8]BNUZ套题比赛div2 CodeForces 960B Minimize the error【补】

B. Minimize the error
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined . You have to perform exactly k1 operations on array A and exactly k2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.

Output the minimum possible value of error after k1 operations on array A and k2 operations on array B have been performed.

Input

The first line contains three space-separated integers n (1 ≤ n ≤ 103), k1 and k2 (0 ≤ k1 + k2 ≤ 103k1 and k2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.

Second line contains n space separated integers a1, a2, ..., an ( - 106 ≤ ai ≤ 106) — array A.

Third line contains n space separated integers b1, b2, ..., bn ( - 106 ≤ bi ≤ 106)— array B.

Output

Output a single integer — the minimum possible value of  after doing exactly k1 operations on array A and exactly k2operations on array B.

Examples
input
Copy
2 0 0
1 2
2 3
output
Copy
2
input
Copy
2 1 0
1 2
2 2
output
Copy
0
input
Copy
2 5 7
3 4
14 4
output
Copy
1
Note

In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)2 + (2 - 3)2 = 2.

In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)2 + (2 - 2)2 = 0. This is the minimum possible error obtainable.

In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)2 + (4 - 4)2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)2 + (4 - 5)2 = 1.

题意:n ,ka, kb 分别代表 数组长度, 数组a可以操作的次数, 数组b可以操作的次数。 操作是 对某个数加1或减1。要求 所有 (ai-bi)^2 相加的最小值。 

题解:设置一个优先队列 大顶, 将每一个(ai - bi)求绝对值 push 进队列, 所有操作数 k = ka + kb; 每次只需要对差距最大的 减一  即可暴力求出最小值;

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
	priority_queue<ll> q;
	ll n, ka, kb, a[1010], b[1010], tmp, ans = 0;
	cin >> n >> ka >> kb;
	ll k = ka + kb;
	for (ll i = 0;	i < n; i++)
		cin >> a[i];
	for (ll i = 0; i < n; i++) {
		cin >> b[i];
		q.push(abs(a[i]-b[i]));
	}
	while (k--) {
		tmp = q.top();
		q.pop();
		tmp = abs(--tmp);
		q.push(tmp);
	}
	while (!q.empty()) {
		ans += q.top() * q.top();
		q.pop();
	}
	printf("%lld\n", ans);
}
后记:  这题要用 long long 去存, 因为后面存 int*int 时会爆 int, 这个很关键。。。。。。

猜你喜欢

转载自blog.csdn.net/qq_40731186/article/details/79868557
今日推荐