zoukankan      html  css  js  c++  java
  • HDU4960Another OCD Patient(间隙dp,后座DP)

    Another OCD Patient

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


    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
    题意:给出n个数,把这n个数合成一个对称的集合。第三行代表一次合成i个数须要花费a[i],求出最小的花费。
    解题:先把n个数合成一个对称集合共k个块,再进行对称区间DP。

    #include<stdio.h>
    __int64 v[5005],a[5005],pre[5005],dp[5005];
    void dfs(int l,int r)//分块,使[l,r]对称
    {
        int i=l,j=r;
        __int64 suml=v[l],sumr=v[r];
        while(i<j)
        {
            if(suml==sumr)
            {
                if(i+1<=j-1)dfs(i+1,j-1);
                pre[l]=i; pre[j]=r;//区间最左最右块和相等
                 return ;
            }
            if(suml<sumr)suml+=v[++i];
            else sumr+=v[--j];
        }
        pre[l]=r;//一个区间仅仅能合成一块
    }
    void count(int k)//分成k个块后。从最中间块向两边扩大范围进行DP,pre[i]表示从第一块到
    {
        int l, r,m;
        if(k%2)
            {
                l=k/2; r=k/2+2; m=k/2+1;//m是最中间块
                dp[m]=a[pre[m]-pre[m-1]];
            }
        else
            {
                l=k/2; r=k/2+1; m=l; dp[l]=0;
            }
        while(r<=k)
            {
                dp[r]=a[pre[r]-pre[l-1]];//合成一大块时
                for(int tr=r,tl=l;m<tr;tr--,tl++)//在区间块找出相应的最小dp[r]
                if(dp[r]>dp[tr-1]+a[pre[r]-pre[tr-1]]+a[pre[tl]-pre[l-1]])
                dp[r]=dp[tr-1]+a[pre[r]-pre[tr-1]]+a[pre[tl]-pre[l-1]];
                l--;r++;//向两边扩增
            }
    }
    int main()
    {
        int n,i,k;
        __int64 tk;
        while(scanf("%d",&n)>0&&n)
        {
            for( i=1;i<=n;i++)scanf("%I64d",&v[i]);
            for( i=1;i<=n;i++)scanf("%I64d",&a[i]);
            dfs(1,n);
            k=0; i=1;pre[0]=0;
            while(i<=n)//把一整块缩成一个点,pre[k]变成前k个块共同拥有多少个数组成k块
            {
                tk=pre[i]-i+1; i=pre[i]+1; ++k; pre[k]=tk+pre[k-1];
            }
            count(k);
            printf("%I64d
    ",dp[k]);
        }
    }
    

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    一个配置引发的血案
    软件工程之学习方法篇
    开篇
    重拾《 两周自制脚本语言 》- 中文关键字与原生函数
    重拾《 两周自制脚本语言 》- 支持中文标识符
    将《 两周自制脚本语言 》测试中使用的接口中文化
    5分钟入门LingaScript-尝鲜中文版TypeScript
    为《 两周自制脚本语言 》添加中文测试代码
    2019-02-18 扩展Python控制台实现中文反馈信息之二-正则替换
    2019-02-14 1992年日本对母语编程的可读性比较实验
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4850854.html
Copyright © 2011-2022 走看看