HDU4960(SummerTrainingDay03-F dp)
Another OCD Patient
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1757 Accepted Submission(s): 600
Problem Description
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 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
Sample Input
6 2 8 7 1
0 5 2 10 20
0
Sample Output
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
//2017-08-03
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
int n, a[N], dp[N][N];//dp[l][r]表示把区间l-r合并为回文的最小代价
long long v[N], sum[N]; int dfs(int l, int r){
if(l >= r)return dp[l][r] = ;
if(dp[l][r] != -)return dp[l][r];//记忆化搜索
int i = l;
dp[l][r] = v[r-l+];
for(int j = r; j >= l; j--){
while((sum[i]-sum[l-]) < (sum[r]-sum[j-]) && i < j){
i++;
}
if(j == i)break;
if((sum[i]-sum[l-]) == (sum[r]-sum[j-])){//划分子区间,需保证区间左端所有数之和与区间右端所有数之和相等。
int tmp = dfs(i+, j-)+v[i-l+]+v[r-j+];
dp[l][r] = dp[l][r] < tmp ? dp[l][r] : tmp;
}
}
return dp[l][r];
} int main(){
while(scanf("%d", &n)!=EOF && n){
sum[] = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
sum[i] = sum[i-]+a[i];
}
for(int i = ; i <= n; i++)
scanf("%lld", &v[i]);
memset(dp, -, sizeof(dp));
int ans = dfs(, n);
printf("%d\n", ans);
} return ;
}
HDU4960(SummerTrainingDay03-F dp)的更多相关文章
- Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理
https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...
- Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希
https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...
- 【BZOJ4953】lydsy七月月赛 F DP
[BZOJ4953]lydsy七月月赛 F 题面 题解:设f[i][j]表示第i个强度取为j时的最小误差.那么每次转移时,我们只计算j'和j之间的像素点带来的误差,于是有: $f[i][j]=min( ...
- hdu 4389 X mod f(x) 数位DP
思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...
- HDU - 4389 X mod f(x)(数位dp)
http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...
- BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...
- F. Wi-Fi(线段树实现dp)
题:http://codeforces.com/contest/1216/problem/F dp[i][0]:表示第i个位置不装的最小代价 dp[i][1]:表示第i个位置装的最小代价 T1的线段树 ...
- UVALive 6908---Electric Bike(DP或记录型深搜)
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- coderforces #384 D Chloe and pleasant prizes(DP)
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- HDU4003Find Metal Mineral[树形DP 分组背包]
Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Other ...
随机推荐
- Python入门 ---基础知识
Python入门不知道这些你还是承早放弃吧!真的 Python 简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言 ...
- linux apache+php+mysql安装及乱码解决办法
1.乱码解决方法 首先确认mysql数据库字符集设置正确,php页面字符设置正确,之后修改apache配制文件http.conf 注释掉以下字符 AddDefaultCharset UTF-8 此为乱 ...
- spring-boot集成thymeleaf。
thymeleaf是前台页面展示,原来一直是jsp,jsp中包含很多服务器端的逻辑,逐渐淘汰.同样功能的还有freemarker.孰好孰坏不予评价,只做简单实现. 1.基本思路 (1)pom.xml中 ...
- [LeetCode]138复制带随机指针的链表
题目描述: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 思路: 先遍历链表,将每个节点对应的随机指针指向的对象利用Hash ...
- 解决ubuntu安装系统默认没有创建root用户
安装ubuntu操作系统的时候,默认没有设置root账户的步骤!!! 这样在操作系统安装完成之后,就没有root用户, 一般,当前的普通用户,如果需要执行一些命令的时候,只要在命令前边加上sudo就行 ...
- c++中double类型控制小数位数
有时,我们需要输出确定小数位数的double,可以先引入如下头文件: #include <iomanip> 然后通过下列方式输出: double zzz = 8.66666; cout & ...
- JAVA框架之Hibernate【配置文件详解】
Hibernate配置文件主要功能是配置数据库连接和Hibernate运行时所需的各种属性,配置文件应该位于JAVA应用或者JAVA Web应用的类文件中,刚开始接触Hibernate的时候,感觉Hi ...
- centos7-使用nginx做ftp站
关于nginx的安装可见: http://www.cnblogs.com/wenbronk/p/6557482.html 然后最简单的方式, 修改nginx的配置文件: server { listen ...
- myeclipse解决Fail to create the java Virtual Machine
今天在打开myeclipse的时候,就显示出 Fail to create the java Virtual Machine 这样的一个窗口出来. 解决的方案就是在myeclipse的安装目录下找到m ...
- Mouse单击高亮GridView数据行
有网友需要对GridView控件作一些操作.不过有些复杂,Insus.NET细分他的要求,一步一步来实现.不过细分的每一步,亦是一个小功能.因此Insus.NET就单独实现,然后一起结合起来,就是可以 ...