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 ;
}

hdu 4960 记忆化搜索 DP的更多相关文章

  1. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  2. hdu 1514 记忆化搜索

    题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面的 ...

  3. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  4. HDU - 6415 多校9 Rikka with Nash Equilibrium(纳什均衡+记忆化搜索/dp)

    Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K ...

  5. hdu 2089 记忆化搜索写法(数位dp)

    /* 记忆化搜索,第二维判断是否是6 */ #include<stdio.h> #include<string.h> #define N 9 int dp[N][2],digi ...

  6. 记忆化搜索 dp学习~2

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1331 Function Run Fun Time Limit: 2000/1000 MS (Java/ ...

  7. hdu 4722(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...

  8. HDU 4597 记忆化搜索

    ² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...

  9. 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】

    Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...

随机推荐

  1. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class

    解决方案: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 作用://取消数据库配置 但是 在用到数据库的时候记 ...

  2. 虚拟DOM -------- 最易理解的解释

    虚拟DOM是最先由Facebook在react里使用的, 虚拟DOM是一个特别棒的概念,我们都知道,在浏览器上进行DOM操作的时候,会特别的消耗性能而且响应.渲染特别慢,但是有了虚拟DOM就不一样了, ...

  3. 利用python进行数据分析2_数据采集与操作

    txt_filename = './files/python_baidu.txt' # 打开文件 file_obj = open(txt_filename, 'r', encoding='utf-8' ...

  4. POI 读取 Excel 文件

    import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Out ...

  5. 洛谷 P2668 斗地主

    毒瘤题目,搞了三天-- 也没什么好讲的,就是纯搜索,先搜顺子,再搜其他的,最后单张牌和对子的时候,就不要搜索了,直接枚举,不然会T飞掉多么痛的领悟-- 主要还是靠码力 #include<iost ...

  6. Mac OSX用 dd 命令,浇灌ISO镜像到USB驱动器

    Mac OSX用 dd 命令,浇灌ISO镜像到USB驱动器 字数244 阅读197 评论0 喜欢0 把ISO镜像转换为一个可启动的USB设备.一种可行的方法是通过OS X的Terminal “浇灌”到 ...

  7. js解析器

    1>js的预解析 找var function 参数等 所有的变量,在正式运行代码前,都提前赋了一个值:未定义 所有的函数,在正式运行代码前,都是整个函数块. 遇到重名的:只留一个 如果变量与函数 ...

  8. 【php】 PHP 支持 9 种原始数据类型

    PHP 支持 9 种原始数据类型. 四种标量类型: boolean(布尔型) integer(整型) float(浮点型,也称作 double) string(字符串) 三种复合类型: array(数 ...

  9. 手动搭建redis集群(3台)

    安装redis 1.搜索源中的redis包 apt-cache pkgnames | grep redis 2.安装redis-server apt-get install redis-server ...

  10. python 购物车小程序(列表、循环、条件语句)

    goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...