USACO 1.3 Ski Course Design - 暴力
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 ;
}
USACO 1.3 Ski Course Design - 暴力的更多相关文章
- [题解]USACO 1.3 Ski Course Design
Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer ...
- USACO 1.3 Ski Course Design
Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer ...
- USACO Ski Course Design 暴力
从Min到Max范围内暴力一下即可. /* ID: wushuai2 PROG: skidesign LANG: C++ */ //#pragma comment(linker, "/STA ...
- USACO Section1.3 Ski Course Design 解题报告
skidesign解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...
- 洛谷 P3650 [USACO1.3]滑雪课程设计Ski Course Design
P3650 [USACO1.3]滑雪课程设计Ski Course Design 题目描述 农民约翰的农场里有N座山峰(1<=N<=1000),每座山都有一个在0到100之间的整数的海拔高度 ...
- 【USACO 1.3】Ski Course Design
n个点(n<=1000)大小范围[0,100],改变一些点的值,使得极差不超过17,代价为改变值的平方. 枚举修改后的最低高度low,维护最小代价. /* TASK: skidesign LAN ...
- USACO Section 1.3 Ski Course Design 解题报告
题目 题目描述 有N座山,每座山都有一个高度,现在由于农夫想避税,所以想把这些山的高度进行一些改变,使得最高的山与最低的山之间的高度差不超过17.每座山最多只能改变一次高度,每次改变高度都会产生一定的 ...
- 「日常训练」「小专题·USACO」 Ski Course Design (1-4)
题目 以后补 分析 mmp这题把我写蠢哭了 我原来的思路是什么呢? 每轮找min/max,然后两个决策:升min/降max 像这样子dfs找最优,然后花式剪枝 但是一想不对啊,这才1-4,哪有那么复杂 ...
- USACO 1.3.6 Ski Course Design[滑雪课程设计]
先说说思路: 这题比上一道坑人的wormholes简单多了!我一看到这题,“XXX设计”,还以为要用到什么dp呢,没想到是水题 用两层循环,第一层循环相差17中的上界,第二层遍历所有的山峰计算答案.并 ...
随机推荐
- MySQL复制日常维护与管理
一.复制一些常见设置 1.mysql复制启动时参数: mysql启动时的参数包括:master_host,master_port,master_user,master_password,master_ ...
- LoadRunner-参数化(参数间关联)
多用户测试,每个用户账号对应一个密码,账号和密码需要一 一关联才能登录成功. 1.在已经对账号进行参数化后,选中密码的值->右键选择Replace with a Parameter,参数名为pa ...
- mybatis联接查询例子
where判断如果放在最外层就是对连接查询后的结果经行筛选. SELECT * from ( and lw_area.area_id like '35%' ) la LEFT JOIN ( selec ...
- __getattr__,settr
__getattr__ 如果属性查找在实例以及对应的类中(通过__dict__)失败, 那么会调用到类的__getattr__函数, 如果没有定义这个函数,那么抛出AttributeError异常. ...
- Python几种并发实现方案的性能比较
http://blog.csdn.net/permike/article/details/54846831 Python几种并发实现方案的性能比较 2017-02-03 14:33 1541人阅读 评 ...
- python基础班-淘宝-目录.txt
卷 TOSHIBA EXT 的文件夹 PATH 列表卷序列号为 AE86-8E8DF:.│ python基础班-淘宝-目录.txt│ ├─1-1 Linux基础│ ├─01-课程简介│ │ 01-课程 ...
- Unity 脚本的执行顺序
在Unity脚本中常用到的函数就是下面这些,他们的顺序也是按照箭头的方向执行的. Awake ->OnEable-> Start -> FixedUpdate-> Update ...
- 使用idea maven开发spring boot 分布式开发入门
1:使用idea创建一个maven工程 bos2 2:删除bos2的src 文件夹,向pom.xml文件 中添加版本号 <packaging>pom</packaging> & ...
- Google面试题[一]
谷歌是不少IT人都想去的企业,那么在进入公司前,少不了面试笔试的测试.那么这里我们就总结了如下谷歌笔试题,并提供了一些参考答案.希望对您有用. 谷歌笔试题:判断一个自然数是否是某个数的平方.当然不能使 ...
- Java编写验证码
Java后台代码(CheckCodeServlet.java) package web; import java.awt.Color; import java.awt.Font; import jav ...