Hdu4960another OCD patient (interval DP, block before DP)

Source: Internet
Author: User
Another OCD patient Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 716 accepted submission (s): 270


Problem descriptionxiaoji is an OCD (obsessive-compulsive disorder) patient. this morning, his children played with plasticene. they broke the plasticene into N pieces, and put them in a line. each piece has a volume VI. since Xiaoji is an OCD patient, he can't stand with the disorder of the volume of the N pieces of plasticene. now he wants to merge some successive pieces so that the volume in Lin E is invalid rical! For example, (10, 20, 20, 10), (, 4) and (2) are using rical but (, 2), (3, 1, 1) and (1, 2, 1, 2) are not.

However, because Xiaoji's OCD is more and more serous, now he has a strange opinion that merging I successive pieces into one will cost AI. and he wants to achieve his goal with minimum cost. can you help him?

By the way, if one piece is merged by Xiaoji, He wocould not use it to merge again. Don't ask why. You shoshould know Xiaoji has an OCD.
Inputthe input contains multiple test cases.

The first line of each case is an integer N (0 <n <= 5000), indicating the number of pieces in a line. the second line contains N integers VI, volume of each piece (0 <VI <= 10 ^ 9 ). the third line contains N integers AI (0 <AI <= 10000), And A1 is always 0.

The input is terminated by n = 0.
Outputoutput one line containing the minimum cost of all operations Xiaoji needs.
Sample Input
56 2 8 7 10 5 2 10 200

Sample output
10HintIn the sample, there is two ways to achieve Xiaoji‘s goal.[6 2 8 7 1] -> [8 8 7 1] -> [8 8 8] will cost 5 + 5 = 10.[6 2 8 7 1] -> [24] will cost 20. 

Authorsysu
Source2014 multi-university training contest 9: returns the number of N, which is a symmetric set. The third row indicates that a [I] is required for the number of I merged at a time, and the minimum cost is obtained. Solution: Combine n numbers into a symmetric set of K blocks, and then perform symmetric interval DP.
# Include <stdio. h >__ int64 V [5005], a [5005], pre [5005], DP [5005]; void DFS (int l, int R) // block, symmetric {int I = L, j = r; _ int64 suml = V [L], sumr = V [R]; while (I <j) {If (suml = sumr) {if (I + 1 <= J-1) DFS (I + 1, J-1); Pre [l] = I; pre [J] = r; // The leftmost rightmost block of the interval and return;} If (suml <sumr) suml ++ = V [++ I]; else sumr + = V [-- J];} Pre [l] = r; // only one block can be merged in one interval} After void count (int K) // is divided into k blocks, expand the range from the most intermediate block to the two sides for DP, pre [I] indicates from the first block to {int L, R, M; If (K % 2) {L = K/2; r = K/2 + 2; M = K/2 + 1; // m is the most intermediate block DP [m] = A [pre [m]-pre [M-1];} else {L = K/2; R = K/2 + 1; M = L; DP [l] = 0;} while (r <= K) {DP [R] = A [pre [R]-pre [L-1]; // For (int tr = r, TL = L; m <TR; tr --, Tl ++) // find the corresponding minimum DP [R] If (DP [R]> DP [TR-1] + A [pre [R]-pre [TR-1] + [pre [TL]-pre [L-1]) DP [R] = DP [TR-1] + A [pre [R]-pre [TR-1] + A [pre [TL]-pre [L-1]; l --; r ++; // amplified to both sides} int main () {int N, I, K; _ int64 TK; while (scanf ("% d ", & N)> 0 & N) {for (I = 1; I <= N; I ++) scanf ("% i64d", & V [I]); for (I = 1; I <= N; I ++) scanf ("% i64d", & A [I]); DFS (1, N); k = 0; I = 1; Pre [0] = 0; while (I <= N) // scale a whole block into a point, the number of Pre [k] blocks that are converted into the first K blocks makes up k blocks {TK = pre [I]-I + 1; I = pre [I] + 1; ++ K; pre [k] = TK + pre [k-1];} count (k); printf ("% i64d \ n", DP [k]);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.