HDU4960Another OCD Patient (interval dp, block before DP)

Source: Internet
Author: User

HDU4960Another OCD Patient (interval dp, block before DP)
Another OCD PatientTime 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
 
  
_ Int64 v [5005], a [5005], pre [5005], dp [5005]; void dfs (int l, int r) // block to [l, r] symmetric {int I = l, j = r; _ int64 suml = v [l], sumr = v [r]; while (I
  
   
Dp [TR-1] + a [pre [r]-pre [TR-1] + a [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.