UVa 10755 Garbage Heap 解题报告(前缀和)

56 篇文章 0 订阅

10755 - Garbage Heap

Time limit: 3.000 seconds

Memory limit: 64 megabytes

Farmer John has a heap of garbage formed in a rectangularparallelepiped.

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

The farmer thinks that he has too much harmful garbage, sohe wants to decrease the heap size, leaving a rectangularnonempty parallelepiped of smaller size cut of the originalheap to maximize the sum of the values of the garbage piecesin it. You have to find the optimal parallelepiped value.(Actually, if any smaller parallelepiped has value less thanthe original one, the farmer will leave the originalparallelepiped).

Input

The first line of the input contains the number of thetest cases, which is at most 15. The descriptions of thetest cases follow.The first line of a test case description contains three integersA, B, and C (1 ≤ A, B, C ≤ 20). The next linescontain $A\cdot B\cdot C$ numbers, which are the values of garbagepieces. Each number does not exceed $2^{31}$ by absolutevalue. If we introduce coordinates in the parallelepipedsuch that the cell in one corner is (1,1,1) and the cellin the opposite corner is (A,B,C), then the values arelisted 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 newgarbage heap. Print a blank line between test cases.

Examples

InputOutput
1

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

    解题报告:《训练指南》上的例题。很有代表性的枚举前缀和题目。3维的代码,同时可以很方便的改成更多维度。

    代码如下,还是挺考验码力的。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <string>
using namespace std;

#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define mem(a) memset((a), 0, sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
void work();

int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
//    freopen("in.txt", "w", stdout);
#endif // ACM

    work();
}

/*****************************************/

LL box[22][22][22];

void expand(int n, int & b0, int & b1, int & b2)
{
    b0 = (n&1) ? 1 : 0;
    b1 = (n&2) ? 1 : 0;
    b2 = (n&4) ? 1 : 0;
}

int sign(int b0, int b1, int b2)
{
    return (b0^b1^b2) ? 1 : -1;
}

LL sum(int x1, int x2, int y1, int y2, int z1, int z2)
{
    int dx=x2-x1+1, dy=y2-y1+1, dz=z2-z1+1;

    LL s = 0;
    ff(i, 8)
    {
        int b0, b1, b2;
        expand(i, b0, b1, b2);
        s -= box[x2-b0*dx][y2-b1*dy][z2-b2*dz]*sign(b0, b1, b2);
    }
    return s;
}

void work()
{
    int T;
    scanf("%d", &T);
    ff(cas, T)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        fff(x, 1, a) fff(y, 1, b) fff(z, 1, c)
            scanf("%lld", &box[x][y][z]);

        int b0, b1, b2;
        fff(x, 1, a) fff(y, 1, b) fff(z, 1, c) fff(i, 1, 7)
        {
            expand(i, b0, b1, b2);
            box[x][y][z] += box[x-b0][y-b1][z-b2]*sign(b0, b1, b2);
        }

        LL ans = box[a][b][c];
        fff(x1, 1, a) fff(x2, x1, a)
            fff(y1, 1, b) fff(y2, y1, b)
        {
            LL M = 0;
            fff(z, 1, c)
            {
                LL s = sum(x1, x2, y1, y2, 1, z);
                ans = max(ans, s - M);
                M = min(M, s);
            }
        }

        printf("%lld\n", ans);
        if(cas != T-1) puts("");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值