标签:
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.
Line 1: | The integer N. |
Lines 2..1+N: | Each line contains the elevation of a single hill. |
5 20 4 1 24 21
FJ‘s farm has 5 hills, with elevations 1, 4, 20, 21, and 24.
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: |
18
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.
题目大意:就是说有n座山(1<=n<=1000),每座山高度为h_i(1<=h_i<=100),现在希望最高峰与最底峰高度差不大于17,一座山的高度被增加x单位或减少x单位的代价为x^2(每座山最多只能操作一次),问达成目的的最小代价。
思路:看数据量,枚举最底峰高度就可以(我枚举了一个中间值),随便做,不会超时。
1 /* 2 ID:fffgrdcc1 3 PROB:skidesign 4 LANG:C++ 5 */ 6 #include<cstdio> 7 #include<iostream> 8 using namespace std; 9 int a[1002]; 10 int main() 11 { 12 freopen("skidesign.in","r",stdin); 13 freopen("skidesign.out","w",stdout); 14 int n; 15 scanf("%d",&n); 16 int l=10000,r=0,mid;//l is min,r is max 17 for(int i=1;i<=n;i++) 18 { 19 scanf("%d",&a[i]); 20 if(a[i]>r)r=a[i]; 21 if(a[i]<l)l=a[i]; 22 } 23 mid=(l+r)/2;l=r=mid;l--;r++; 24 int ans=0; 25 for(int i=1;i<=n;i++) 26 { 27 if(a[i]<mid) 28 { 29 if(a[i]+8<mid) 30 { 31 ans+=(a[i]+8-mid)*(a[i]+8-mid); 32 } 33 } 34 else 35 { 36 if(a[i]-9>mid) 37 { 38 ans+=(a[i]-9-mid)*(a[i]-9-mid); 39 } 40 } 41 } 42 int temp=0;mid=l; 43 for(int i=1;i<=n;i++) 44 { 45 if(a[i]<mid) 46 { 47 if(a[i]+8<mid) 48 { 49 temp+=(a[i]+8-mid)*(a[i]+8-mid); 50 } 51 } 52 else 53 { 54 if(a[i]-9>mid) 55 { 56 temp+=(a[i]-9-mid)*(a[i]-9-mid); 57 } 58 } 59 } 60 while(temp<ans) 61 { 62 ans=temp; 63 mid--; 64 temp=0; 65 for(int i=1;i<=n;i++) 66 { 67 if(a[i]<mid) 68 { 69 if(a[i]+8<mid) 70 { 71 temp+=(a[i]+8-mid)*(a[i]+8-mid); 72 } 73 } 74 else 75 { 76 if(a[i]-9>mid) 77 { 78 temp+=(a[i]-9-mid)*(a[i]-9-mid); 79 } 80 } 81 } 82 } 83 temp=0;mid=r; 84 for(int i=1;i<=n;i++) 85 { 86 if(a[i]<mid) 87 { 88 if(a[i]+8<mid) 89 { 90 temp+=(a[i]+8-mid)*(a[i]+8-mid); 91 } 92 } 93 else 94 { 95 if(a[i]-9>mid) 96 { 97 temp+=(a[i]-9-mid)*(a[i]-9-mid); 98 } 99 } 100 } 101 while(temp<ans) 102 { 103 ans=temp; 104 mid++; 105 temp=0; 106 for(int i=1;i<=n;i++) 107 { 108 if(a[i]<mid) 109 { 110 if(a[i]+8<mid) 111 { 112 temp+=(a[i]+8-mid)*(a[i]+8-mid); 113 } 114 } 115 else 116 { 117 if(a[i]-9>mid) 118 { 119 temp+=(a[i]-9-mid)*(a[i]-9-mid); 120 } 121 } 122 } 123 } 124 printf("%d\n",ans); 125 return 0; 126 }
但是不能止步于此,这道题目可以当做三分的基础题做做看,代码如下
1 /* 2 ID:fffgrdcc1 3 PROB:skidesign 4 LANG:C++ 5 */ 6 #include<cstdio> 7 #include<iostream> 8 using namespace std; 9 int a[1002],n; 10 int calc(int mid) 11 { 12 int temp=0; 13 for(int i=1;i<=n;i++) 14 { 15 if(a[i]<mid) 16 { 17 if(a[i]+8<mid) 18 { 19 temp+=(a[i]+8-mid)*(a[i]+8-mid); 20 } 21 } 22 else 23 { 24 if(a[i]-9>mid) 25 { 26 temp+=(a[i]-9-mid)*(a[i]-9-mid); 27 } 28 } 29 } 30 return temp; 31 } 32 int main() 33 { 34 freopen("skidesign.in","r",stdin); 35 freopen("skidesign.out","w",stdout); 36 scanf("%d",&n); 37 int l=10000,r=0,mid;//l is min,r is max 38 for(int i=1;i<=n;i++) 39 { 40 scanf("%d",&a[i]); 41 if(a[i]>r)r=a[i]; 42 if(a[i]<l)l=a[i]; 43 } 44 while(l+1<r)//这里就是三分法的核心部分了 45 { 46 int mid=(l+r)/2; 47 int midmid=(mid+r)/2; 48 int mid_v=calc(mid),midmid_v=calc(midmid); 49 if(mid_v<midmid_v)r=midmid; 50 else l=mid; 51 } 52 printf("%d\n",min(calc(l),calc(r))); 53 return 0; 54 }
写完我就惊了,好简洁,效率也还可以,应该是(logn)^2的,三分算法核心的思路挺简单的,就是在left和right中间有两个点mid=(l+r)/2和midmid=(mid+r)/2,比较两个中间点那个更优秀,远离它的边界就要靠近(注意,这里说的是靠近不是取代),比如mid更好,就让right取代midmid,这样边界就缩小了。
具体的资料见百度文库
标签:
原文地址:http://www.cnblogs.com/xuwangzihao/p/5000318.html