960B - Minimize the error - CodeForces Solution


data structures greedy sortings *1500

Please click on ads to support us..

Python Code:

import heapq
def main():
    n,k1,k2 = [int(i) for i in input().split()]
    arr1 = [int(i) for i in input().split()]
    arr2 = [int(i) for i in input().split()]

    diff = []
    for i in range(len(arr1)):
        tempDiff = abs(arr1[i]-arr2[i])
        diff.append(-tempDiff)

    heapq.heapify(diff)

    for i in range(k1+k2):
        x = heapq.heappop(diff)
        if abs(x)>0:
            x = -(abs(x)-1)
        else:
            x = -1
        heapq.heappush(diff,x)

    minError = sum([x*x for x in diff])
    print(minError)
    
    

if __name__ == "__main__":
    main()

C++ Code:

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

int main(){
  ll n, k1, k2;
  scanf("%lld %lld %lld",&n,&k1,&k2);
  vector<long long int>st;
  vector<ll>v,a(n),b(n);
  ll k, item, temp, ans = 0;
  k = k1+k2;
  for(int i=0;i<n;i++){
    scanf("%lld",&a[i]);
  }
  for(int i=0;i<n;i++){
    scanf("%lld",&b[i]);
    temp = abs(a[i]-b[i]);
    v.push_back(temp);
  }
  sort(v.begin(),v.end());
  while(k>0){
    temp = v[v.size()-1];
    v.pop_back();
    temp -= 1;
    v.push_back(abs(temp));
    sort(v.begin(),v.end());
    k--;
  }
  for(int i=v.size()-1;i>=0;i--){
    temp = v[i];
    ans += pow(temp,2);
    v.pop_back();
  }
  printf("%lld",ans);
}


Comments

Submit
0 Comments
More Questions

873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales