HDU4960:Another OCD Patient

来源:互联网 发布:apache storm java应用 编辑:程序博客网 时间:2024/05/19 12:40
Problem Description
Xiaoji 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 line is symmetrical! For example, (10, 20, 20, 10), (4,1,4) and (2) are symmetrical but (3,1,2), (3, 1, 1) and (1, 2, 1, 2) are not.

However, because Xiaoji's OCD is more and more serious, 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 would not use it to merge again. Don't ask why. You should know Xiaoji has an OCD.
 

Input
The 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.
 

Output
Output one line containing the minimum cost of all operations Xiaoji needs.
 

Sample Input
56 2 8 7 10 5 2 10 200
 

Sample Output
10
Hint
In 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.
按照题目,我们可以发现左右端点不相等的时候,那么小的那个肯定要与邻近的数进行合并
那么通过这个方法对整个数组来一次扫描,将其先合并成一个回文串,再统计各个部分合并了几个数,分别记为l区间和r区间
然后再对l和r进行dp,统计将这个回文串再进行合并的各种情况下的最小花费
最后枚举所有情况,找到最小值
#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,b) memset(a,b,sizeof(a))#define w(x) while(x)#define inf 99999999#define l 5005#define s(a) scanf("%d",&a)int dp[l],a[l],cost[l],lnum,rnum,lsum,rsum,len,i,j,ll[l],rr[l],ans,n;int main(){    w((s(n),n))    {        up(i,1,n)        s(a[i]);        up(i,1,n)        s(cost[i]);        len=0;        for(i=1,j=n; i<j; i++,j--)//扫描,对整个串进行合并,成为回文        {            lsum=a[i],rsum=a[j];            lnum=rnum=1;            w(lsum!=rsum)            {                if(i>=j)                    break;                if(lsum<rsum)                {                    lsum+=a[++i];                    lnum++;                }                else                {                    rsum+=a[--j];                    rnum++;                }            }            if(lsum==rsum)            {                len++;                ll[len]=lnum;                rr[len]=rnum;            }        }        up(i,1,len)//对两端进行DP        {            int t1=0,t2=0;            dp[i]=inf;            down(j,i,1)            {                t1+=ll[j];                t2+=rr[j];                dp[i]=min(dp[i],dp[j-1]+cost[t1]+cost[t2]);            }        }        ans=cost[n];        up(i,1,len)        {            n-=ll[i]+rr[i];//中间部分进行合并            ans=min(ans,dp[i]+cost[n]);        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击