Description
Recently, Farmer John's cows are increasingly dissatisfied with the mess in the bullpen. So, they asked FJ to replace those old phone lines with better-performing new phone lines. The new telephone line is set up on the existing N (2 <= n <= 100,000) root telephone pole, and the root I telephone pole is height_i m (1 <= height_i <= 100). The telephone line always leads from the top of a telephone pole to the top of the adjacent root if the height of the two telephone poles is different, then the FJ must pay the c* of the telephone pole height difference (1 <= C <= 100). Of course, you can't have a mobile phone pole, you can only set up a telephone line between adjacent poles in the original order. Farmer John thinks raising some telephone poles can reduce the total cost of erecting telephone lines, even though the work also requires a certain amount of expense. More accurately, he would have to pay a x^2 fee if he raised a telephone pole to a height of x meters. Would you please help Farmer John Calculate how much he would spend at least on this phone line renovation project if both of these jobs were to be done properly.
Input
* Line 1th: 2 integers separated by a space: N and C
* 2nd. N+1 Line: Line i+1 has only one integer: height_i
Output
* Line 1th: Output farmer John the minimum cost required to complete the phone line retrofit project
Sample Input5 2
2
3
5
1
4
Input Description:
A total of 5 telephone poles, the cost of pulling the telephone line between the poles is the height difference per meter.
Before the transformation, the height of the telephone bar was 2,3,5,1,4 meters.
Sample Output15
Output Description:
The best way to do this is: Farmer John put the first phone pole up 1 meters and raise the four to 2 meters,
They are 3,3,5,3,4 meters in order of height. So the money spent on the booster poles is $.
At this point, the cost of pulling the phone line is $2* (0+2+2+1) = $ $, total cost is $.
Good
dynamic planning ... In fact, in the end you have to use greedy optimization ... The optimal strategy for a position can be considered on both sides, then update the current optimal solution while updating the state. Poor monotony!
#include <cstdio>#include<algorithm>using namespacestd;intn,c,h,le,f[2][101],i,j,xx;CharCS;intRead () {CS=getchar (); xx=0; while(cs<'0'|| Cs>'9') cs=GetChar (); while(cs>='0'&&cs<='9') xx=xx*Ten+cs- -, cs=GetChar (); returnxx;}intMain () {n=read (); C=read (); h=read (); for(i=1; i+h<= -; i++) f[0][i+h]=i*i; Le=h; intLa=1, now=0, mi,x; for(i=1; i<n;i++) {swap (la,now); H=read (); X=h; if(h<le) {mi=f[la][le]+abs (le-h) *C; for(j=le+1; j<= -; j + +) if(Mi>f[la][j]+abs (j-h) *c) mi=f[la][j]+abs (j-h) *C; for(j=h;j<le;j++) f[now][j]=mi,mi-=C; H=le; } mi=f[la][le]+abs (le-h) *C; for(j=le;jif(Mi>f[la][j]+abs (j-h) *c) mi=f[la][j]+abs (j-h) *C; for(; j<= -; j + +){ if(Mi>f[la][j]) mi=F[la][j]; F[NOW][J]=mi; Mi+=C; } mi=f[la][ -]+C; for(j= About; j>=h;j--){ if(F[NOW][J]>MI) f[now][j]=mi; if(Mi>f[la][j]) mi=F[la][j]; Mi+=C; } for(j=1; j+x<= -; j + +) f[now][j+x]+=j*J; Le=x; } h=F[now][le]; for(i=le+1; i<= -; i++) if(f[now][i]F[now][i]; printf ("%d\n", h);}
Bzoj 1705;poj 3612:[usaco2007 nov]telephone Wire Erection Telephone Line