码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4960 记忆化搜索 DP

时间:2014-08-20 15:55:02      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

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)
 

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<map>
 9 
10 #define N 5005
11 #define M 15
12 #define mod 6
13 #define mod2 100000000
14 #define ll long long
15 #define maxi(a,b) (a)>(b)? (a) : (b)
16 #define mini(a,b) (a)<(b)? (a) : (b)
17 
18 using namespace std;
19 
20 int n;
21 ll v[N],sum[N];
22 int a[N],dp[N][N];
23 
24 int DP(int l,int r)
25 {
26     //int i;
27     //ll s1,s2;
28     if(dp[l][r]!=-1) return dp[l][r];
29     dp[l][r]=a[r-l+1];
30     if(l>=r) return dp[l][r]=0;
31     
32     //i=l;
33     int now=l;
34     ll re;
35     for(int i=r;i>=l;i--){
36         re=sum[r]-sum[i-1];
37         while(sum[now]-sum[l-1]<re && now<i)
38             now++;
39         if(now==i) break;
40         if(sum[now]-sum[l-1]==re){
41             dp[l][r]=min(dp[l][r],DP(now+1,i-1)+a[now-l+1]+a[r-i+1]);
42         }
43     }
44     return dp[l][r];
45 }
46 
47 int main()
48 {
49     int i;
50     //freopen("data.in","r",stdin);
51     //scanf("%d",&T);
52     //for(int cnt=1;cnt<=T;cnt++)
53     //while(T--)
54     while(scanf("%d",&n)!=EOF)
55     {
56         if(n==0) break;
57         memset(dp,-1,sizeof(dp));
58         //memset(sum,0,sizeof(sum));
59         for(i=1;i<=n;i++){
60             scanf("%I64d",&v[i]);
61             sum[i]=sum[i-1]+v[i];
62         }
63         for(i=1;i<=n;i++){
64             scanf("%d",&a[i]);
65         }
66         DP(1,n);
67         printf("%d\n",dp[1][n]);
68     }
69 
70     return 0;
71 }

 

hdu 4960 记忆化搜索 DP,布布扣,bubuko.com

hdu 4960 记忆化搜索 DP

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/njczy2010/p/3924538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!