问题 J: 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 x2 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.
输入
Line 1:The integer N.
Lines 2…1+N:Each line contains the elevation of a single hill.
输出
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.
样例输入 Copy
5
20
4
1
24
21
样例输出 Copy
18
提示
FJ’s farm has 5 hills, with elevations 1, 4, 20, 21, and 24.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 = 32 = 9). He shortens the hill of height 24 to height 21, also at a cost of 32 = 9.

题意:
农夫John(以后简称“FJ”)的农场里有N(1 <= N <= 1,000)座小山,每座山都有一个在0 … 100范围内的整数海拔高度。每年冬天,这些小山上充满积雪时,FJ都会开设一个滑雪训练营。不幸的是,FJ刚刚得知明年用作滑雪训练营的农场将会被收取一项新税。在仔细地阅读了相关的法律条文后,他发现滑雪营地的官方定义中要求最高和最低的山丘高度差严格大于17。所以,如果FJ降低他最高小山的高度并升高矮小的小山,使之最高和最低小山的高度差不超过17,他就能够避免交税。如果改变一座山的高度x个单位需要花费x^2个单位的金钱,FJ最少需要花费多少钱来改造小山的高度?FJ只会将每座小山改变整数个单位的高度。

解析:
我们将所有山的高度全部调整到[l,r] 其中l+17==r 这样可以一步到位 不用一个个的改山丘的高度

#include<bits/stdc++.h>
using namespace std;
int a[100005];
int n;
int main()
{
	cin>>n;
	int minn=0x3f3f3f3f;
	for(int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+1+n);
	for(int i=0;i<=100-17;i++)
	{
		int ans=0;
		for(int j=1;j<=n;j++)
		{
				 if(a[j]<i) ans=ans+(a[j]-i)*(a[j]-i);
				 else if(a[j]>i+17) ans=ans+(a[j]-i-17)*(a[j]-i-17);
		}
		minn=min(minn,ans);
	}
	cout<<minn<<endl;
		
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值