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;
    }




  • 相关阅读:
    hadoop环境搭建之分布式的建立3
    hadoop环境搭建之分布式的建立2
    hadoop环境搭建之分布式的建立1
    安装Centos虚拟机
    VMWare安装虚拟机
    VMware虚拟机的下载及安装
    启用并登陆Administrator账户后显示“无法登陆到你的账户”
    Windows PowerShell打不开,无法重装.net framework
    中国软件杯赛题分析
    每周总结(10)
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226352.html
Copyright © 2011-2022 走看看