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
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.
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.
6 2 8 7 1
0 5 2 10 20
0
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.
#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的更多相关文章
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- hdu 1514 记忆化搜索
题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面的 ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- HDU - 6415 多校9 Rikka with Nash Equilibrium(纳什均衡+记忆化搜索/dp)
Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K ...
- hdu 2089 记忆化搜索写法(数位dp)
/* 记忆化搜索,第二维判断是否是6 */ #include<stdio.h> #include<string.h> #define N 9 int dp[N][2],digi ...
- 记忆化搜索 dp学习~2
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1331 Function Run Fun Time Limit: 2000/1000 MS (Java/ ...
- hdu 4722(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...
- HDU 4597 记忆化搜索
² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...
- 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】
Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...
随机推荐
- UVA10917 A walk trough the Forest (最短路,dp)
求出家到其他点的最短路径,题目的条件变成了u->v不是回头路等价于d[u]>d[v]. 然后根据这个条件建DAG图,跑dp统计方案数,dp[u] = sum(dp[v]). #includ ...
- shell 复合条件测试 if [ $1 == "1" -o $1 == "0" ] ------==和-eq怎么用
想要实现: ”,或者$1等于“” ];then 输出一些东西 ”,或者$1等于“” ];then 输出一些东西 fi 这里比较难操作的是等于和或者: 等于: -eq 或者 == 或者: -o 见: ...
- msys2 使用指定boost
pacman -S mingw-w64-x86_64-toolchain make mingw-w64-x86_64-cmake mingw-w64-x86_64-openssl mingw-w64- ...
- Nginx代理tcp端口实现负载均衡
Nginx代理tcp端口实现负载均衡 1.修改配置文件 vi /etc/nginx/nginx.conf 添加如下配置: stream { ###XXX upstream notify { has ...
- iOS使用技巧---高效使用你的xcode
推荐一遍好文章:绝对可以学到关于xcode的很多哟 转载自cocoachina: http://www.cocoachina.com/ios/20140731/9284.html
- ios之alloc和init
复制代码 SomeObject *obj = [[SomeObject alloc] initWithCenter:centerPoint radius:radius]; 和 复制代码 Som ...
- UIViewAnimationOptions
常规动画属性设置(可以同时选择多个进行设置) UIViewAnimationOptionLayoutSubviews:执行UIView动画时,自动更新Subview的Layout约束.. UIView ...
- 前端vue 里的tab切换 减少dom操作
<div class="vuedemo"> <div class="all"> <div class="tabone&q ...
- 更新portage之后 安装 certbot
运行的时候一直报如下的错误: sudo certbot 错误结果: Traceback (most recent call last): File "/usr/lib/python-exec ...
- react事件代理
参考:https://github.com/youngwind/blog/issues/107 首先回顾以下原生事件的两个方法:event.stopImmediatePropagation 和 eve ...