zoukankan      html  css  js  c++  java
  • 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
    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.

    题解:摘自codeforce

    The problem can be interpreted as follows: array B is fixed and a total of k1 + k2 = K operations allowed on A. Let the array C be defined as Ci = |Ai - Bi| Now this is just a simple greedy problem where value of is to be minimized after exactly K subtraction/addition operations spread over the elements of array C.

    1. Till E is non-zero, the largest element is chosen and 1 is subtracted from it. This is done as currently we want to maximize the reduction in error per operation and decreasing an element x by 1 reduces error by x2 - (x - 1)2 = 2·x - 1.
    2. Once all the elements become zero, we can use the remaining moves to alternately increase and decrease the same element till we run out of moves.

    This can be implemented using a priority queue or by sorting the array C and iterating over it.

    Expected complexity: O(N·K·log(N)) or O(N·log(N))

    感受:好题。和wanfly1交流赛的B有相同的特点,不过需要思考得多一点。将k1和k2看作是同一种操作,然后固定一个序列,转化为对序列c得操作。贪心体现在使用一次操作,最多能减少2*x-1,与x有关,所以要不断找最大值来进行操作。好题!!!!,同时学习了一波代码。

     1 #pragma warning(disable:4996)
     2 #include<bitset>
     3 #include<queue>
     4 #include<vector>
     5 #include<string>
     6 #include<cstdio>
     7 #include<cstring>
     8 #include<iostream>
     9 #include<algorithm>
    10 using namespace std;
    11 typedef long long ll;
    12 
    13 int n, k1, k2;
    14 priority_queue<ll> p;
    15 
    16 int main()
    17 {
    18     while (cin >> n >> k1 >> k2) {
    19         vector<ll> a(n), b(n), arr(n);
    20         int k = k1 + k2;
    21         for (int i = 0; i < n; i++) cin >> a[i];
    22         for (int i = 0; i < n; i++) {
    23             cin >> b[i];
    24             arr[i] = abs(a[i] - b[i]);
    25             p.push(arr[i]);
    26         }
    27         while (k > 0) {
    28             ll tp = p.top();
    29             p.pop();
    30             p.push(abs(tp - 1));  //一定要取绝对值,当最大值是0的时候。
    31             k--;
    32         }
    33         ll ans = 0;
    34         while (!p.empty()) {
    35             ll tp = p.top();
    36             p.pop();
    37             ans += tp * tp;
    38         }
    39         cout << ans << endl;
    40     }
    41 
    42 }
  • 相关阅读:
    mysqldump 备份导出数据排除某张表
    PHP中cookies跨目录无法调用解决办法
    数据库之mac上mysql root密码忘记或权限错误的解决办法
    mac攻略(4) -- 使用brew配置php7开发环境(mac+php+apache+mysql+redis)
    js中event.keyCode用法及keyCode对照表
    BigDecimal用法详解
    Java开发中的23种设计模式详解(转)
    常用正则表达式大全 (转)
    sql查询重复记录、删除重复记录方法大全
    使用连接来代替in和not in(使用外连接技巧)
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8798338.html
Copyright © 2011-2022 走看看