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

翻译:滑雪课程设计Ski Course Design

题目大意:就是给定n个数,这n个数中的最大值和最小值之间不能超过17,如果超过17就需要增大或者减小数字,改动数字x为y的费用为(x-y)(x-y)。求这个最小费用。

解法(暴力枚举)(先对n个数从小到大排序)

可以枚举最小数字a[j],范围为a[1]~a[n](每次最下值增加1)。对于小于a[j]的补齐,大于a[j]的减去即可。每一个最小数字可以计算出一个费用s,求a[1]~a[n]中最小的s即可。

比如:

1 4 20 21 24

假设最小山峰为3

则1 需要补齐到3    sum=sum+(3-1)(3-1)=4

3+17=20(3~20之间的数不需要变动)

大于20的数需要减到20. sum=sum+(21-20)^2+(24-20)^2=4+17=21

得到最小山峰为3时的费用为21.

依次算出最小山峰为4、5、6、7……的费用,找里面的最小值即可。

/*
ID: L
PROG: skidesign
LANG: C++
*/
#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main()
{
	freopen("skidesign.in","r",stdin);
	freopen("skidesign.out","w",stdout);
	int n,s=999999,sum=0;
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	sort(a+1,a+1+n);//对山峰排序
	//枚举最低峰的高度,低于它的补齐。高于它的看是否相差大于17,大于17就削掉 
	for(int i = a[1]; i <= a[n]; ++i) //最低山峰 
	{
		sum = 0;//每次费用之和 
		for(int j = 1; j <= n; ++j)//对于每座山峰补齐或者削掉 
		{
			if(a[j] < i) sum += (i-a[j])*(i-a[j]);//低于最小值的补齐到最小值 
			if(a[j] > i+17) sum += (a[j]-i-17)*(a[j]-i-17);//高于17的削掉到最高值 
		}
		s = min(s,sum); //更新最小费用 
	} 
	cout << s << endl; 
	fclose(stdin);
	fclose(stdout);
	return 0;
} 

 官方解答:

Ski Course Design
Fatih Gelgi

The problem can be solved with different approaches. A simple idea is of course brute-force -- try all possible elevations and find the minimum amount. We can try all possible values as follows: try the modification for elevation interval (0,17) then (1,18), (2,19), ..., (83,100). For each elevation interval (i,i+17), we need to calculate the cost for each hill j:

  1. If the elevation of hill j, say hill[j], is in the interval (i,i+17) then there is no cost.
  2. If it is less than i then the cost increases by (i-hill[j])^2
  3. If it is greater than i+17 then the cost increases by (hill[j]-(i+17))^2

The total cost for that interval will be the sum of the costs of modifying all hills.

For the sample input:

hill			elevation intervals and cost
height (0,17)  (1,18)  (2,19)  (3,20)  (4,21)  (5,22)  (6,23)  (7,24) ....
------ ---------------------------------------------------------------
 1	0	0	1	4	9	16	25	36	
 4	0	0	0	0	0	1	4	9
20	9	4	1	0	0	0	0	0
21	16	9	4	1	0	0	0	0
24	49	36	25	16	9	4	1	0
	-------------------------------------------------------------
total	74	49	31	21	*18*	21	30	45

As you observed, it is unnecessary to try elevation intervals after (7,24) since the maximum height is 24. You may want to modify the solution to eliminate these type of redundancies although it is not necessary.

For each interval, scanning through all hill elevations require O(N) time. Since we try all possible intervals, the total time is O(NM) where M is the size of the elevation range. Since N=1000 and M=100 are very small, this brute-force approach is sufficient. A sample code is provided below:

#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();
}
发布了308 篇原创文章 · 获赞 168 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/yanyanwenmeng/article/details/105200854
ski