Time Limit: 10000 ms Case Time Limit: 1000 ms Memory Limit: 256 MB
Total Submission: 21 Submission Accepted: 6
Total Submission: 21 Submission Accepted: 6
Judge By Case
Description
俗话说得好,精卫填海,LF 平山。作为处女座的 SLF 强迫症有很多,他学成后买下一个荒无人烟的山丘地带,但是山的高度很是令他烦恼,于是他决定要用最小的代价让最高的山峰与最低的山峰的高度差不超过 17,SLF 经过调查,已知第 i 座山峰高度为 a[i],由于填山或是平山都需要代价,SLF 询问了专业人员,将高度为 a[i]的山峰改造成高度为 x 的山峰的代价为(a[i] – x)^ 2,经过苦难的他深知赚钱的不容易,所以他希望代价最小。
Input
第一行一个数 n。
接下来 n 行每行一个整数 a[i](0 ≤ a[i] ≤ 100),表示山峰高度。
接下来 n 行每行一个整数 a[i](0 ≤ a[i] ≤ 100),表示山峰高度。
Output
一行一个整数,最小的代价
Sample Input
Original | Transformed |
5 20 4 1 24 21
5[EOL] 20[EOL] 4[EOL] 1[EOL] 24[EOL] 21[EOF]
Sample Output
Original | Transformed |
18
18[EOF]
Hint
对于 100%的数据,1 ≤ n ≤ 1000
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<sstream>
#include<map>
//#define DEBUG
const int maxn = 1055;
using namespace std;;
int pow(int n);
int main() {
#ifdef DEBUG
freopen("Text.txt", "r", stdin);
#endif // DEBUG
cin.tie(0);
cin.sync_with_stdio(false);
int n;
while (cin >> n) {
int i, j, mx = 0;
long long mm = 10000000;
int a[maxn];
for (i = 0; i < n; i++) {
cin >> a[i];
mx = max(mx, a[i]);
}
for (i = 0; i <= mx; i++) {
long long ans = 0;
for (j = 0; j < n; j++) {
if (a[j] > i)ans += pow(a[j] - i);
else
if (i - a[j] <= 17)continue;
else ans += pow(i - a[j] - 17);
}
mm = min(mm, ans);
//cout << "mm=" << mm<<" i="<<i << endl;
}
cout << mm << endl;
}
return 0;
}
int pow(int n) {
return n*n;
}