题意:有个长方体由A*B*C组成,每个废料都有一个价值,要选一个子长方体,使得价值最大。
析:我们暴力枚举上下左右边界,然后用前缀和来快速得到另一个,然后就能得到长方体,每次维护一个最小值,然后差就是最大值。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <unordered_map> #include <unordered_set> #define debug() puts("++++"); #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1LL << 60; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 20 + 5; const int mod = 2000; const int dr[] = {-1, 1, 0, 0}; const int dc[] = {0, 0, 1, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int A, B, C; LL sum[maxn][maxn][maxn]; void solve(int i){ A = i & 1; i >>= 1; B = i & 1; i >>= 1; C = i & 1; } inline int sign(){ return (A + B + C) & 1 ? 1 : -1; } LL getSum(int x1, int x2, int y1, int y2, int z1, int z2){ int dx = x2 - x1 + 1; int dy = y2 - y1 + 1; int dz = z2 - z1 + 1; LL ans = 0; for(int i = 0; i < 8; ++i){ solve(i); ans -= sum[x2-A*dx][y2-B*dy][z2-C*dz] * sign(); } return ans; } int main(){ int T; cin >> T; while(T--){ int a, b, c; scanf("%d %d %d", &a, &b, &c); memset(sum, 0, sizeof sum); for(int i = 1; i <= a; ++i) for(int j = 1; j <= b; ++j) for(int k = 1; k <= c; ++k) scanf("%lld", &sum[i][j][k]); for(int i = 1; i <= a; ++i) for(int j = 1; j <= b; ++j) for(int k = 1; k <= c; ++k) for(int x = 1; x < 8; ++x){ solve(x); sum[i][j][k] += sum[i-A][j-B][k-C] * sign(); } LL ans = -LNF; for(int x1 = 1; x1 <= a; ++x1) for(int x2 = x1; x2 <= a; ++x2) for(int y1 = 1; y1 <= b; ++y1) for(int y2 = y1; y2 <= b; ++y2){ LL mmin = 0; for(int z = 1; z <= c; ++z){ LL s = getSum(x1, x2, y1, y2, 1, z); ans = max(ans, s - mmin); mmin = min(mmin, s); } } printf("%lld\n", ans); if(T) putchar('\n'); } return 0; }