Advertisement
dmkozyrev

b.cpp

Apr 7th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef int64_t Int;
  6.  
  7. int main() {
  8.     //freopen("input.txt", "rt", stdin);
  9.    
  10.     ios_base::sync_with_stdio(false);
  11.    
  12.     Int n, ka, kb; cin >> n >> ka >> kb;
  13.    
  14.     vector<Int> a(n), b(n);
  15.    
  16.     for (auto& it : a) cin >> it;
  17.     for (auto& it : b) cin >> it;
  18.    
  19.     Int curr = 0;
  20.     for (int i = 0; i < n; ++i) {
  21.         curr += (a[i]-b[i])*(a[i]-b[i]);
  22.     }
  23.    
  24.     for (int j = 0; j < ka; ++j) {
  25.    
  26.         Int min_i = -1;
  27.         Int min_s = (Int)1e18L;
  28.         bool is_inc = true;
  29.         for (int i = 0; i < n; ++i) {
  30.             { // try inc
  31.                 auto temp = a[i] + 1;
  32.                 auto next = curr + (temp - b[i]) * (temp-b[i]) - (a[i]-b[i])*(a[i]-b[i]);
  33.                 if (min_s > next) {
  34.                     min_i = i;
  35.                     is_inc = true;
  36.                     min_s = next;
  37.                 }
  38.             }
  39.            
  40.             { // try dec
  41.                 auto temp = a[i] - 1;
  42.                 auto next = curr + (temp - b[i]) * (temp-b[i]) - (a[i]-b[i])*(a[i]-b[i]);
  43.                 if (min_s > next) {
  44.                     min_i = i;
  45.                     is_inc = false;
  46.                     min_s = next;
  47.                 }
  48.             }
  49.         }
  50.         assert(min_i != -1);
  51.        
  52.         curr = min_s;
  53.         if (is_inc) {
  54.             a[min_i]++;
  55.         } else {
  56.             a[min_i]--;
  57.         }
  58.        
  59.     }
  60.    
  61.     for (int j = 0; j < kb; ++j) {
  62.    
  63.         Int min_i = -1;
  64.         Int min_s = (Int)1e18L;
  65.         bool is_inc = true;
  66.         for (int i = 0; i < n; ++i) {
  67.             { // try inc
  68.                 auto temp = b[i] + 1;
  69.                 auto next = curr + (temp - a[i]) * (temp-a[i]) - (a[i]-b[i])*(a[i]-b[i]);
  70.                 if (min_s > next) {
  71.                     min_i = i;
  72.                     is_inc = true;
  73.                     min_s = next;
  74.                 }
  75.             }
  76.            
  77.             { // try dec
  78.                 auto temp = b[i] - 1;
  79.                 auto next = curr + (temp - a[i]) * (temp-a[i]) - (a[i]-b[i])*(a[i]-b[i]);
  80.                 if (min_s > next) {
  81.                     min_i = i;
  82.                     is_inc = false;
  83.                     min_s = next;
  84.                 }
  85.             }
  86.         }
  87.        
  88.         assert(min_i != -1);
  89.        
  90.         curr = min_s;
  91.         if (is_inc) {
  92.             b[min_i]++;
  93.         } else {
  94.             b[min_i]--;
  95.         }
  96.     }
  97.    
  98.     cout << curr;
  99.    
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement