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.
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 21
INPUT 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):
18
OUTPUT 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.
Submission file Name: USACO Gateway | Comment or Question
(转自[USACO])
首先讲一下题目大意。有N座山峰,每座山峰的高度大于等于0小于等于100,由于下了雪,所以可以改造成滑雪场(倾盆大雪),但是如果最高的山峰和最低的山峰高度之差大于17政府就要收税,John为了不被收费,就决定改造山峰。把一座山峰的高度改动x,就会产生x2的费用。求最小的费用。
鉴于数据范围较小,于是决定暴力,枚举改造后的最小高度,然后暴力每次跑一遍就可以了
Code
1 /* 2 ID: 3 PROG: skidesign 4 LANG: C++11 5 */ 6 /** 7 * USACO 8 * Accepted 9 * Time:0ms 10 * Memory:4180k 11 */ 12 #include<iostream> 13 #include<cstdio> 14 #include<cctype> 15 #include<cstring> 16 #include<cstdlib> 17 #include<cmath> 18 #include<fstream> 19 #include<sstream> 20 #include<algorithm> 21 #include<map> 22 #include<set> 23 #include<queue> 24 #include<vector> 25 #include<stack> 26 using namespace std; 27 typedef bool boolean; 28 #define INF 0xfffffff 29 #define smin(a, b) a = min(a, b) 30 #define smax(a, b) a = max(a, b) 31 template<typename T> 32 inline void readInteger(T& u){ 33 char x; 34 int aFlag = 1; 35 while(!isdigit((x = getchar())) && x != ‘-‘); 36 if(x == ‘-‘){ 37 x = getchar(); 38 aFlag = -1; 39 } 40 for(u = x - ‘0‘; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - ‘0‘); 41 ungetc(x, stdin); 42 u *= aFlag; 43 } 44 45 int n; 46 int *hills; 47 48 inline void init(){ 49 readInteger(n); 50 hills = new int[(const int)(n + 1)]; 51 for(int i = 1; i <= n; i++){ 52 readInteger(hills[i]); 53 } 54 } 55 56 int result = INF; 57 inline void solve(){ 58 for(int res = 0; res <= 83; res++){ 59 int cmp = 0; 60 for(int i = 1; i <= n; i++){ 61 if(hills[i] < res){ 62 cmp += (res - hills[i]) * (res - hills[i]); 63 }else if(hills[i] > res + 17){ 64 cmp += (hills[i] - res - 17) * (hills[i] - res - 17); 65 } 66 } 67 smin(result, cmp); 68 } 69 printf("%d\n", result); 70 } 71 72 int main(){ 73 freopen("skidesign.in", "r", stdin); 74 freopen("skidesign.out", "w", stdout); 75 init(); 76 solve(); 77 return 0; 78 }