zoukankan      html  css  js  c++  java
  • UVa 10755 Garbage Heap 最大子块和 dp

    Garbage Heap

    Time limit: ? seconds
    Memory limit: 64 megabytes

    Farmer John has a heap of garbage formed in a rectangular parallelepiped.

    It consists of $A\times B\times C$ garbage pieces each of which has a value. The value of a piece may be 0, if the piece is neither profitable nor harmful, and may be negative which means that the piece is not just unprofitable, but even harmful (for environment).

    The farmer thinks that he has too much harmful garbage, so he wants to decrease the heap size, leaving a rectangular nonempty parallelepiped of smaller size cut of the original heap to maximize the sum of the values of the garbage pieces in it. You have to find the optimal parallelepiped value. (Actually, if any smaller parallelepiped has value less than the original one, the farmer will leave the original parallelepiped).

    Input

    The first line of the input contains the number of the test cases, which is at most 15. The descriptions of the test cases follow. The first line of a test case description contains three integers AB, and C (1 ≤ A, B, C ≤ 20). The next lines contain $A\cdot B\cdot C$ numbers, which are the values of garbage pieces. Each number does not exceed $2^{31}$ by absolute value. If we introduce coordinates in the parallelepiped such that the cell in one corner is (1,1,1) and the cell in the opposite corner is (A,B,C), then the values are listed in the order

    $$\begin{gathered}(1,1,1),(1,1,2),\dots,(1,1,C),\\(1,2,1),\dots,(1,2,C),\dots,(1,B,C),\\(2,1,1),\dots,(2,B,C),\dots,(A,B,C).\end{gathered}$$

    The test cases are separated by blank lines.

    Output

    For each test case in the input, output a single integer denoting the maximal value of the new garbage heap. Print a blank line between test cases.

    Examples

    Input Output
    1
    
    2 2 2
    -1 2 0 -3 -2 -1 1 5
    
    6
    

    ----------------------

    三维压二维

    二维压一维

    对一维进行dp

    long long

    ----------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn=32;
    const long long INF=(1LL<<62);
    int A,B,C;
    
    long long submax(long long num[maxn])
    {
        long long ans=num[1];
        long long f=num[1];
        for (int i=2;i<=C;i++)
        {
            f=max(num[i]+f,num[i]);
            ans=max(ans,f);
        }
        return ans;
    }
    
    long long submax2d(long long num[maxn][maxn])
    {
        long long ret,ans=-INF;
        long long a[maxn];
        for (int l=1;l<=B;l++)
        {
            memset(a,0,sizeof(a));
            for (int r=l;r<=B;r++)
            {
                for (int i=1;i<=C;i++)
                {
                    a[i]+=num[r][i];
                }
                ret=submax(a);
                if (ret>ans) ans=ret;
            }
        }
        return ans;
    }
    
    long long submax3d(long long num[maxn][maxn][maxn])
    {
        long long ret,ans=-INF;
        long long a[maxn][maxn];
        for (int l=1;l<=A;l++)
        {
            memset(a,0,sizeof(a));
            for (int r=l;r<=A;r++)
            {
                for (int i=1;i<=B;i++)
                {
                    for (int j=1;j<=C;j++)
                    {
                        a[i][j]+=num[r][i][j];
                    }
                }
                ret=submax2d(a);
                if (ret>ans) ans=ret;
            }
        }
        return ans;
    }
    
    int main()
    {
        int T;
        long long a[maxn][maxn][maxn];
        long long ans;
        //freopen("abc.in","r",stdin);
        //freopen("abc.out","w",stdout);
        scanf("%d",&T);
        while (T--)
        {
            memset(a,0,sizeof(a));
            scanf("%d%d%d",&A,&B,&C);
            for (int i=1;i<=A;i++)
            {
                for (int j=1;j<=B;j++)
                {
                    for (int k=1;k<=C;k++)
                    {
                        scanf("%lld",&a[i][j][k]);
                    }
                }
            }
            ans=submax3d(a);
            printf("%lld\n",ans);
            if (T) printf("\n");
        }
        return 0;
    }




  • 相关阅读:
    postman中 form-data、x-www-form-urlencoded、raw、binary的区别
    常见的http response
    ==容易错误的例子
    点击panel滚动条滚动到底部
    fabric实现文本聚焦、可编辑
    JSON.parse()和JSON.stringify()的使用
    centos7安装mysql
    阿里云第一次访问tomcat非常慢的解决办法
    java中的语法糖
    jvm | 基于栈的解释器执行过程
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226352.html
Copyright © 2011-2022 走看看