题目原文
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 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.
分析
提交代码
/*
ID:
PROG: skidesign
LANG: C++
*/
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <limits.h>
using namespace std;
int main()
{
ifstream fin("skidesign.in");
ofstream fout("skidesign.out");
int N; fin >> N;
vector<int> hills(N);
for (int i=0;i!=N;i++)
{
fin >> hills[i];
}
sort(hills.begin(),hills.end());
int low = hills[0];
int high = hills[N-1];
int mincost = INT_MAX;
for (int i=low;i<=high-17;i++)
{
int cost = 0;
int current_high = i + 17;
for (int j=0;j!=N;j++)
{
if(hills[j] < i)
{
cost += (i - hills[j]) * (i - hills[j]);
}
else if(hills[j] > current_high)
{
cost += (hills[j] - current_high) * (hills[j] - current_high);
}
}
mincost = cost < mincost? cost : mincost;
}
fout << mincost << endl;
return 0;
}
提交结果
TASK: skidesign LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.008 secs, 3504 KB] Test 2: TEST OK [0.008 secs, 3504 KB] Test 3: TEST OK [0.008 secs, 3504 KB] Test 4: TEST OK [0.005 secs, 3504 KB] Test 5: TEST OK [0.008 secs, 3504 KB] Test 6: TEST OK [0.005 secs, 3504 KB] Test 7: TEST OK [0.011 secs, 3504 KB] Test 8: TEST OK [0.011 secs, 3504 KB] Test 9: TEST OK [0.005 secs, 3504 KB] Test 10: TEST OK [0.005 secs, 3504 KB] All tests OK.
官方提供的参考答案
#include <fstream>
using namespace std;
int n,hills[1000];
int main()
{
ifstream fin("skidesign.in");
fin >> n;
for (int i=0; i<n; i++)
fin >> hills[i];
fin.close();
// brute-force search
// try all elevation intervals from (0,17) to (83,100)
int mincost=1000000000;
for (int i=0; i<=83; i++)
{
// calculate the cost for elevation interval (i,i+17)
int cost=0,x;
for (int j=0; j<n; j++)
{
// if hill is below the interval
if (hills[j]<i)
x=i-hills[j];
// if hill is above the interval
else if (hills[j]>i+17)
x=hills[j]-(i+17);
// if hill is int the interval
else
x=0;
cost+=x*x;
}
// update the minimum cost
mincost=min(mincost,cost);
}
ofstream fout("skidesign.out");
fout << mincost << "\n";
fout.close();
}
相比本文的思路,官方给出的答案并没有求出输入数据的最大值和最小值,而是直接将最低值设为0~83之间求取所有可能的处理成本。由于这样可以减少求取输入数据极值的过程,在数据量较大的时候会有一定的优势。