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.

Line 1:

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.


Submission file Name:  USACO Gateway |   Comment or Question

(转自[USACO]


  首先讲一下题目大意。有N座山峰,每座山峰的高度大于等于0小于等于100,由于下了雪,所以可以改造成滑雪场(倾盆大雪),但是如果最高的山峰和最低的山峰高度之差大于17政府就要收税,John为了不被收费,就决定改造山峰。把一座山峰的高度改动x,就会产生x2的费用。求最小的费用。

  鉴于数据范围较小,于是决定暴力,枚举改造后的最小高度,然后暴力每次跑一遍就可以了

Code

 /*
ID:
PROG: skidesign
LANG: C++11
*/
/**
* USACO
* Accepted
* Time:0ms
* Memory:4180k
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} int n;
int *hills; inline void init(){
readInteger(n);
hills = new int[(const int)(n + )];
for(int i = ; i <= n; i++){
readInteger(hills[i]);
}
} int result = INF;
inline void solve(){
for(int res = ; res <= ; res++){
int cmp = ;
for(int i = ; i <= n; i++){
if(hills[i] < res){
cmp += (res - hills[i]) * (res - hills[i]);
}else if(hills[i] > res + ){
cmp += (hills[i] - res - ) * (hills[i] - res - );
}
}
smin(result, cmp);
}
printf("%d\n", result);
} int main(){
freopen("skidesign.in", "r", stdin);
freopen("skidesign.out", "w", stdout);
init();
solve();
return ;
}

最新文章

  1. Lite Your Android English
  2. 生产环境下实践DDD中的规约模式
  3. plain framework 1 参考手册 入门指引之 简明教程
  4. Qt 子窗体嵌入父窗体
  5. robotframework笔记10
  6. window.location.Reload()和window.location.href 区别
  7. LCD驱动(FrameBuffer)实例开发讲解
  8. checking for oracle home incompatibilities failed
  9. android离线下载的相关知识
  10. Ext中图片上传预览的问题,困扰了好几天终于解决了,记录下
  11. poj 1144 Network(割点)
  12. MongoDB系列之三(副本集配置)
  13. CSS3+HTML5特效9 - 简单的时钟
  14. 优化viewHolder
  15. Python中的支持向量机SVM的使用(有实例)
  16. selenium2之文件上传
  17. Exp3 免杀原理与实践 20164314 郭浏聿
  18. 第四节:Task的启动的四种方式以及Task、TaskFactory的线程等待和线程延续的解决方案
  19. 4.9cf自训9..
  20. JAVA高级篇(一、JVM基本概念)

热门文章

  1. Android使用Drawable资源之使用ClipDrawable资源 实现进入条
  2. python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API
  3. 2016/9/25编写java实验报告时对synchronized(同步代码块)的一些感悟
  4. MyEclipse怎么设置个性化代码注释模板
  5. javascript数组的一些方法实例
  6. Linux Shell ---系统命令(1)
  7. androidstudio 配置git和github
  8. MySql开始日期、结束日期查询
  9. Res_Orders_01
  10. HostOnly Cookie和HttpOnly Cookie