Another OCD Patient

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

题意:给出一串数字,把这串数字合并成对称的串,合并连续的一段串有相应的花费,问最下花费是多少。

sl : 很水的dp,但是tle 好几发, 因为我是跳到了下一个状态还保留了当前的状态。但是想法还是对的。就是枚举两端相等的字段和。

这样就有转移方程 dp【i】【j】=min(dp【i+t】【j-x】 ,dp[i][j])  满足sigma(a[i] to a[i+t-1])==sigma(a[j-x+1] to a[j] ) .

开始傻比了的代码。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int MAX = +;
const int inf = 0x3f3f3f3f;
int dp[MAX][MAX]; LL sum[MAX];
int v[MAX],n,t[MAX],a[MAX];
inline void rdl(LL &n){
    n = ;
    char c = getchar();
    while(c < '' || c > '') c = getchar();
    while(c >= '' && c <= '') n *= , n += (c - ''),c = getchar();
}
inline void rd(int &n){
    n = ;
    char c = getchar();
    while(c < '' || c > '') c = getchar();
    while(c >= '' && c <= '') n *= , n += (c - ''),c = getchar();
}
int check(int L,int R,int d) {
    if(L+d==R) return ;
    LL sum1=sum[L+d]-sum[L-];
    LL sum2=; int id=;
    for(int i=R;i>L+d;i--) {
        sum2+=v[i];
        if(sum2>=sum1) {
            id=R-i;
            break;
        }
    }
    if(sum2==sum1) return id;
    else return -;
}
int dfs(int L,int R) {
    if(L>=R) return ;
    if(~dp[L][R]) return dp[L][R];
    int ans=inf; int d;
    for(int i=;i<=(R-L);i++) {
        d=check(L,R,i);
        if(d!=-) {
            ans=min(ans,dfs(L+i+,R-d-)+a[i+]+a[d+]);
        }
    }
    return dp[L][R]=ans;
} int main() {
    while(scanf("%d",&n)==&&n) {
        memset(sum,,sizeof(sum));
        memset(dp,-,sizeof(dp));
        for(int i=;i<=n;i++) {
            rd(v[i]);
            sum[i]=sum[i-]+v[i];
        }
        for(int i=;i<=n;i++) {
            rd(a[i]);
        }
        int ans=dfs(,n);
        printf("%d\n",ans);
    }
    return ;

}

随便改过的代码。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int MAX = +;
int dp[MAX][MAX],a[MAX],v[MAX];
LL sum[MAX];
int dfs(int L,int R) {
    if(L>=R) return ;
    if(~dp[L][R]) return dp[L][R];
    LL sum1,sum2; int ans=a[R-L+];
    for(int i=L,j=R;i<j;) {
        sum1=sum[i]-sum[L-];
        sum2=sum[R]-sum[j-];
        if(sum1==sum2) {
            ans=min(ans,dfs(i+,j-)+a[i-L+]+a[R-j+]);
            i++; j--;
        }
        else if(sum1>sum2) j--;
        else i++;
    }
    return dp[L][R]=ans;
}
int main() {
    int n;
    while(scanf("%d",&n)==&&n) {
        memset(sum,,sizeof(sum));
        for(int i=;i<=n;i++) {
            scanf("%d",&v[i]);
            sum[i]=sum[i-]+v[i];
        }
        for(int i=;i<=n;i++) {
            scanf("%d",&a[i]);
        }
        memset(dp,-,sizeof(dp));
        int ans=dfs(,n);
        printf("%d\n",ans);
    }

}

HDU 4960 (水dp)的更多相关文章

  1. Tickets HDU - 1260 水DP

    HDU - 1260 现在有n个人要买电影票,如果知道每个人单独买票花费的时间, 还有和前一个人一起买花费的时间,问最少花多长时间可以全部买完票. 直接dp就行,注意下输出和初始化 每次从dp[i-1 ...

  2. HDU 4968 (水dp 其他?)

    +;],dp1[][],dp2[][]; map<      memset(GPA,,     ;i<=;i++) hash[i]=;     ;i<=;i++) hash[i]=; ...

  3. HDU 2084 数塔 (水DP)

    题意:.... 析:从下往上算即可,水DP. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...

  4. hdu 2571 命运(水DP)

    题意: M*N的grid,每个格上有一个整数. 小明从左上角(1,1)打算走到右下角(M,N). 每次可以向下走一格,或向右走一格,或向右走到当前所在列的倍数的列的位置上.即:若当前位置是(i,j), ...

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

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

  6. CodeForces 706C Hard problem (水DP)

    题意:对于给定的n个字符串,可以花费a[i]  将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...

  7. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  8. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  9. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...

  10. 水dp第二天(背包有关)

    水dp第二天(背包有关) 标签: dp poj_3624 题意:裸的01背包 注意:这种题要注意两个问题,一个是要看清楚数组要开的范围大小,然后考虑需要空间优化吗,还有事用int还是long long ...

随机推荐

  1. Wannafly挑战赛10:A题:小H和迷宫

    题目描述       小H陷入了一个迷宫中,迷宫里有一个可怕的怪兽,血量有N点,小H有三瓶魔法药水,分别可以使怪兽损失a%.b%.c%的血量(之后怪兽的血量会向下取整),小H想合理地运用这三瓶药水,使 ...

  2. java中什么包不需要导入

    java中Math.random()*10;在math包中不需要导入: 即import java.lang.Math; 即lang下的所有包都不需要导入.

  3. 在Windows上部署Zabbix客户端

    将Zabbix for Windows客户端拷贝到windows系统的c盘,修改配置文件的相关配置项后,打开cmd窗口执行: # 安装服务 c:\zabbix\bin\win32\zabbix_age ...

  4. C. Unfair Poll 数学题,

    http://codeforces.com/contest/758/problem/C 需要一个能够找到任意一个位置的步数的方法,就能解决三个问题. 预处理出one(row, col)表示第一次经过这 ...

  5. h5学习-css3的一些内容整理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 波哥!一个不安分的IT男

    第一篇博文,紧张,窃喜,辣眼睛! 这个订阅号主要是写给自己的,近期越来越发现记忆力不如以前了! 时光如梭,岁月荏苒,或许这两句经典的开头文比较契合自己的年纪.依稀记得几年前还在组装服务器.搬机柜.做系 ...

  7. jmeter 连接 mysql 进行压力测试

  8. c#中out参数的作用

    给你个简单的解释说法吧.虽然不完全对.但是我可以让你理解OUT有什么作用.呵呵 举个例子.每个方法只能有一个返回值.但是你想有多个返回值,呵呵.OUT就起作用了啊.比如分页,不光返回数据,还要返回总记 ...

  9. Farseer.net轻量级开源框架说明及链接索引

    Farseer.net是什么? 基于.net framework 4 开发的一系列解决方案. 完全开源在GitHub中托管.并发布到NuGet中. Farseer.Net由最初的关系数据库ORM框架后 ...

  10. PostgreSQL 数据库错误状态编号解释[附带列表

    PostgreSQL 服务器发出的所有消息都赋予了五个字符的错误代码, 这些代码遵循 SQL 的 "SQLSTATE" 代码的习惯.需要知道发生了什么错误条件的应用通常应该测试错误 ...