USACO 1.3 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.
————————————————————————题解
这题™辣么水为啥我提交了6次才过啊orz
我本不该对什么常数优化有什么期待……我一开始觉得应该是某个长为17区间,然后两边往那里靠,我觉得这个区间应该在中间
但是好像有些数据会被卡,然后我想起来range只有100
™我暴力枚举每个长为17的区间就好啦orz
:-(唉,好蒟蒻啊……
复杂度也只有106简直妥妥过嘛……
大意是一个农夫为了逃税要破坏自然,比如给山降低或给山增高,使得最大最小的山差值严格控制在17以内,包括17,降低或增高一个高度x的花费是x2,一个山只能修改一次高度
1 /* 2 PROB: skidesign 3 LANG: C++ 4 ID: jiaqi si 5 */ 6 #include <iostream> 7 #include <string.h> 8 #include <cstdlib> 9 #include <cstdio> 10 #include <cmath> 11 #include <algorithm> 12 #include <cstring> 13 #include <vector> 14 #define ivory 15 #define mo 1000000007 16 #define siji(i,x,y) for(int i=(x);i<=(y);i++) 17 #define gongzi(j,x,y) for(int j=(x);j>=(y);j--) 18 #define xiaosiji(i,x,y) for(int i=(x);i<(y);i++) 19 #define sigongzi(j,x,y) for(int j=(x);j>(y);j--) 20 #define pii pair<int,int> 21 #define fi first 22 #define se second 23 #define mo 1000000007 24 using namespace std; 25 int n; 26 int a[1005]; 27 int ans; 28 int all=0x1f1f1f1f; 29 inline int o(int x) {return x*x;} 30 int main() { 31 #ifdef ivory 32 freopen("skidesign.in","r",stdin); 33 freopen("skidesign.out","w",stdout); 34 #else 35 freopen("f1.in","r",stdin); 36 #endif 37 scanf("%d",&n); 38 siji(i,1,n) {scanf("%d",&a[i]);} 39 sort(a+1,a+n+1); 40 int l,r; 41 siji(i,0,100-17) { 42 l=i; 43 r=l+17; 44 ans=0; 45 siji(i,1,n) { 46 if(a[i]>r) ans+=o(a[i]-r); 47 if(a[i]<l) ans+=o(l-a[i]); 48 } 49 all=min(all,ans); 50 } 51 52 printf("%d\n",all); 53 }