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 ...
随机推荐
- 关于bootstrap栅格系统的五等分以及八等分代码
众所周知,bootstrap的栅格系统是基于十二等分的,今天拿到设计的设计稿一看,发现一个图片list上只有8张图片,然后上网查资料,发现只能自己写css代码实现,故写博客记录代码. 以下是八等分的代 ...
- Asp.Net Core 进阶(一) —— 读取appsettings.json
我们以前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 来读取web.config文件.但是Asp.Net Core MVC已经没有 ...
- numpy.random.randint
low.high.size三个参数.默认high是None,如果只有low,那范围就是[0,low).如果有high,范围就是[low,high). >>> np.random.ra ...
- 小白安装python软件
首先下载:anaconda3.x 下载方式:百度搜索 清华镜像anaconda https://mirrors.tuna.tsinghua.edu.cn/help/anacond ...
- NYOJ-1057-寻找最大数(三)
http://acm.nyist.net/JudgeOnline/problem.php?pid=1057 寻找最大数(三) 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描 ...
- #include <> 和 #inlude ""的区别
#include < >引用的是编译器的类库路径里面的头文件#include " "引用的是你程序目录的相对路径中的头文件,在程序目录的相对路径中找不到该头文件时会继 ...
- java在线聊天项目1.2版 ——开启多个客户端,分别实现数据库注册和登录功能后,成功登陆则登录框消失,好友列表窗出现
登录框消失语句 dispose(); 好友列表窗出现 使用new FriendsFrame(phone,s); 登陆对话框代码修改如下: package com.swift.frame; import ...
- XAMPP虚拟主机配置--20150423
你需要一些顶级域名访问方式来访问你本地的项目文件而不是目录方式访问,这时候就需要配置虚拟主机,给你的目录绑定一个域名(本地的话可以通过修改 hosts 文件随便绑定什么域名比如 www.a.com 或 ...
- AT2172 Shik and Travel
题目描述: luogu 题解: 二分+暴力$vector$+$dfs$. 记录下所有可能的子树内合法方案,双指针+归并合并. 代码: #include<vector> #include&l ...
- PAT 乙级 1011
题目 题目地址:PAT 乙级 1011 思路 这道题的比较坑的地方在于给定数据的范围 int 类型的数据大小是[-2^31 , 2^31 -1] 即 [-2147483648,2147483647] ...