hdu 4960 记忆化搜索 DP

Another OCD Patient

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 490    Accepted Submission(s): 180

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

 
Author
SYSU
 
Source
 
Recommend
We have carefully selected several similar problems for you:  4970 4968 4967 4966 4964 
 
记忆化搜索,由于每个碎片值都是正数,所以每个前缀和后缀都是递增的,就可以利用twopointer去找到每个相等的位置,然后下一个区间相当于一个子问题,用记忆化搜索即可,复杂度接近O(n^2)
 
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 5005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
ll v[N],sum[N];
int a[N],dp[N][N]; int DP(int l,int r)
{
//int i;
//ll s1,s2;
if(dp[l][r]!=-) return dp[l][r];
dp[l][r]=a[r-l+];
if(l>=r) return dp[l][r]=; //i=l;
int now=l;
ll re;
for(int i=r;i>=l;i--){
re=sum[r]-sum[i-];
while(sum[now]-sum[l-]<re && now<i)
now++;
if(now==i) break;
if(sum[now]-sum[l-]==re){
dp[l][r]=min(dp[l][r],DP(now+,i-)+a[now-l+]+a[r-i+]);
}
}
return dp[l][r];
} int main()
{
int i;
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
memset(dp,-,sizeof(dp));
//memset(sum,0,sizeof(sum));
for(i=;i<=n;i++){
scanf("%I64d",&v[i]);
sum[i]=sum[i-]+v[i];
}
for(i=;i<=n;i++){
scanf("%d",&a[i]);
}
DP(,n);
printf("%d\n",dp[][n]);
} return ;
}
上一篇:案例学python——案例一:抓图


下一篇:设置frameset高度