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

 

题意:Farmer John 有N(1 <= N <= 1,000)坐山,每座山都有一个高度hills[i] (0<=hills[i]<=100),Farmer John可以修改山的高度(增加或减少h),但得花费h^2的费用。

现要求最高的山和最低的山相差不得超过17,Farmer John开始按要求修改删的高度,求最小花费。

题解:山的高度在0到100之间 ,设最低的山高度为lowest,最高的山为highest。lowest=i,highest=i+17,i从1开始枚举,到100结束,算每次的花费,记录下最小值;

 ps:本人大三狗一枚,正在持续更新博客,文章里有任何问题,希望各位网友可以指出。若有疑问也可在评论区留言,我会尽快回复。希望能与各位网友互相学习,谢谢

 

/*
ID: cxq_xia1
PROG: skidesign
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;


int main()
{
    freopen("skidesign.in","r",stdin);
    freopen("skidesign.out","w",stdout);

    int hills[1005],N;
    int money,ans=0xfffffff;
    cin >> N;
    for(int i=0;i<N;i++)
    {
        cin >> hills[i];
    }
    int lowest,highest ;
    for(int i=0;i<=100;i++)
    {
        lowest=i;highest=lowest+17;
        money=0;
        for(int j=0;j<N;j++)
        {
            if(hills[j]<lowest)
                money+=(hills[j]-lowest)*(hills[j]-lowest);
            else if(hills[j]>highest)
                money+=(highest-hills[j])*(highest-hills[j]);
        }
        if(money<ans)
            ans=money;
    }

    cout << ans <<endl;
    return 0;
}

  

 

转载于:https://www.cnblogs.com/WillsCheng/p/4793462.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值