UVA 10755 - Garbage Heap(三维子矩阵最大和)
来源:互联网 发布:星际淘宝网txt下载棉花 编辑:程序博客网 时间:2024/06/12 01:42
Garbage Heap
Farmer John has a heap of garbage formed in a rectangular parallelepiped.
It consists of 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 numbers, which are the values of garbage pieces. Each number does not exceed 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
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
InputOutput12 2 2-1 2 0 -3 -2 -1 1 5
6
题意:说白了求三维子矩阵最大和。
思路:利用前缀和的思想求出2维每个子矩阵和,剩下就等同于求一维子序列最大和的方法一样了。
代码:
#include <stdio.h>#include <string.h>#define max(a, b) (a)>(b)?(a):(b)#define INF 0x3f3f3f3f3f3f3fconst int N = 25;int t, A, B, C;long long g[N][N][N], sum[N][N][N][N], res[N][N][N][N];void init() {scanf("%d%d%d", &A, &B, &C);for (int i = 1; i <= A; i++)for (int j = 1; j <= B; j++)for (int k = 1; k <= C; k++)scanf("%lld", &g[i][j][k]);}long long solve() {long long ans = -INF;for (int c = 1; c <= A; c++) {for (int i = 1; i <= B; i++) {for (int j = i; j <= B; j++) {for (int k = 1; k <= C; k++) {long long h = 0;for (int l = k; l <= C; l++) {h += g[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]);}}}}}return ans;}int main() {scanf("%d", &t);while (t--) {init();printf("%lld\n", solve());if (t) printf("\n");}return 0;}
- UVA 10755 - Garbage Heap(三维子矩阵最大和)
- UVA-10755 Garbage Heap 三维子矩阵最大和
- UVA 10755 Garbage Heap 三维最大子矩阵和 -
- Uva 10755 Garbage Heap(三维最大子矩阵)
- uva 10755 - Garbage Heap(三维最大子矩阵)
- UVA - 10755 Garbage Heap(最大子矩阵)
- UVa 10755 - Garbage Heap 三维最大子矩阵问题转化为1维..
- 【UVa】10755 Garbage Heap 三维前缀和
- UVa 10755 - Garbage Heap 最大子块和 dp
- UVA 10755 Garbage Heap 最大子立方体和
- uva 10755 - Garbage Heap(最大和问题)
- UVa 10755 Garbage Heap (最大价值子立方体+扫描法)
- uva10755 - Garbage Heap (最大子立方体)
- 【降维解法:最大字段和->最大子矩阵和->最终版最大子长方体和】【UVA10755】Garbage Heap
- UVA 10755 Garbage Heap
- UVa:10755 Garbage Heap
- UVA 10755 Garbage Heap
- UVA - 10755 Garbage Heap
- 完成端口在MSDN中的专题翻译
- hive的几种文件格式
- 《Unity3D大风暴之入门篇(海量教学视频版)》
- 因为前缀索引问题导致mysql无法使用覆盖索引
- GetKeyState和GetAsyncKeyState的区别
- UVA 10755 - Garbage Heap(三维子矩阵最大和)
- cocos2d-x游戏发布交叉编译成安卓apk(一)环境搭建
- hdu 1087 Super Jumping! Jumping! Jumping!(LIS)
- PDP附着和PDP激活的区别
- POJ 1837 Balance (DP)
- hdu 4691 Front compression(后缀数组)
- unbunt系统安装Nginx
- 一个简单的计时器类
- Auto Refresh Plus – 页面自动刷新[Chrome 扩展]