Ski Course Design
Farmer John have N hills on his farm (1 <= N <= $), each with a integer elevation in the range 0. In the winter, since there are abundant snow on these hills, FJ routinely operates a ski training camp.
Unfortunately, FJ have just found out about a new tax that would 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 are at most 17.
If It costs x^2 units the height of a hill by x units, what's the minimum amount of money FJ would need To pay? FJ can change the height of a hill only once, so that the total cost for each hill are the square of the difference between its Original and final height. FJ is only willing to the height of each hill by an integer amount.
Program Name:skidesigninput 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
21
INPUT DETAILS:
FJ's Farm has a 5 hills, with elevations 1, 4, a, and 24.
OUTPUT FORMAT:
The minimum amount FJ needs to pay to modify the elevations of He hills so the difference between largest and smallest are At the most units.
SAMPLE OUTPUT (file skidesign.out):
18
OUTPUT DETAILS:
FJ keeps the hills of heights 4, and as they are. He adds Mass to the hill of height 1 and bringing it to height 4 (cost = 3^2 = 9). He shortens the hill of height to height, also at a cost of 3^2 = 9.
Test instructions: Farmer John has n (1 <= n <= 1,000) sitting mountain, each mountain has a height hills[i] (0<=hills[i]<=100), Farmer John can modify the height of the mountain (increase or decrease h), But it costs a h^2.
It is required that the highest mountain and the lowest mountain difference shall not exceed 17,farmer John began to modify the height of the cut as required for minimum cost.
Puzzle: The height of the mountain is between 0 and 100, the lowest mountain height is lowest, the tallest mountain is highest. Lowest=i,highest=i+17,i starting from 1 enumeration, to 100 end, calculate each time the cost, record the minimum value;
PS: The People's Congress Three dog one, is continuing to update the blog, the article has any questions, I hope you can point out. If in doubt you can leave a comment in the comments section, I will reply as soon as possible. I hope you can learn from each other, thank you
/*
ID: cxq_xia1
PROG: skidesign
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
freopen("skidesign.in","r",stdin);
freopen("skidesign.out","w",stdout);
int hills[1005],N;
int money,ans=0xfffffff;
cin >> N;
for(int i=0;i<N;i++)
{
cin >> hills[i];
}
int lowest,highest ;
for(int i=0;i<=100;i++)
{
lowest=i;highest=lowest+17;
money=0;
for(int j=0;j<N;j++)
{
if(hills[j]<lowest)
money+=(hills[j]-lowest)*(hills[j]-lowest);
else if(hills[j]>highest)
money+=(highest-hills[j])*(highest-hills[j]);
}
if(money<ans)
ans=money;
}
cout << ans <<endl;
return 0;
}
Usaco 1.3 Ski Course Design