• USACO Training 1.4 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.

    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.

    1. /*
    2. ID:choiyin1
    3. PROG:skidesign
    4. LANG:C++
    5. */
    6. #include
    7. using namespace std;
    8. int N;
    9. int h[1001];
    10. #define MIN(a,b) ((a)>(b)?(b):(a))
    11. int main(int argc,char *argv[])
    12. {
    13. freopen("skidesign.in","r",stdin);
    14. freopen("skidesign.out","w",stdout);
    15. scanf("%d",&N);
    16. int Max=0;
    17. int tmp;
    18. for(int i=0;i
    19. {
    20. scanf("%d",&tmp);
    21. if(Max
    22. h[i]=tmp;
    23. }
    24. int ans=0x3f3f3f3f;
    25. for(int i=0;i<=Max;++i)
    26. {
    27. int sum=0;
    28. for(int j=0;j
    29. {
    30. if(h[j]17)
    31. {
    32. int c=(i-h[j]-17)*(i-h[j]-17);
    33. sum+=c;
    34. }
    35. if(h[j]>i)
    36. {
    37. int c= h[j]-i;
    38. sum+=c*c;
    39. }
    40. }
    41. ans=MIN(sum,ans);
    42. }
    43. printf("%d\n",ans);
    44. }

  • 相关阅读:
    【Python 千题 —— 基础篇】班里来了新同学
    只有真正将产业互联网看成是一种嬗变的过程,才能把握其精髓和原始奥义
    curl命令行发送post/get请求
    【网络服务&数据库教程】05 LAMP 部署
    j2Cache 缓存框架讲解;SpringBoot 整合 j2Cache 代码示例
    基于SpringBoot的招聘网站
    java计算机毕业设计互联网校园家教兼职平台源码+mysql数据库+系统+lw文档+部署
    高德地图sdk设置marker并且将设置为地图中心
    多层感知机
    Java基础进阶Map-HashMap集合
  • 原文地址:https://blog.csdn.net/GeekAlice/article/details/127037928