[题解]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

 1 /*
 2 ID:
 3 PROG: skidesign
 4 LANG: C++11
 5 */
 6 /**
 7  * USACO
 8  * Accepted
 9  * Time:0ms
10  * Memory:4180k
11  */
12 #include<iostream>
13 #include<cstdio>
14 #include<cctype>
15 #include<cstring>
16 #include<cstdlib>
17 #include<cmath>
18 #include<fstream>
19 #include<sstream>
20 #include<algorithm>
21 #include<map>
22 #include<set>
23 #include<queue>
24 #include<vector>
25 #include<stack>
26 using namespace std;
27 typedef bool boolean;
28 #define INF 0xfffffff
29 #define smin(a, b) a = min(a, b)
30 #define smax(a, b) a = max(a, b)
31 template<typename T>
32 inline void readInteger(T& u){
33     char x;
34     int aFlag = 1;
35     while(!isdigit((x = getchar())) && x != ‘-‘);
36     if(x == ‘-‘){
37         x = getchar();
38         aFlag = -1;
39     }
40     for(u = x - ‘0‘; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - ‘0‘);
41     ungetc(x, stdin);
42     u *= aFlag;
43 }
44
45 int n;
46 int *hills;
47
48 inline void init(){
49     readInteger(n);
50     hills = new int[(const int)(n + 1)];
51     for(int i = 1; i <= n; i++){
52         readInteger(hills[i]);
53     }
54 }
55
56 int result = INF;
57 inline void solve(){
58     for(int res = 0; res <= 83; res++){
59         int cmp = 0;
60         for(int i = 1; i <= n; i++){
61             if(hills[i] < res){
62                 cmp += (res - hills[i]) * (res - hills[i]);
63             }else if(hills[i] > res + 17){
64                 cmp += (hills[i] - res - 17) * (hills[i] - res - 17);
65             }
66         }
67         smin(result, cmp);
68     }
69     printf("%d\n", result);
70 }
71
72 int main(){
73     freopen("skidesign.in", "r", stdin);
74     freopen("skidesign.out", "w", stdout);
75     init();
76     solve();
77     return 0;
78 }
时间: 2024-10-10 00:20:53

[题解]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 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 h

USACO 1.3 Ski Course Design (枚举)

描述 农民约翰的农场里有N座山峰(1<=N<=1000),每座山都有一个在0到100之间的整数的海拔高度.在冬天,因为山上有丰富的积雪,约翰经常开办滑雪训练营. 不幸的是,约翰刚刚得知税法在滑雪训练营方面有新变化,明年开始实施.在仔细阅读法律后,他发现如果滑雪训练营的最高和最低的山峰海拔高度差大于17就要收税.因此,如果他改变山峰的高度(使最高与最低的山峰海拔高度差不超过17),约翰可以避免支付税收. 如果改变一座山x单位的高度成本是x^2单位,约翰最少需要付多少钱?约翰只愿意改变整数单位的高

USACO Section1.3 Ski Course Design 解题报告

skidesign解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] N座山,每座山高度是0到100的整数.我们要调整山高,让最高的山和最低的山高度差不超过17. 将一座山的高度调整x,花费是

【USACO 1.3】Ski Course Design

n个点(n<=1000)大小范围[0,100],改变一些点的值,使得极差不超过17,代价为改变值的平方. 枚举修改后的最低高度low,维护最小代价. /* TASK: skidesign LANG:C++ URL:http://train.usaco.org/usacoprob2?a=LxVrSLLAzuR&S=skidesign */ #include <iostream> #include <algorithm> #include <cstdio> #

usaco Ski Course Design

只有1000个数,每个数在0到100 之间,暴力可做. /* ID: modengd1 PROG: skidesign LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> using namespace std; int N; int input[1000]; int main() { freopen("skidesign.in","r",

USACO Ski Course Design 暴力

从Min到Max范围内暴力一下即可. /* ID: wushuai2 PROG: skidesign LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cma

[USACO][枚举]Ski Course Design

这道题位于第一章section1.4贪心部分. 题意: 给出N个山峰高度,高度属于[0,100]units:Farmer FJ需要做一些修改,来保证最高高度和最低高度的差值不大于17uints.这个修改包括减小最高山峰和增高最低山峰高度,并且修改x unit的高度要付出x^2的费用. 给出N个山峰的高度,求出满足要求修改的最小费用. 思路: 枚举所有可能的高度上下限(约定maxh-minh==17),判断此时的耗资是否最小. 排序实际上是没有必要的.只要每次都完全遍历hil[]数组,判断比下限小

[题解]USACO 1.3 Wormholes

Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfired, causing N wormholes (2 <= N <= 12, N even) to materialize on his farm, each located at a distinct point on the 2D map of his farm (the x,y coordin

题解 UVA1349 【Optimal Bus Route Design】

题目链接 Solution UVA1349 Optimal Bus Route Design 题目大意:给定一个带权有向图,选出若干个简单环,使每个点含于且仅含于一个环,并使得边权和最小 分析:既然每个点仅被包含于一个简单环,那么每个点的入度与出度都为\(1\),也就是这个点有且仅有一条入(出)边.但是我们又不能贪心的去选这个点入边/出边中边权最小的一条边,这样选出来的方案可能根本不合法(不能保证每个点都有入边,可能出现一个环加上一条链之类鬼畜的情况) 那么我们回想一下在无权\(DAG\)上我们