USACO-Section 1.3 Ski Course Design(枚举)
2015-08-26 23:55
239 查看
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.
思路:数据很小,直接枚举山的最高高度即可,对于山的最高高度i,若i-h[j]>17,则将h[j]的高度调整为i-17;若h[j]>i,则将h[j]的高度调整为i;求出所有最高高度的方案的调整费用的最小值。
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.思路:数据很小,直接枚举山的最高高度即可,对于山的最高高度i,若i-h[j]>17,则将h[j]的高度调整为i-17;若h[j]>i,则将h[j]的高度调整为i;求出所有最高高度的方案的调整费用的最小值。
/* ID: your_id_here PROG: skidesign LANG: C++ */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int h[1005],ans,minh,maxh,tmp,t; int main() { int i,j,n; freopen("skidesign.in","r",stdin); freopen("skidesign.out","w",stdout); while(1==scanf("%d",&n)) { maxh=0,minh=100; for(i=0;i<n;++i) { scanf("%d",h+i); maxh=max(maxh,h[i]);//求山的最高高度 minh=min(minh,h[i]);//求山的最低高度 } ans=0x3f3f3f3f; for(i=minh+17;i<=maxh;++i) {//枚举山的最高高度 tmp=0; for(j=0;j<n;++j) { if((t=i-h[j]-17)>0)//如果此山的高度小于 最高高度-17,将其调整至 最高高度-17 tmp+=t*t; else if((t=h[j]-i)>0)//如果此山的高度大于最高高度,将其调整至最高高度 tmp+=t*t; } ans=min(ans,tmp); } printf("%d\n",ans); } return 0; }
相关文章推荐
- STM32F1-workarea : how to drive a WS2812 RGB LED using PWM and DMA
- UVA 题目760 DNA Sequencing (后缀数组求两个串最长公共子串,字典序输出)
- Android Studio多个module导入AAR库
- UVA 624 CD(01背包问题)
- shell脚本(严格的终端格式控制,美丽的输出字体颜色)
- 1076. Forwards on Weibo (30)限定层数的广搜 or 最短路
- 在linux和windows上安装qt及creator
- 2015年8月20-21日,杨学明老师《青岛民航信息某子公司对标华为产品管理培训》在山东青岛市成功举办!
- Android Studio下使用GoAgent进行sdk更新
- Java并发编程:Callable、Future和FutureTask
- hdu5145 NPY and girls 莫队算法,分块
- php大力力 [020节]mysql数据库唯一id字段如何设置
- mysql命令语句来去除掉字段中空格字符的方法
- linux下杀死进程的若干方法
- Android中广播的使用(动态、静态注册的区别,有序无序广播的使用)
- 配置lenovo G400 + Ubuntu 13.04无线网卡BCM43142
- TortoiseSVN 提交时 hook 的 post-commit.bat 不执行的解决方法
- zepto中$.proxy()的到底有多强大?
- CSS3 icon font完全指南(CSS3 font 会取代icon图标)
- optirun执行错误的解决办法