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.
The first line contains three space-separated integers n (1 ≤ n ≤ 103), k1 and k2 (0 ≤ k1 + k2 ≤ 103, k1 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 a single integer — the minimum possible value of after doing exactly k1 operations on array A and exactly k2operations on array B.
2 0 0 1 2 2 3
2
2 1 0 1 2 2 2
0
2 5 7 3 4 14 4
1
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.
这题题意是给你两个数组a[],b[],你可以对a进行m1次+1或-1,对b进行m2次+1或-1,求从1到n-1的 pow(abs(a[i]-b[i]),2) 的和;
这题在当时没有补,结果在后面北理的比赛中。。
这题要注意long long 因为pow(abs(a[i]-b[i]),2)肯定会爆int;
下面是暴力的方法ps(我一直TLE,但为什么同学就可以过。。。感觉时间复杂度差不多)
wa:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,m1,m2;
long long int a[1010],b[1010];
cin>>t;
while(t--)
{
cin>>n>>m1>>m2;
for(int i = 0 ; i < n ; i++)
{
scanf("%lld",&a[i]);
}
for(int i = 0 ; i < n ; i++)
{
scanf("%lld",&b[i]);
a[i] = abs(a[i] - b[i]);
}
for(int i = 0 ; i < m1 + m2 ; i++)
{
int max = -1;
int judge;
for(int j = 0 ; j < n ; j++)
{
if(a[j]>max)
{
max = a[j];
judge = j;
}
}
if(max != 0)
{
a[judge]--;
}
else
{
a[judge]++;
}
}
long long int sum = 0;
for(int i = 0 ; i < n ; i++)
{
sum += pow(a[i],2);
}
cout<<sum<<endl;
}
}
ac:
#include<bits/stdc++.h>
using namespace std;
priority_queue<long long int> q;
int main(){
int n,m1,m2;
long long int a[1010],b[1010];
// while(cin>>t)
// {
// while(t--)
// {
cin>>n>>m1>>m2;
for(int i = 0 ; i < n ; i++)
{
cin>>a[i];
}
for(int i = 0 ; i < n ; i++)
{
cin>>b[i];
q.push(abs(a[i]-b[i]));
}
long long int t;
for(int i = 0 ; i < m1+ m2 ; i++)
{
t = q.top();
q.pop();
q.push(abs(t-1));
}
long long int sum = 0;
while(!q.empty())
{
sum += pow(q.top(),2);
q.pop();
}
cout<<sum<<endl;
// }
// }
return 0;
}
用了优先队列,先将两个数组相减依次放入队列中,优先队列默认最上面的就是最大的,每次只需要改变最上面的数字减一即可得出答案,还有如果m1,m2过大的话,答案只会在0或1之间注意不要减过头了;
这代码在codefores可以过,就不知道为什么在北理那里一直wa。。