首页 > 代码库 > HDU 4960 (水dp)

HDU 4960 (水dp)

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 = 5000+10;
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 = 0;
    char c = getchar();
    while(c < 0 || c > 9) c = getchar();
    while(c >= 0 && c <= 9) n *= 10, n += (c - 0),c = getchar();
}
inline void rd(int &n){
    n = 0;
    char c = getchar();
    while(c < 0 || c > 9) c = getchar();
    while(c >= 0 && c <= 9) n *= 10, n += (c - 0),c = getchar();
}
int check(int L,int R,int d) {
    if(L+d==R) return 0;
    LL sum1=sum[L+d]-sum[L-1];
    LL sum2=0int id=0;
    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 -1;
}
int dfs(int L,int R) {
    if(L>=R) return 0;
    if(~dp[L][R]) return dp[L][R];
    int ans=inf; int d;
    for(int i=0;i<=(R-L);i++) {
        d=check(L,R,i);
        if(d!=-1) {
            ans=min(ans,dfs(L+i+1,R-d-1)+a[i+1]+a[d+1]);
        }
    }
    return dp[L][R]=ans;
}

int main() {
    while(scanf("%d",&n)==1&&n) {
        memset(sum,0,sizeof(sum));
        memset(dp,-1,sizeof(dp));
        for(int i=1;i<=n;i++) {
            rd(v[i]);
            sum[i]=sum[i-1]+v[i];
        }
        for(int i=1;i<=n;i++) {
            rd(a[i]);
        }
        int ans=dfs(1,n);
        printf("%d\n",ans);
    }
    return 0;

} 

 

随便改过的代码。

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

}