有 N 座山峰做滑雪场,每个的高度为 Hi ( Hi <= 100 ), 但是这 N 个山峰中的最大值与最小值的差大于 17,就要交税,但是滑雪场的主人不想交,于是就改变山峰的高度,使得没有差大于 17 的两座山峰,改变一个山峰的话费为 改变量的平方,最小化总花费。
因为山峰的高度都很小,我们可以枚举所有差值为 17 的数 l, r ,此时当有山峰大于 r 或 小于 l 的话就改变这个山峰,得到一个费用来更新答案。
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_N = 1005;
int N, al[MAX_N];
int doit(void)
{
int ret = 99999999;
for(int i=1; i<=100-17; i++){
int l=i, r=i+17, ans = 0;// count r - l = 17, because changing the al which should be to 17 is the best
for(int j=1; j<=N; j++){ // count al which big than r or small then l
if(al[j] < l) ans += (l - al[j]) * (l - al[j]);
if(al[j] > r) ans += (al[j] - r) * (al[j] - r);
}
ret = min(ret, ans);
}
return ret;
}
int main()
{
freopen("skidesign.in", "r", stdin);
freopen("skidesign.out", "w", stdout);
scanf("%d", &N);
for(int i=1; i<=N; i++) scanf("%d", &al[i]);// cin
printf("%d\n", doit());
return 0;
}