当前位置:网站首页>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 1≤N≤1000
Data assurance , The initial height of each mountain is 0 ∼ 100 0\sim 100 0∼100 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 0∼100 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 0∼100 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 0∼17,1∼18,…,83∼100 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+(l−h[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;
}
边栏推荐
- 使用Yolov5训练好模型调用电脑自带摄像头时出现问题:TypeError: argument of type “int‘ is not iterable的解决方法
- SWUST oj668: the thief ran away
- 恋爱时将房屋一半产权登记在女方名下,分手后想要回
- Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO
- Electron桌面端开发(开发一个闹钟【完结】)
- 外观模式--在各种套餐中早就用到啦!
- Distance measurement - Euclidean distance
- Jszip get the file of the specified file in the uploaded zip package
- Working principle analysis of rxjs fromEvent
- Using hystrix to implement fault-tolerant processing of microservices
猜你喜欢

VOC格式数据集转yolo格式数据集的方法

Campus lost and found applet source code can be used for graduation design

Why does a ddrx power supply design require a VTT power supply

SurroundDepth:自监督多摄像头环视深度估计

不做伪工作者

DROID-SLAM: 用于单目双目RGBD相机的深度视觉SLAM

Writing the program into the microcontroller can control the forward and reverse rotation of the motor more conveniently and quickly

Don't be a fake worker

Using hystrix to implement fault-tolerant processing of microservices

Electron桌面端开发(开发一个闹钟【完结】)
随机推荐
MYSQL(九)
如何养成一个好习惯?靠毅力?靠决心?都不是!
Vscode——vscode 多窗口名字配置成项目名字
Content-Type: multipart/form-data; boundary=${bound}
Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged
MySQL optimized learning diary 10 - locking mechanism
Installing redis in CentOS 7 environment
Electron桌面端开发(开发一个闹钟【完结】)
杰理之BLE SPP 开启 pin_code 功能【篇】
想做钢铁侠?听说很多大佬都是用它入门的
34. find the first and last positions of elements in the sorted array ●●
Jerry's acquisition of ble distinguishes between reset and wake-up [chapter]
小白在同花顺上直接开户是安全的吗?
Remote monitoring project offline log specification
不做伪工作者
Characteristics and classification of creation mode (single case, factory)
Update更新 bytea类型失败 PostGresql
使用Yolov5训练自己制作的数据集,快速上手
RxJs fromEvent 工作原理分析
Rxjs Observable.pipe 传入多个 operators 的执行逻辑分析