码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4960 Another OCD Patient(区间dp记忆化搜索)

时间:2014-08-26 21:29:36      阅读:380      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   java   io   strong   

题目大意:给你一串数字让你判断经过若干次合并,使得这个数字串变成回文串的最小成本是多少。第一行是数字串,第二行是合并连续i个数字的成本是多少。

解题思路:区间dp,可以进行记忆化搜索,如果左边比右边和大那么右边一定是小了,右边比左边大那么左边一定小了。因为保证有解。具体不太好说,直接看代码吧。

Another OCD Patient

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


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
 
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;

const int maxn = 5010;

int dp[maxn][maxn];
LL sum[maxn];
int num[maxn];

int dfs(int l, int r)
{
    if(l > r) return 0;
    if(dp[l][r] != -1) return dp[l][r];
    dp[l][r] = num[r-l+1];
    int ll = l, rr = r;
    while(ll < rr)
    {
        if((sum[ll]-sum[l-1]) > (sum[r]-sum[rr-1])) rr--;
        else if((sum[ll]-sum[l-1]) < (sum[r]-sum[rr-1])) ll++;
        else if((sum[ll]-sum[l-1]) == (sum[r]-sum[rr-1]))
        {
            dp[l][r] = min(dp[l][r], num[ll-l+1]+dfs(ll+1, rr-1)+num[r-rr+1]);
            rr--;
        }
    }
    return dp[l][r];
}

int main()
{
    int n;
    while(~scanf("%d",&n) && n)
    {
        sum[0] = 0;
        for(int i = 0; i <= n; i++)
            for(int j = 0; j <= n; j++) dp[i][j] = -1;
        LL x;
        for(int i = 1; i <= n; i++)
        {
            scanf("%I64d",&x);
            sum[i] = sum[i-1]+x;
        }
        for(int i = 1; i <= n; i++) scanf("%d",&num[i]);
        cout<<dfs(1, n)<<endl;
    }
    return 0;
}


HDU 4960 Another OCD Patient(区间dp记忆化搜索)

标签:des   style   blog   http   color   os   java   io   strong   

原文地址:http://blog.csdn.net/xu12110501127/article/details/38853273

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!