Han's father hasNA son, respectively, is Hanil, Han two ... HanN。 Due to the strong skills of the Korean family, coupled with their close cooperation, the performance has been a great success, the box office even up -Million. Boat Son is a very prestigious public knowledge, but he penniless afterwards on the surface of the dark, see Korean home booming, jealous heart then, then made a meager ridicule kanji stand in a column when height uneven. Because of the influence of the boat, casually a sentence will cause the great loss of the Korean family, the specific loss is calculated, Hanil, Han two ... HanNStand in a row, the loss isCx (HanIWith HanI+1 The height difference (1≤i<N ) and the sum, not even the daughter has lost. Han father hard thinking, decided to give the South Korean children in the increase (pay attention to the South Korean children are not scientific can only increase or do nothing), increase1CM is very easy, but the increaseTenCM costs a lot, for any Korean i, the cost of increasing hcm is H2. Please help the Korean father to make the Korean family the least loss.
Input
There are several sets of data that have been processed until the end of the file.
First behavior for each set of data two integers: Han Zi numberN(1≤N≤50000 ) and the coefficient of the boat childc ( 1≤c≤100 )
The next N rows are the height of Korea I (1≤hi≤).
Output
For each set of test data, the minimum loss for the Korean family is output.
Sample Input and output
Sample Input |
Sample Output |
5 223514 |
15 |
Problem Solving Report
Use monotone queue to optimize (positive solution) ... Space complexity can also be optimized to O (100*2) ... Well, I'm not optimizing.
At that time, the first use of the line tree to O (log) to get the best solution, the results of the test run limit data 1.5s+ ... To give up (a constant too large)
After the use of RMQ ... I was handed a tle, and my thoughts were completely broken.
#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>typedefLong Longll;using namespacestd;Const intMAXN = 5e4 + -;intn,c,h[maxn];ll q[ the];ll f[maxn][ the];ll MAX;intMainintargcChar*argv[]) {MAX=1<< -; MAX*=MAX; while(SCANF ("%d%d", &n,&c)! =EOF) { for(inti =1; I <= N; ++i) scanf ("%d", H +i); for(inti =1; I <= N; ++i) for(intj =1; J <= -; ++j) F[i][j]=MAX; for(inti = h[1] ; I <= -; ++i) f[1][i] = (i-h[1]) * (i-h[1]); intfront, rear; for(inti =2; I <= N; ++i) {front= Rear =0; for(intj = h[i-1] ; J <= -; ++j) { while(Front < rear && f[i-1][q[rear-1]]-c*q[rear-1] > f[i-1][j]-c*j) Rear--; Q[rear++] =J; } for(intj = H[i]; J <= -; ++j)if(J >=Q[front]) f[i][j]= f[i-1][q[front]]-C*q[front] + c*j + (J-h[i]) * (J-H[i]); Front= Rear =0; for(intj = h[i-1] ; J <= -; ++j) { while(Front < rear && f[i-1][q[rear-1]]+c*q[rear-1] > f[i-1][j]+c*j) Rear--; Q[rear++] =J; } for(intj = H[i]; J <= -; ++j) { while(J > Q[front] && Front <rear) Front++; if(Q[front] >=j) F[i][j]= Min (f[i][j],f[i-1][q[front]] + C*q[front]-c*j + (J-h[i]) * (J-h[i])); }} ll ans=F[n][h[n]]; for(intj = H[n]; J <= -; ++j) Ans=min (ans,f[n][j]); printf ("%lld\n", ans); } return 0;}
Uestc_ I want to grow tall cdoj 594