HDU 4960 Another OCD patient (interval DP memory-based search)

Source: Internet
Author: User

A string of numbers allows you to determine the minimum cost of merging a string into a string. The first row is a numeric string, and the second row is the cost of merging consecutive I numbers.

Solution: interval DP. You can perform Memory search. If the left side is smaller than the right side and the right side is greater than the right side, the left side is smaller than the left side. Because there is a solution. It's not very easy to say, just look at the code.

Another OCD patient Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 932 accepted submission (s): 329


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
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-10///#define M 1000100///#define LL __int64#define LL long long///#define INF 0x7ffffff#define INF 0x3f3f3f3f#define PI 3.1415926535898#define zero(x) ((fabs(x)<eps)?0:x)using namespace std;const int maxn = 5010;int dp[maxn][maxn];LL sum[maxn];int num[maxn];int dfs(int l, int r){    if(l > r) return 0;    if(dp[l][r] != -1) return dp[l][r];    dp[l][r] = num[r-l+1];    int ll = l, rr = r;    while(ll < rr)    {        if((sum[ll]-sum[l-1]) > (sum[r]-sum[rr-1])) rr--;        else if((sum[ll]-sum[l-1]) < (sum[r]-sum[rr-1])) ll++;        else if((sum[ll]-sum[l-1]) == (sum[r]-sum[rr-1]))        {            dp[l][r] = min(dp[l][r], num[ll-l+1]+dfs(ll+1, rr-1)+num[r-rr+1]);            rr--;        }    }    return dp[l][r];}int main(){    int n;    while(~scanf("%d",&n) && n)    {        sum[0] = 0;        for(int i = 0; i <= n; i++)            for(int j = 0; j <= n; j++) dp[i][j] = -1;        LL x;        for(int i = 1; i <= n; i++)        {            scanf("%I64d",&x);            sum[i] = sum[i-1]+x;        }        for(int i = 1; i <= n; i++) scanf("%d",&num[i]);        cout<<dfs(1, n)<<endl;    }    return 0;}


HDU 4960 Another OCD patient (interval DP memory-based search)

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.