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();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章