USACO 1.4 Ski Course Design 修雪山

题目描述

 


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.

 

样例输入&输出


 sample input

5

20 4 1 24 21

sample output

18 

解释: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.

 

分析&反思


本以为5秒水过结果,,,

还是想当然了,不一定以某一个山为lowest,不然你咋不以一个山为highest呢!

 

代码


 

#include
#include
#include
#include
using namespace std;

const int inf = 1999999999;

long long ans = inf;
int n, hill[1002];

int main () {
	
	freopen("skidesign.in", "r", stdin);
	freopen("skidesign.out", "w", stdout);
	
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> hill[i];
	
	sort(hill+1, hill+n+1);

	for(int i = 1; i < 85; i++) {
		long long anss = 0;
		
		int lowest = i;
		int highest = lowest+17;
		
		for(int j = 1; j <= n; j++) {
			if(hill[j] < lowest) {
				anss += (lowest-hill[j]) * (lowest-hill[j]);
				continue;
			}
			if(hill[j] > highest) {
				anss += (hill[j]-highest) * (hill[j]-highest);
				continue;
			}
		}
		
		ans = min(ans, anss);
		//cout << anss << " " << i << endl;
	}
	
	cout << ans << endl;
	
	return 0;
}

备注

 


 离秒题还有很大差距啊。。

你可能感兴趣的:(USACO)