UVA 10755 10755 - Garbage Heap(DP, s)

Farmer John has a heap of garbage formed in a rectangular parallelepiped.
It consists of A×B ×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 all 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 A, B, and C
(1 ≤ A, B, C ≤ 20). The next lines contain A · B · C numbers, which are the values of garbage pieces.
Each number does not exceed 231 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
(1, 1, 1),(1, 1, 2), . . . ,(1, 1, C),
(1, 2, 1), . . . ,(1, 2, C), . . . ,(1, B, C),
(2, 1, 1), . . . ,(2, B, C), . . . ,(A, B, C).
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.
Sample Input
1
2 2 2

-1 2 0 -3 -2 -1 1 5

16291403 10755 Garbage Heap Accepted C++ 0.026 2015-10-19 08:32:45

ac代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f3f3f3f  
ll a[22][22][22],sum[22][22][22][22],res[22][22][22][22];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int i,j,k,x,y,z,c,l;
        scanf("%d%d%d",&x,&y,&z);
        for(i=1;i<=x;i++)
            for(j=1;j<=y;j++)
                for(k=1;k<=z;k++)
                    scanf("%lld",&a[i][j][k]);
        ll ans=-INF;
        for(c=1;c<=x;c++)
            for(i=1;i<=y;i++)
                for(j=i;j<=y;j++)
                    for(k=1;k<=z;k++)
                    {
                        ll h=0;
                        for(l=k;l<=z;l++)
                        {
                            h+=a[c][j][l];
                            sum[i][j][k][l]=sum[i][j-1][k][l]+h;
                            if(c==1)
                                res[i][j][k][l]=sum[i][j][k][l];
                            else
                                res[i][j][k][l]=max(sum[i][j][k][l],res[i][j][k][l]+sum[i][j][k][l]);
                            ans=max(ans,res[i][j][k][l]);
                        }
                    }
        printf("%lld\n",ans);
        if(t)
            printf("\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值