CodeForces - 960B- Minimize the error(思维--优先队列)

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 ≤ 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
Output a single integer — the minimum possible value of after doing exactly k1 operations on array A and exactly k2 operations on array B.

Examples
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
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.

有两个数组a,b
现在定义E为两个数组对应值的差的平方和(见题目公式)
对于数组a有k1个操作
对于数组b有k2个操作
每次操作可以把数组里面的任何一个数 +1 或者 -1
并且所有的操作要必须执行完毕
求最小的E

思路:
将数组里面对应值的差的绝对值 压进优先队列(大到小排序)
不难想到,对于数组a或者b的每一次操作,对绝对值的影响只有+1(加1)或者-1(减1),而我们若想要最后结果尽可能的小,那么就要去操作当前队列里面的最大值,使它的值-1。但是每一次操作之后,这个值就有可能不再是最大的值了,这时候如果要让结果尽可能小,还是要去操作当前队列里的最大值,所以这就是要用优先队列的目的。
上面说到还会有-1的情况,就是当队列里所有的元素都是0的时候,即数组全部对应相等的时候,因为题目说要执行完所有的操作,所以不得不让结果先变大(如果操作还没有执行完,可以再变小)

叙述很长,代码很短。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<utility>
#include<list>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(a,v)  memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define e  2.718281828459045
#define eps 1.0e-8
using namespace std;
typedef long long int LL;
typedef pair<int,int>pa;
const int MAXL(1e5);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
priority_queue<LL,vector<LL>,less<LL> >q;
LL a[MAXL+50];
LL b[MAXL+50];
int main()
{
    int n,k1,k2;
    scanf("%d%d%d",&n,&k1,&k2);
    LL x,y,delt;
    for(int i=1; i<=n; i++)
        scanf("%lld",&a[i]);
    for(int i=1; i<=n; i++)
        scanf("%lld",&b[i]);
    for(int i=1; i<=n; i++)
        q.push(abs(a[i]-b[i]));
    int k=k1+k2;
    while(k>0)
    {
        int t=q.top();
        q.pop();
        if(t==0) t++;
        else t--;
        k--,q.push(t);
    }
    LL ans=0;
    while(!q.empty())
        ans+=(q.top()*q.top()),q.pop();
    cout<<ans<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值