Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. const int max_n = 1001;
  7. int n;
  8. int v[max_n];
  9.  
  10. int main() {
  11.     freopen("input", "r", stdin);
  12.  
  13.  
  14.     int max_num = 0;
  15.     cin >> n;
  16.     for (int i = 0; i< n;i++) {
  17.       cin >> v[i];
  18.       max_num = max(v[i], max_num);
  19.     }
  20.  
  21.     int best_t = 0;
  22.     int best_cost = INT_MAX;
  23.  for (int t = 1; t < max_num + 1; t++) {
  24.    int cost = 0;
  25.  
  26.    for (int i =0;i<n;i++){
  27.     if (int(abs(v[i] - t)) > 1) {
  28.       if (v[i] > t) {
  29.         cost += v[i] - t - 1;
  30.  
  31.       } else {
  32.        cost += t - v[i] - 1;
  33.       }
  34.     }
  35.    }
  36.  
  37.     if (cost < best_cost) {
  38.       best_cost = cost;
  39.       best_t = t;
  40.     }
  41.  
  42.  }
  43.  
  44.  cout << best_t << " " << best_cost << "\n";
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement