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. 


一开始我理解错题目了(感觉最近老是理解不好题目,难过),我以为,要把高的山减下来的部分加到矮的山上面去。这样子,虽然这里想复杂了,但解决方法并没有考虑透,我简单地把山的海拔从低到高排列起来,然后把最高的一部分给最低的,第二高的一部分给第二低的(当然仔细想想即使题目是我想的样子,我这种做法也是错的)——当然,这样做过了样例,也就是第一个测试数据,然后就开始尴尬了。

然后,现在我们正确理解题目后,来仔细观察她。

如果我们采用暴力枚举的方法。把每一座山的高度h控制在[l, l + 17]内:如果高度h低于l, 就加上 l - h;如果高于l + 17, 就减去 h - (l + 17)。

因为山最高100米,所以l只要从0(或1,问题不大)枚举到 100 - 17 = 83就好,然后用一个叫money的数组存下这84(83)种不同的可能的钱,再找出最便宜的输出就行了。所以是一个O (n^2)的算法。

可行吗?按照经验,这个时间复杂度,在机考的OJ上是最低只有30分的(还是在助教仁慈的情况下),但是,在USACO上,是极大可能过掉的!!

于是我们试一试。

过啦!!!

当然,我们希望更快的算法。于是我去Google了这道题,残念,几乎全是这样做的,而我又想不到别的算法了。

而且,这道题的代码,真的好少啊啊啊啊啊啊!!!


下面是代码:

#include<fstream>
#include<iostream>
#include<algorithm>
#define maxn 1000
using namespace std;

ifstream fin("skidesign.in");
ofstream fout("skidesign.out");

int n, cnt;
int elevation[maxn], money[maxn];
int l, r;

void solve()
{
        //sort(elevation, elevation + n);
        for (l = 0; l <= 100 - 17; ++l){
                r = l + 17;
                for (int j = 0; j < n; ++j){
                        if (elevation[j] < l) money[cnt] += (elevation[j] - l) * (elevation[j] - l);
                        else if (elevation[j] > r) money[cnt] += (r - elevation[j]) * (r - elevation[j]);
                }
                ++cnt;
        }
        sort(money, money + cnt);
        fout << money[0] << endl;
}

int main()
{
        fin >> n;
        //cin >> n;
        for (int i = 0; i < n; ++i) fin >> elevation[i];
        solve();

        fin.close();
        fout.close();
        return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值