Another OCD Patient

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 645    Accepted Submission(s): 238

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

2014 Multi-University Training Contest 9

今天比赛的时候想到记忆化搜索,时间复杂度n^2logn,不过想想实际,也就n^2的复杂度,因为这个复杂度,怕被卡时间,所以

也没什么信心打,结果跟队友码别的题,码到最后才发现思路错了,囧

view code//按比赛是想法做的,有点可惜,时间800ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int INF = 1<<30;
const int N = 5005;
int n, dp[N][N];
ll sum[N], v[N],a[N];
bool vis[N][N]; int find(int l, int r, ll x)
{
int n = r;
while(l<=r)
{
int m = (l+r)>>1;
if(sum[n]-sum[m-1]==x) return m;
if(sum[n]-sum[m-1]>x) l = m+1;
else r = m-1;
}
return 0;
} int dfs(int l, int r)
{
if(l>=r) return 0;
if(vis[l][r]) return dp[l][r];
vis[l][r] = 1;
int &ans = dp[l][r];
ans = v[r-l+1];
for(int i=l; i<r; i++)
{
int R = find(i+1, r, sum[i]-sum[l-1]);
if(!R) continue;
ans = min((ll)ans, (ll)(dfs(i+1, R-1)+v[i-l+1]+v[r-R+1]));
}
return ans;
} void solve()
{
for(int i=1; i<=n; i++){
scanf("%I64d", a+i);
sum[i] = sum[i-1] + a[i];
}
for(int i=1; i<=n; i++) scanf("%I64d", v+i);
memset(vis, 0, sizeof(vis));
printf("%d\n", dfs(1,n));
} int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d", &n)>0 && n) solve();
return 0;
}

根据比赛是想法的优化,思想差不多,只不过优化了时间空间

view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 5010;
int L[N], R[N], cnt;
ll dp[N], sum[N], a[N], v[N], n;
bool vis[N]; int find(int l, int r, ll x)
{
int n = r;
while(l<=r)
{
int m = (l+r)>>1;
if(sum[n]-sum[m-1]==x) return m;
if(sum[n]-sum[m-1]>x) l=m+1;
else r = m-1;
}
return 0;
} void init()
{
cnt = 0;
for(int l=1; l<=n; l++)
{
int r = find(l+1, n, sum[l]);
if(!r) continue;
L[cnt] = l;
R[cnt++] = r;
}
} ll dfs(int pos, int l, int r)
{
if(l>=r) return 0;
if(dp[l]!=-1) return dp[l];
ll& ans=dp[l];
ans = v[r-l+1];
for(int i=pos; i<cnt; i++)
{
ans = min(dfs(i+1, L[i]+1,R[i]-1)+v[L[i]-l+1]+v[r-R[i]+1], ans);
}
return ans;
} void solve()
{
for(int i=1; i<=n; i++){
scanf("%I64d", &a[i]);
sum[i] = sum[i-1]+a[i];
}
for(int i=1; i<=n; i++) scanf("%I64d", v+i);
init();
memset(dp, -1, sizeof(dp));
printf("%I64d\n",dfs(0, 1, n));
} int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%I64d", &n)>0 && n) solve();
return 0;
}

hdu 4960 Another OCD Patient(dp)的更多相关文章

  1. HDU 4960 Another OCD Patient(记忆化搜索)

    HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...

  2. hdu 4960 Another OCD Patient (最短路 解法

    http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...

  3. HDU 4960 Another OCD Patient 简单DP

    思路: 因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和.根据这个性质,我们可以预处理出这个数列的对称点对.然后最后一个对称段是从哪里开始的,做n^2的DP就可以了. 代码: ...

  4. hdu 4960 记忆化搜索 DP

    Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  5. HDU_4960 2014多校9 Another OCD Patient DP

    其实现在想起来是个巨简单的DP,模型就跟LCS很像,比赛的时候居然没想出来,在聪哥提醒下还卡了个地方 就是说给定一串n个数字的序列,可以连续合并,最终使得序列是回文的,题目也给定了合并数字所需的代价, ...

  6. HDU4960Another OCD Patient(间隙dp,后座DP)

    Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  7. HDU 4960 (水dp)

    Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. Th ...

  8. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  9. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

随机推荐

  1. C#读取Excel文件:通过OleDb连接,把excel文件作为数据源来读取

    转载于:http://developer.51cto.com/art/200908/142392.htm C#读取Excel文件可以通过直接读取和OleDb连接,把excel文件作为数据源来读取:   ...

  2. 十大Intellij IDEA快捷键

    转载:http://blog.csdn.net/dc_726/article/details/42784275 Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow上也有一 ...

  3. JavaScript学习(1):基础

    这篇文章里,我们来聊一些JavaScript的基础知识. 1. 如何运行JavaScript? JavaScript是一种解释型的语言,它不需要提前编译.通常情况下,JavaScript会放在网页中, ...

  4. 学习HTML5之表单

    HTML5 的标准已经定了,应该火了,或者已经火了.那么是不是可以学习一下呢? 目前h5的主场还是在手机端,pc还是受困于浏览器的兼容,主要是IE在拖后腿.所以这里侧重的是手机里面的表现. 先来看看表 ...

  5. css知多少(8)——float上篇

    1. 引言 对于我们所有的web前端开发人员,float是或者曾经一度是你最熟悉的陌生人——你离不开它,却整天承受着它所带给你的各种痛苦,你以为它很简单就那么一点知识,但却驾驭不了它各种奇怪的现象. ...

  6. 图解javascript this指向什么?

    JavaScript 是一种脚本语言,支持函数式编程.闭包.基于原型的继承等高级功能.JavaScript一开始看起来感觉会很容易入门,但是随着使用的深入,你会发现JavaScript其实很难掌握,有 ...

  7. [js开源组件开发]数字或金额千分位格式化组件

    数字或金额千分位格式化组件 这次距离上一个组件<[js开源组件开发]table表格组件>时隔了一个月,由于最近的项目比较坑,刚挖完坑,所以来总结性提出来几个组件弥补这次的空缺,首先是金额和 ...

  8. Office 365 - SharePoint 2013 Online 中创建母版页

    1.登陆SharePoint Online站点,点击右上角的设置按钮,如下图: 2.点击进入网站设置,到下面两个地方开启SharePoint Server 发布基础架构: 网站集管理 – 网站集功能 ...

  9. [SQL] SQL Server 触发器

    触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...

  10. 如何处理 android 方法总数超过 65536 . the number of method references in a .dex file exceed 64k

    一:问题描述:     应用中的Dex 文件方法数超过了最大值65536的上限,简单来说,应用爆棚了. 二.解决方案:      方案1:使用插件化框架  比如: https://github.com ...