当前位置:网站首页>AcWing 1353. Ski resort design (greedy)

AcWing 1353. Ski resort design (greedy)

2022-06-11 11:10:00 Eurya song

【 Title Description 】
Farmer John's farm has N N N A mountain , The height of each mountain is an integer .
In winter , John often holds ski training camps on these mountains .
Unfortunately , From next year , The state will introduce a new tax law on ski resorts .
If the height difference between the highest peak and the lowest peak of the ski resort is greater than 17 17 17, The state collects taxes .
To avoid paying taxes , John decided to trim the height of these peaks .
It is known that , Increase or decrease a mountain peak x x x Unit height , Need to spend x 2 x^2 x2 The money of .
John is only willing to change the height of integer units , And each peak can only be modified once .
Excuse me, , How much does John need to spend at least , Only then can the height difference between the highest peak and the lowest peak not be greater than 17 17 17.

【 Input format 】
The first line contains integers N N N.
Next N N N That's ok , Each line contains an integer , Indicates the height of a mountain .

【 Output format 】
Output an integer , Indicates the least money spent .

【 Data range 】
1 ≤ N ≤ 1000 1≤N≤1000 1N1000
Data assurance , The initial height of each mountain is 0 ∼ 100 0\sim 100 0100 Between .

【 sample input 】

5
20
4
1
24
21

【 sample output 】

18

【 Sample explanation 】
The best solution is , Set the height to 1 1 1 The mountains of , increase 3 3 3 One unit height , Set the height to 24 24 24 The mountains of , Reduce 3 3 3 One unit height .

【 analysis 】


Because the initial height of each mountain is 0 ∼ 100 0\sim 100 0100 Between , Therefore, it is easy to prove that the height of the highest peak and the lowest peak must also be 0 ∼ 100 0\sim 100 0100 In this range , That is, the height range is 0 ∼ 17 , 1 ∼ 18 , … , 83 ∼ 100 0\sim 17,1\sim 18,\dots ,83\sim 100 017,118,,83100 One of them . So we can enumerate each interval [ l , r ] [l,r] [l,r], Calculate the height of all the peaks h [ i ] h[i] h[i] The total cost of modifying to this range c o s t cost cost( if h [ i ] < l h[i]<l h[i]<l be c o s t + ( l − h [ i ] ) 2 cost+(l-h[i])^2 cost+(lh[i])2; if h [ i ] > r h[i]>r h[i]>r be c o s t + ( h [ i ] − r ) 2 cost+(h[i]-r)^2 cost+(h[i]r)2), Take the minimum value of all interval costs .


【 Code 】

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int N = 1010;
int h[N];
int n;

int main()
{
    
    cin >> n;
    for (int i = 0; i < n; i++) cin >> h[i];
    int res = 0x3f3f3f3f;
    for (int l = 0; l + 17 <= 100; l++)
    {
    
        int r = l + 17, cost = 0;
        for (int i = 0; i < n; i++)
            if (h[i] < l) cost += pow(l - h[i], 2);
            else if (h[i] > r) cost += pow(h[i] - r, 2);
        res = min(res, cost);
    }
    cout << res << endl;
    return 0;
}
原网站

版权声明
本文为[Eurya song]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111058037633.html