说实话,一开始看上一题的时候觉得太恶心就先来做这道,所以这题其实比上一题早做出来()
Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer elevation in the range 0 .. 100. In the winter, since there is abundant snow on these hills, FJ routinely operates a ski training camp.
Unfortunately, FJ has just found out about a new tax that will be assessed next year on farms used as ski training camps. Upon careful reading of the law, however, he discovers that the official definition of a ski camp requires the difference between the highest and lowest hill on his property to be strictly larger than 17. Therefore, if he shortens his tallest hills and adds mass to increase the height of his shorter hills, FJ can avoid paying the tax as long as the new difference between the highest and lowest hill is at most 17.
If it costs x^2 units of money to change the height of a hill by x units, what is the minimum amount of money FJ will need to pay? FJ can change the height of a hill only once, so the total cost for each hill is the square of the difference between its original and final height. FJ is only willing to change the height of each hill by an integer amount.
PROGRAM NAME: skidesign
INPUT FORMAT:
Line 1: The integer N. Lines 2..1+N: Each line contains the elevation of a single hill. SAMPLE INPUT (file skidesign.in):
5 20 4 1 24 21INPUT DETAILS:
FJ's farm has 5 hills, with elevations 1, 4, 20, 21, and 24.
OUTPUT FORMAT:
The minimum amount FJ needs to pay to modify the elevations of his hills so the difference between largest and smallest is at most 17 units.
Line 1: SAMPLE OUTPUT (file skidesign.out):
18OUTPUT DETAILS:
FJ keeps the hills of heights 4, 20, and 21 as they are. He adds mass to the hill of height 1, bringing it to height 4 (cost = 3^2 = 9). He shortens the hill of height 24 to height 21, also at a cost of 3^2 = 9.
每日翻译(2/1)
题目大意:farmer john有n个小山丘,这些山丘都有自己的高度。税务局有一个新规定:n个山丘之间的最大差值如果大于17的话就要交税。尽人皆知farmer john不愿意交,所以他需要调整他山丘的高度。将一个山丘降低或升高x的话需要交x^2的钱,问farmer john最少交多少钱能够不交税。
还是不太难的
我们去枚举他的一个界限值,这个值需要是目前的最低,且和最高差不超过17。
第一步:很好想出,如果哪个山峰和界限值差大于17,就降低呗。这里注意一下,因为他要交最少的钱,所以差值必须为17
第二步:需要把低于他的给提上来。为什么呢?
我们就拿样例说一下
题目明确指出这个临界值是4
因为20和21这两个数与4的差都通过了,所以如果没有第二步就会成为现在的样子
可事实是这样吗?还有个1呢!!
这个1与20和21都不行,所以得把他提升
提到多少呢?我们目前的临界值是多少就提到多少。
这时四个数分别是:4,4,20,21
都没问题了
所以我们的第二步就是把低于临界值的全部提升
AC代码:
/*
ID:
TASK:skidesign
LANG:C++
*/
# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
using namespace std;
# define int long long
int a[100000],n,minn,cnt=0x3f3f3f;
signed main(){
//freopen("skidesign.in","r",stdin);
//freopen("skidesign.out","w",stdout);
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+n+1);
for(int i=1;i<=84;i++){
int sum=0;
for(int j=1;j<=n;j++){
if(a[j]-i>17){
sum+=(a[j]-i-17)*(a[j]-i-17);
}
if(a[j]<i){
sum+=(i-a[j])*(i-a[j]);
}
}
cnt=min(sum,cnt);
}
printf("%lld\n",cnt);
//fclose(stdin);
//fclose(stdout);
return 0;
}