Lawrence
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1523 accepted submission (s): 671
Problem descriptiont. e. lawrence was a controversial figure during World War I. he was a briish officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. his primary targets were the railroads. A highly fictionalized version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia ".
You are to write a program to help Lawrence figure out how to best use his limited resources. you have some information from briish intelligence. first, the rail line is completely linear --- there are no branches, no spurs. next, briish intelligence has assigned a strategic importance to each depot --- an integer from 1 to 100. A depot is of no use on its own, it only has value if it is connected to other depots. the strategic value of the entire railroad is calculated by adding up the products of the strategic values for every pair of depots that are connected, directly or indirectly, by the rail line. consider this railroad:
Its strategic value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.
Now, suppose that Lawrence only has enough resources for one attack. he cannot attack the depots themselves --- they are too well defended. he must attack the rail line between depots, in the middle of the desert. consider what wowould happen if Lawrence attacked this rail line right in the middle:
The strategic value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:
The strategic value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence's best option.
Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest strategic value that he can achieve for that railroad.
Inputthere will be several data sets. each data set will begin with a line with two integers, N and M. n is the number of depots on the railroad (1 ≤ n ≤ 1000), and m is the number of attacks Lawrence has resources for (0 ≤ m <n ). on the next line will be n integers, each from 1 to 100, indicating the strategic value of each depot in order. end of input will be marked by a line with n = 0 and m = 0, which shoshould not be processed.
Outputfor each data set, output a single integer, indicating the smallest strategic value for the railroad that Lawrence can achieve with his attacks. output each integer in its own line.
Sample input4 1
4 5 1 2
4 2
4 5 1 2
0 0
Sample output17
2
Source2009 multi-university training Contest 2-host by tju
Recommendgaojie is a classic topic. For the second explanation, see comments.
/* * Use DP [I] [x] to represent the first I point, the maximum value that can be damaged when x edge is blown up * The answer is TOL-DP [N] [m] * DP [I] [x] = max {DP [J] [x-1] + sum [J] * (sum [I]-sum [J])} x-1 <j <I * assume that K <J, J is better than K. * DP [k] [x-1] + sum [k] * (sum [I]-sum [k]) <= DP [J] [x-1] + sum [J] * (sum [I]-sum [J]) * simplified (sum [J] * sum [J]-DP [J] [x-1]) -(sum [k] * sum [k]-DP [k] [x-1]) /(sum [J]-sum [k] <= sum [I] ** YJ = sum [J] * sum [J]-DP [J] [x-1] XJ = sum [J] * (YJ-YK) /(XJ-XK) <= sum [I] * The Right Inequality is incremental * g [K, J] = (YJ-YK)/(XJ-XK) * The above inequalities indicate that J is better than K. * if k <j <I g [K, J]> G [I, j], K can be eliminated. * If G [J, i] <sum [I] J can be eliminated ** */ # Include <Iostream># Include < String . H> # Include <Stdio. h> # Include <Algorithm> Using Namespace STD; Const Int Maxn = 1010 ; Int N, m; Int A [maxn]; Int Sum [maxn]; Int DP [maxn] [maxn]; Int Tol; Int Getdp ( Int I, Int X, Int J ){ Return DP [J] [x- 1 ] + Sum [J] * (sum [I]- Sum [J]);} Int Getup (Int J, Int X, Int K ){ Return Sum [J] * sum [J]-DP [J] [x- 1 ]-(Sum [k] * sum [k]-DP [k] [x- 1 ]);} Int Getdown ( Int J, Int K ){ Return Sum [J]- Sum [k];} Int Q [maxn]; Void Solve () {memset (DP, 0 , Sizeof (DP )); Int Front, rear; For ( Int X = 1 ; X <= m; X ++ ) {Rear = Front = 0 ; Q [rear ++] =X; For ( Int I = x + 1 ; I <= N; I ++ ){ While (Front + 1 <Rear & getup (Q [Front + 1 ], X, Q [Front]) <= sum [I] * getdown (Q [Front + 1 ], Q [Front]) Front ++ ; DP [I] [x] = Getdp (I, X, Q [Front]); While (Front + 1 <Rear & getup (I, X, Q [rear- 1 ]) * Getdown (Q [rear- 1 ], Q [rear- 2 ]) <= Getup (Q [rear- 1 ], X, Q [rear- 2 ]) * Getdown (I, Q [rear- 1 ]) Rear -- ; Q [rear ++] = I ;}} printf ( " % D \ n " , Tol- DP [N] [m]);} Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); While (Scanf ( " % D " , & N, & M) = 2 ){ If (N = 0 & M = 0 ) Break ; Sum [ 0 ] = 0 ; For ( Int I = 1 ; I <= N; I ++ ) {Scanf ( " % D " ,& A [I]); sum [I] = Sum [I- 1 ] + A [I];} Tol = 0 ; For ( Int I = N; I> 1 ; I -- ) Tol + = A [I] * sum [I- 1 ]; Solve ();} Return 0 ;}