Another OCD Patient HDU - 4960 (dp+前缀和)

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
5
6 2 8 7 1
0 5 2 10 20
0
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.

大致题意:就是给你n个数的一个序列,然后你可以将连续的一串数相加合并一个数,告诉你合并1个到n个数的花费,问使得这个序列变成一个回文串所需花费的最少代价。

思路:假设dp[l][r]表示使得l到r这个区间内的序列变成回文串的最小花费,那么状态转移方程为
dp[l][r]=min(dp[l][r],dp[i][j]+cost[i-l+1]+cost[r-j+1]) ( l < i < j < r ,假设l到i区间的数总和等于j到r区间数的总和,那么分别将他们合并,左右两边的数就相等了),这里我们用前缀和来快速求出某个区间的和。

代码如下

#include <iostream> 
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
#include <map>
using namespace std; 
#define LL long long int 
LL pre[5005];
int cost[5005];
int dp[5005][5005];
int n;
int dfs(int l,int r)
{
	if(l>r) return 0;
	if(dp[l][r]!=-1) return dp[l][r];//假如该区间最小花费值已经求出,则直接返回
	dp[l][r]=cost[r-l+1];//最坏情况假设将这个区间全部合并
	
	int ll=l,rr=r;
	while(ll<rr)//遍历该区间内所有合法情况
	{
		LL sum1=pre[ll]-pre[l-1];//将ll位置左边的数全部合并
		LL sum2=pre[r]-pre[rr-1];//将rr位置右边的数全部合并
		
		//存在以下三种情况
		if(sum1<sum2)
		{
			ll++;
			continue;
		}
		if(sum1>sum2)
		{
			rr--;
			continue;
		}
		if(sum1==sum2)
		{
			dp[l][r]=min(dp[l][r],cost[ll-l+1]+cost[r-rr+1]+dfs(ll+1,rr-1));
			ll++;
		}
	}
	return dp[l][r];
}
int main()  
{    
	while(scanf("%d",&n)!=EOF&&n)
	{
		pre[0]=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%lld",&pre[i]);
			pre[i]+=pre[i-1];	
			for(int j=1;j<=n;j++)
			dp[i][j]=-1;//初始化dp数组
		}	
		for(int i=1;i<=n;i++)
		scanf("%d",&cost[i]);
		
		printf("%d\n",dfs(1,n));
	}
  	return 0;
} 	 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值