【USACO】Ski Course Design

/*
ID:
PROG: skidesign
LANG: C++11
 */

#include 
#include 
#include 
using namespace std;

int main() {
    freopen("skidesign.in", "r", stdin);
    freopen("skidesign.out", "w", stdout);
    int n;
    int lower_bound, upper_bound;
    int a[110] = {};
    cin >> n;
    for (int i = 0; i < n; i ++){
        int t;
        cin >> t;
        a[t] += 1;
    }
    int ans = 10000000;
    for (lower_bound = 0; lower_bound < 84; lower_bound ++){
        upper_bound = lower_bound + 17;
        int sum = 0;
        for (int i = 0; i < 101; i ++){
            if (a[i]){
                if (i < lower_bound){
                    sum += a[i] * pow(i - lower_bound,2);
                }
                else if (i > upper_bound){
                    sum += a[i] * pow(i - upper_bound, 2);
                }
            }
        }
        if (sum < ans)
            ans = sum;
    }
    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(枚举,USACO)