zoukankan      html  css  js  c++  java
  • 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

    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
     
     1 //2017-08-03
     2 #include <cstdio>
     3 #include <iostream>
     4 #include <cstring>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 const int N = 5010;
    10 const int inf = 0x3f3f3f3f;
    11 int n, a[N], dp[N][N];//dp[l][r]表示把区间l-r合并为回文的最小代价
    12 long long v[N], sum[N];
    13 
    14 int dfs(int l, int r){
    15     if(l >= r)return dp[l][r] = 0;
    16     if(dp[l][r] != -1)return dp[l][r];//记忆化搜索
    17     int i = l;
    18     dp[l][r] = v[r-l+1];
    19     for(int j = r; j >= l; j--){
    20         while((sum[i]-sum[l-1]) < (sum[r]-sum[j-1]) && i < j){
    21             i++;
    22         }
    23         if(j == i)break;
    24         if((sum[i]-sum[l-1]) == (sum[r]-sum[j-1])){//划分子区间,需保证区间左端所有数之和与区间右端所有数之和相等。
    25             int tmp = dfs(i+1, j-1)+v[i-l+1]+v[r-j+1];
    26             dp[l][r] = dp[l][r] < tmp ? dp[l][r] : tmp;
    27         }
    28     }
    29     return dp[l][r];
    30 }
    31 
    32 int main(){
    33     while(scanf("%d", &n)!=EOF && n){
    34         sum[0] = 0;
    35         for(int i = 1; i <= n; i++){
    36             scanf("%d", &a[i]);
    37             sum[i] = sum[i-1]+a[i];
    38         }
    39         for(int i = 1; i <= n; i++)
    40             scanf("%lld", &v[i]);
    41         memset(dp, -1, sizeof(dp));
    42         int ans = dfs(1, n);
    43         printf("%d
    ", ans);
    44     }
    45 
    46     return 0;
    47 }
  • 相关阅读:
    软件需求与分析课堂讨论一
    架构漫谈读后感
    Android Studio AVD中文输入法安装
    软件体系结构课后作业01
    寒假阅读笔记六
    寒假阅读笔记五
    寒假阅读笔记三
    寒假阅读笔记二
    寒假阅读笔记一
    mysql绿色版的安装
  • 原文地址:https://www.cnblogs.com/Penn000/p/7282075.html
Copyright © 2011-2022 走看看