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.
| Line 1: | The integer N. |
| Lines 2..1+N: | Each line contains the elevation of a single hill. |
5 20 4 1 24 21
FJ's farm has 5 hills, with elevations 1, 4, 20, 21, and 24.
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
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.
- /*
- ID:choiyin1
- PROG:skidesign
- LANG:C++
- */
- #include
- using namespace std;
- int N;
- int h[1001];
- #define MIN(a,b) ((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
- {
- scanf("%d",&tmp);
- if(Max
- h[i]=tmp;
- }
- int ans=0x3f3f3f3f;
- for(int i=0;i<=Max;++i)
- {
- int sum=0;
- for(int j=0;j
- {
- if(h[j]17)
- {
- int c=(i-h[j]-17)*(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);
- }
-
相关阅读:
【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
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU