USACO Section 1.3 Ski Course Design

本文介绍了一种算法,用于解决滑雪场设计中通过调整不同高度山丘的海拔来最小化成本的问题。该算法考虑了成本函数x^2,目标是在确保最高与最低山丘高度差不超过17的前提下,计算所需的最小成本。

题目原文

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

USACO1.3中最长回文Calf Flac问题要求在给定的文本中找出最长的回文子串,如果有多个回文长度都等于最大值,输出最前面出现的那一个。以下为该问题的解决方案: ### 解题思路 1. **预处理文本**:去除文本中非字母字符,同时记录每个字母在原文本中的位置,方便后续输出原始回文子串。 2. **遍历文本**:以每个字母为中心,向两边扩展来寻找回文子串。回文子串分为奇数长度和偶数长度两种情况,需要分别处理。 3. **记录最长回文子串**:在遍历过程中,记录最长回文子串的长度和起始位置,同时记录该回文子串在原文本中的起始和结束位置。 4. **输出结果**:输出最长回文子串的长度以及原文本中的最长回文子串。 ### 代码实现 ```python # 读取输入 input_text = input() # 预处理文本,记录字母及其在原文本中的位置 letters = [] positions = [] for i, char in enumerate(input_text): if char.isalpha(): letters.append(char.upper()) positions.append(i) n = len(letters) max_length = 0 start = 0 end = 0 # 遍历每个字母,以其为中心扩展寻找回文子串 for i in range(n): # 奇数长度回文串 left, right = i, i while left >= 0 and right < n and letters[left] == letters[right]: length = right - left + 1 if length > max_length: max_length = length start = positions[left] end = positions[right] left -= 1 right += 1 # 偶数长度回文串 left, right = i, i + 1 while left >= 0 and right < n and letters[left] == letters[right]: length = right - left + 1 if length > max_length: max_length = length start = positions[left] end = positions[right] left -= 1 right += 1 # 输出结果 print(max_length) print(input_text[start:end + 1]) ``` ### 复杂度分析 - **时间复杂度**:$O(n^2)$,其中 $n$ 是文本中字母的数量。因为对于每个字母,都需要向两边扩展来寻找回文子串。 - **空间复杂度**:$O(n)$,主要用于存储字母和其在原文本中的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值