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 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 numbers, which are the values of garbagepieces. Each number does not exceed 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
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
Input | Output |
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("");
}
}