Usaco 1.3 Ski Course Design (enumeration)

Source: Internet
Author: User

Description

Farmer John's Farm has N mountain peaks (1<=n<=1000), each with a height of 0 to 100 integers. In winter, John often runs ski camps because of the abundance of snow on the mountains.

Unfortunately, John has just learned that there are new changes in the tax law in ski training camps and that it is coming next year. After reading the law carefully, he found that if the highest and lowest peaks of the ski training camp were above 17 the difference in altitude would be taxed. So if he changes the height of the mountain (so that the highest and lowest peaks are not above 17), John can avoid paying taxes.

If changing the height of a mountain x unit is a x^2 unit, how much does John have to pay at least? John is only willing to change the height of an integer unit.

[Edit]format

Program NAME: skidesign

INPUT FORMAT:

(File skidesign.in)

First line: An integer n

Row two to n+1: Each row is the altitude of a mountain

OUTPUT FORMAT:

(File skidesign.out)

John needs to pay the total amount of changes to the mountain altitude, the highest and lowest height difference between peaks up to 17.

[Edit]SAMPLE INPUT
520412421
[Edit]SAMPLE OUTPUT
18
[Edit]HINTS[Edit]Sample Input Description

John's Farm has 5 mountains, 1,4,20,21 and 24 above sea level.

[Edit]Sample Output Description

John maintains a height of 4, 20 and 21 of the mountain height. He increased the height to 1 of the mountain, turning it into a height of 4 (spending 3 ^ 2 = 9). He lowered the height of the mountain to 24 to become a height of 21, which also cost 3 ^ 2 = 9.


How to solve the problem: the height of each mountain, at most is the highest mountain height, then, from the low to high enumeration height can be adjusted to, if higher than it, minus the corresponding distance, if not it high, but if the difference is less than 17, do nothing, otherwise, cost

/* id:twd30651 prog:skidesign lang:c++*/#include <iostream> #include <fstream> #include <string.h># Include<math.h> #include <stdlib.h>using namespace Std;int n;int h[1001]; #define MIN (a) > (b) (    b):(a)) int main (int argc,char *argv[]) {freopen ("skidesign.in", "R", stdin);    Freopen ("Skidesign.out", "w", stdout);    scanf ("%d", &n);    int max=0;    int tmp;        for (int i=0;i<n;++i) {scanf ("%d", &tmp);        if (max<tmp) max=tmp;    h[i]=tmp;    } int ans=0x3f3f3f3f;        for (int i=0;i<=max;++i) {int sum=0; for (int j=0;j<n;++j) {if (h[j]<i&&i-h[j]>17) {int c= (i-h[j]-                * (I-H[J]-17);            Sum+=c;                } if (h[j]>i) {int c= h[j]-i;            Sum+=c*c;    }} ans=min (Sum,ans);    } printf ("%d\n", ans); return 0;}



Usaco 1.3 Ski Course Design (enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.