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中的上界,第二层遍历所有的山峰计算答案.并 ...
随机推荐
- iOS常用基础框架
一,简述 1.1,IOS操作系统的层次架构 iOS为应用程序开发提供了许多可使用的框架,并构成IOS操作系统的层次架构,分为四层,从上到下依次为:Cocoa Touch Layer( ...
- MySQL事务隔离级别详解(转)
原文: http://xm-king.iteye.com/blog/770721 SQL标准对事务定义了4种隔离级别,包括了一些具体规则,用来限定事务内外的哪些改变是可见的,哪些是不可见的.低级别的隔 ...
- 正则验证ip
用python爬获取这样一条数据: <td class="ip" id="ip"><p style="display: none;& ...
- CDN工作过程(第二种版本)
作者:代希刚链接:https://www.zhihu.com/question/36514327/answer/121026637来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- mysql 内置功能 视图介绍
之前的多表查询本质是把多张有关系的表连接在一起组成一张虚拟表,从而进行查询 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名], 用户使用时只需使用[名称]即 ...
- POJ1258:Agri-Net(最小生成树模板题)
http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of hi ...
- SQLserver数据库连接问题
可能安装好之后数据库的端口1433被防火墙拦截了,查看端口是否在监听当中: 在cmd里输入命令 :netstat -an 查看是否处在监听中,如果没有进入下面的设置, C:\Windows\SysWO ...
- PAT 1028 List Sorting[排序][一般]
1028 List Sorting (25)(25 分) Excel can sort records according to any column. Now you are supposed to ...
- 数据分析与挖掘 - R语言:K-means聚类算法
一个简单的例子!环境:CentOS6.5Hadoop集群.Hive.R.RHive,具体安装及调试方法见博客内文档. 1.分析题目--有一个用户点击数据样本(husercollect)--按用户访问的 ...
- !! A股历史平均市盈率走势图
http://value500.com/PE.asp 一. A股历史平均市盈率走势图 *数据来源:上海证券交易所 分享到: 354 - 上海A股 深圳A股更新时间 2017年6月7日 2017年6月7 ...