Problem Solving Ideas:
The subject is a Dynamic Programming topic, the robot turn off the lights either to the left to turn off the lights, or to the right to turn off the lights, that is, to close the next street light is either from the left side of the closed section of the past, or from the closed section of the right side of the past, definition:
Dp[i][j][0] indicates that I to J street lights have been closed, the robot in the position of the street lamp I, at this time has been consumed by the minimum energy
DP[I][J][1] said that I to J street lights have been closed, the robot in the position of the street lamp J, at this time has been consumed by the minimum energy
Then state-shifted:
Dp[i][j][0] = min (dp[i+1][j][0]+[i+1,j] Road outside of the street lights are not closed in the robot from the i+1 I spent energy, DP[I+1][J][1]+[I+1,J] The electric energy consumed by the robot from J to I during the road is not closed.
Dp[i][j][1] = min (dp[i][j-1][0]+[i,j-1) Road not closed the electric energy consumed by the robot from I walk to J, Dp[i][j-1][1]+[i,j-1] The electric energy consumed by the robot from J-1 to J after the road is not closed.
AC Code:
1#include <algorithm>2#include <cstdio>3#include <iostream>4#include <cstring>5 using namespacestd;6 Const intN =1010;7 intdp[n][n][2],dw[n][n],st[n],co[n];8 intMain ()9 {Ten intn,s,v,sum; One while(SCANF ("%d", &n)! =EOF) A { -memset (DW,0,sizeof(DW)); -sum =0; thescanf"%d",&v); - for(inti =1; i<=n; i++) - { -scanf"%d%d",&st[i],&co[i]); +Sum =sum+Co[i]; - } + for(intI=1; i<=n;i++) A for(intj=i;j<=n;j++) atDW[I][J] = dw[i][j-1]+Co[j]; - for(inti = V1; I>0; i--) - { -dp[i][v][0] = dp[i+1][v][0]+ (sum-dw[i+1][V]) * (st[i+1]-st[i]); -dp[i][v][1] = dp[i][v][0] + (Sum-dw[i][v]) * (st[v]-st[i]); - } in for(intJ =v+1; j<=n; J + +) - { todp[v][j][1] =dp[v][j-1][1]+ (sum-dw[v][j-1]) * (st[j]-st[j-1]); +dp[v][j][0] =dp[v][j][1]+ (Sum-dw[v][j]) * (st[j]-st[v]); - } the * for(intI =v-1;i>0; i--) $ {Panax Notoginseng for(intJ =v+1; j<=n; J + +) - { thedp[i][j][0] = min (dp[i+1][j][0]+ (sum-dw[i+1][J]) * (st[i+1]-St[i]), +dp[i+1][j][1]+ (sum-dw[i+1][J]) * (st[j]-st[i])); Adp[i][j][1] = min (dp[i][j-1][0]+ (sum-dw[i][j-1]) * (st[j]-St[i]), thedp[i][j-1][1]+ (sum-dw[i][j-1]) * (st[j]-st[j-1])); + } - } $ $printf"%d\n", Min (dp[1][n][0], dp[1][n][1])); - } - return 0; the}