uva 10755

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1696

10755 - Garbage Heap

Time limit: 3.000 seconds

 

Garbage Heap

Time limit: 3.000 seconds

Memory limit: 64 megabytes

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

  It consists of a x b x 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 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 2 ^ 31by 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

 

 

 

  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

 

 

 

 

 

 

AC代码:

  1 // UVa10755 Garbage heap
  2 
  3 #include<cstdio>
  4 
  5 #include<cstring>
  6 
  7 #include<algorithm>
  8 
  9 #define FOR(i,s,t)  for(int i = (s); i <= (t); ++i)
 10 
 11 using namespace std;
 12 
 13  
 14 
 15 void expand(int i, int& b0, int& b1, int& b2) {
 16 
 17   b0 = i&1; i >>= 1;
 18 
 19   b1 = i&1; i >>= 1;
 20 
 21   b2 = i&1;
 22 
 23 }
 24 
 25  
 26 
 27 int sign(int b0, int b1, int b2) {
 28 
 29   return (b0 + b1 + b2) % 2 == 1 ? 1 : -1;
 30 
 31 }
 32 
 33  
 34 
 35 const int maxn = 30;
 36 
 37 const long long INF = 1LL << 60;
 38 
 39  
 40 
 41 long long S[maxn][maxn][maxn];
 42 
 43  
 44 
 45 long long sum(int x1, int x2, int y1, int y2, int z1, int z2) {
 46 
 47   int dx = x2-x1+1, dy = y2-y1+1, dz = z2-z1+1;
 48 
 49   long long s = 0;
 50 
 51   for(int i = 0; i < 8; i++) {
 52 
 53     int b0, b1, b2;
 54 
 55     expand(i, b0, b1, b2);
 56 
 57     s -= S[x2-b0*dx][y2-b1*dy][z2-b2*dz] * sign(b0, b1, b2);
 58 
 59   }
 60 
 61   return s;
 62 
 63 }
 64 
 65  
 66 
 67 int main() {
 68 
 69   int T;
 70 
 71   scanf("%d", &T);
 72 
 73   while(T--) {
 74 
 75     int a, b, c, b0, b1, b2;
 76 
 77     scanf("%d%d%d", &a, &b, &c);
 78 
 79     memset(S, 0, sizeof(S));
 80 
 81     FOR(x,1,a) FOR(y,1,b) FOR(z,1,c) scanf("%lld", &S[x][y][z]);
 82 
 83     FOR(x,1,a) FOR(y,1,b) FOR(z,1,c) FOR(i,1,7){
 84 
 85       expand(i, b0, b1, b2);
 86 
 87       S[x][y][z] += S[x-b0][y-b1][z-b2] * sign(b0, b1, b2);
 88 
 89     }
 90 
 91     long long ans = -INF;
 92 
 93     FOR(x1,1,a) FOR(x2,x1,a) FOR(y1,1,b) FOR(y2,y1,b) {
 94 
 95       long long M = 0;
 96 
 97       FOR(z,1,c) {
 98 
 99         long long s = sum(x1,x2,y1,y2,1,z);
100 
101         ans = max(ans, s - M);
102 
103         M = min(M, s);
104 
105       }
106 
107     }
108 
109     printf("%lld
", ans);
110 
111     if(T) printf("
");
112 
113   }
114 
115   return 0;
116 
117 }
118 
119  

 

 

原文地址:https://www.cnblogs.com/jeff-wgc/p/4479219.html