USACO Section 1.3 Ski Course Design

题目原文

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 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. 


分析

题目的意思可以简化为,给定不多于1000个的0~100整数,要求通过对其中个别数字进行加减,使得任意两个数的差不超过17,并且对任意数字加减x的成本是x^2,求最小成本。
例如,输入的数字为20,4,1,24,21,为了使处理后的数字达到要求,处理后的数字为20,4,4,21,21,这样的成本为3^2+3^2=18,为最小成本。
本题的一个思路是先得到输入数字的最小值和最大值,然后依据最小值和最大值的范围依次计算所有的加减情况,并且记录最小的成本。

提交代码

 /*
 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之间求取所有可能的处理成本。由于这样可以减少求取输入数据极值的过程,在数据量较大的时候会有一定的优势。


THE END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值