Advertisement
mickypinata

USACO-T015: Ski Course Design

Nov 27th, 2021
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. /*
  2. ID: mickyta1
  3. TASK: skidesign
  4. LANG: C++
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. const int N = 1000 + 5;
  11.  
  12. int arr[N];
  13.  
  14. int main(){
  15.     freopen("skidesign.in", "r", stdin);
  16.     freopen("skidesign.out", "w", stdout);
  17.  
  18.     int n;
  19.     scanf("%d", &n);
  20.     int lwb = 100;
  21.     int upb = 0;
  22.     for(int i = 1; i <= n; ++i){
  23.         scanf("%d", &arr[i]);
  24.         lwb = min(lwb, arr[i]);
  25.         upb = max(upb, arr[i]);
  26.     }
  27.     if(upb - lwb <= 17){
  28.         cout << "0\n";
  29.         fclose(stdin);
  30.         fclose(stdout);
  31.         return 0;
  32.     }
  33.     int mn = 1e9;
  34.     for(int l = lwb; l < upb - 17; ++l){
  35.         int r = l + 17;
  36.         int sum = 0;
  37.         for(int j = 1; j <= n; ++j){
  38.             if(arr[j] < l){
  39.                 int dif = l - arr[j];
  40.                 sum += dif * dif;
  41.             } else if(arr[j] > r){
  42.                 int dif = arr[j] - r;
  43.                 sum += dif * dif;
  44.             }
  45.         }
  46.         mn = min(mn, sum);
  47.     }
  48.     cout << mn << '\n';
  49.  
  50.     fclose(stdin);
  51.     fclose(stdout);
  52.     return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement