USA Computing Olympiad
Problem 1: Ski Course Design [Brian Dean, 2014] 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. PROBLEM 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: * Line 1: 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. 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.
各个山峰调整到的高度,顶多是最高的山的高度,那么,从低往高枚举调整到的高度即可,如果比它高,就减去相应的距离,如果没它高,但如果差小于17,就啥也不做,否则,花费成本
for (int i = 0; i < n; ++i)
{
int tmp;
fin >> tmp;
v.push_back(tmp);
}
sort(v.begin(), v.end());
int min = 0x7fffffff;
for (int i = v[0]; i <= v[n - 1] - 17; ++i)
{
int cost = 0;
for (int j = 0; v[j] < i; ++j)
{
cost += (i - v[j]) * (i - v[j]);
}
for (int j = n - 1; v[j] > i + 17; --j)
{
cost += (v[j] - i - 17) * (v[j] - i - 17);
}
if (cost < min) min = cost;
}
fout << min << endl;
Read full article from USA Computing Olympiad
Problem 1: Ski Course Design [Brian Dean, 2014] 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. PROBLEM 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: * Line 1: 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. 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.
各个山峰调整到的高度,顶多是最高的山的高度,那么,从低往高枚举调整到的高度即可,如果比它高,就减去相应的距离,如果没它高,但如果差小于17,就啥也不做,否则,花费成本
- scanf("%d",&N);
- int Max=0;
- int tmp;
- for(int i=0;i<N;++i)
- {
- scanf("%d",&tmp);
- if(Max<tmp)Max=tmp;
- h[i]=tmp;
- }
- int ans=0x3f3f3f3f;
- for(int i=0;i<=Max;++i)
- {
- int sum=0;
- for(int j=0;j<N;++j)
- {
- if(h[j]<i&&i-h[j]>17)
- {
- int c=(i-h[j]-17)*(i-h[j]-17);
- sum+=c;
- }
- if(h[j]>i)
- {
- int c= h[j]-i;
- sum+=c*c;
- }
- }
- ans=MIN(sum,ans);
- }
- printf("%d\n",ans);
例如,输入的数字为20,4,1,24,21,为了使处理后的数字达到要求,处理后的数字为20,4,4,21,21,这样的成本为3^2+3^2=18,为最小成本。
这题由于数据很小,高度只有0-100,那么我们只要枚举区间(最小高度 ~ 最小高度+17) ~ (最高高度-17 ~ 最高高度),截去所有超过这一区间的,然后分别计算花费,取最小花费就行了。- 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;
for (int i = 0; i < n; ++i)
{
int tmp;
fin >> tmp;
v.push_back(tmp);
}
sort(v.begin(), v.end());
int min = 0x7fffffff;
for (int i = v[0]; i <= v[n - 1] - 17; ++i)
{
int cost = 0;
for (int j = 0; v[j] < i; ++j)
{
cost += (i - v[j]) * (i - v[j]);
}
for (int j = n - 1; v[j] > i + 17; --j)
{
cost += (v[j] - i - 17) * (v[j] - i - 17);
}
if (cost < min) min = cost;
}
fout << min << endl;
Read full article from USA Computing Olympiad