Luo Gu P3650
https://www.luogu.org/problemnew/show/P3650
JDOJ 2393
https://neooj.com:8082/oldoj/problem.php?id=2393
Title Description
Farmer John's farm has peaks N (1 <= N <= 1000), every mountain has an altitude of integers between 0 and 100. In the winter, because the mountains are rich in snow, John often start skiing training camp.
Unfortunately, John has just learned that a new tax law changes in terms of ski training camp, began to implement next year. After carefully reading the law, he found that if skiing training camp highest and lowest peak altitude difference is greater than 17 is necessary to collect taxes. So, if he change the height of the mountain (the highest and lowest peak altitude difference of not more than 17), John avoid paying taxes.
If the cost of changing the height of a mountain unit of x is x ^ 2 units, how much to pay a minimum of John? John is only willing to change the height of integer units.
Input and output formats
Input formats:
The first line: an integer n
Second row to row N + 1: Each line is the altitude of a mountain
Output formats:
The total amount needed to pay John amend mountain altitude between the highest and lowest peak height difference of up to 17.
Sample input and output
5 20 4 1 24 21
18
thought initially thought was done directly after Max Min Ward's found is obviously wrong after reading the sample.
So what is it positive solution? Thus began a painful thinking, thinking this question and found that dynamic programming similar (note no means of dynamic programming, no no aftereffect nature). We need to maintain a minimum spend money for every hill.
Ideas are better understood in two layers enumeration, the first layer enumerate all possible mountain height (certainly between maximum and minimum values), the second layer is an actual height of mountain enumeration determines their 17 and the relationship between a difference, all the above is cut, it is lower than the cut portion is less than 17.
Note that the layers do not reverse, the first layer is highly enumeration, enumeration must be calculated each time the minimum current height, and finally take a small can.
ACcode:
#include<cstdio> #include<algorithm> using namespace std; int n; int a[1010]; int sum; int ans=2147483647; bool cmp(int a,int b) { return a<b; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1,cmp); for(int i=a[1];i<=a[n];i++) { sum=0; for(int j=1;j<=n;j++) { if(a[j]-i>17) sum+=(a[j]-i-17)*(a[j]-i-17); if(a[j]<i) sum+=(i-a[j])*(i-a[j]); } ans=min(sum,ans); } printf("%d",ans); return 0; }