Title Link: http://acm.swust.edu.cn/problem/589/
Time limit (ms): $ Memory Limit (KB): 65535 description tell you a good news, Wraith a few days ago to get a watermelon, but the box-shaped ....
Wraith found this watermelon long m cm, width n cm, high h cm. He found that if the watermelon is divided evenly into a small cube of m*n*h 1 cubic centimeters, then each small block will have a nutritional value (may be negative, because the watermelon may be broken, but the absolute value of not more than 200).
Now Wraith decided to cut the mm*nn*hh cubic centimeter of a small watermelon from the m*n*h cubic centimeter of watermelon (it must be a cube-shaped, long-width-high integer) and eat it. He wants to know how much nutrition he can get. (0 <= mm <= m,0 <= nn <= n,0 <= hh <= h.mm,nn,hh Value determined by you).
In other words, we want to find a three-dimensional sub-matrix from a m*n*h three-dimensional matrix, the right and maximum of this sub-matrix.
Input first line three number h,m,n (note order), respectively, the watermelon is high, long, wide.
In the following H section, each part is a matrix of m*n, and the number of K in section J of Part I indicates the nutritional value of the small cube of 1 cubic centimeters in the first layer of watermelon, section J of Row K.
1 <= H <= 32,1 <= m,n <= 50 to ensure maximum nutritional value of H <= m,n Outputwraith Sample Input
2 3 44 1 2 80 5-48 43 0 1 92 1 4 91 0 1 73 1 2 8 |
Sample Output
Problem Solving Ideas:
This problem, to find a maximum box, you need to compress the box into a matrix, and then compression into a line, the overall is three-dimensional, time efficiency t (n) =o (N5) The largest weighted rectangle we use SUM[I][J] to represent the first row of the J column of the sum, here we can and so on, with Sum[i][j][k] I layer, the first J row of the K-column and, here in a specific description then sum[i][j][k] = Sum[i][j-1][k] + sum[i][j][k-1]-sum[i][j-1][k-1] + x[i][j][k], you can And then we'll do it. (Upper SX and Nether Ex), enumerate columns (upper bound sy and Nether ey), and then enumerate high h
Now you need to compress them, compress them out as PTR, to illustrate (figure on the Internet, a bit of a problem, you can use painting pictures to see, Khan ~ ~ ~) Ptr=sum[i][ex][ey]-Sum[i][sx-1][ey]-sum[i][ex][sy-1 ] + sum[i][sx-1][sy-1]; then it becomes a linear problem orz~~~ code is as follows:
1#include <iostream>2#include <algorithm>3 using namespacestd;4 #defineMAXN 515 #defineINF 0x3f3f3f3f6 inth, M, N, ans;7 intSUM[MAXN][MAXN][MAXN], X[MAXN][MAXN][MAXN], DP[MAXN][MAXN][MAXN][MAXN];8 //sum of the weights of the former H-layer, the first I row, the column J, the DP array is compressed into two dimensions and the Sx,sy point to the Ex,ey point constitutes the plane rectangle maximum value .9 Ten intMain () { OneCin >> h >> m >>N; A for(inti =1; I <= H; i++) - for(intj =1; J <= M; J + +) - for(intK =1; K <= N; k++){ theCIN >>X[i][j][k]; -SUM[I][J][K] = sum[i][j-1][K] + sum[i][j][k-1]-Sum[i][j-1][k-1] +X[i][j][k]; - } - for(intSX =1; SX <= m; sx++) + for(intSY =1; Sy <= N; sy++) - for(intex = SX; Ex <= M; ex++) + for(intey = sy; EY <= N; ey++){ ADp[sx][sy][ex][ey] =-inf; at for(inti =1; I <= H; i++){ - //ptr compresses the matrix block weights and - intPTR = Sum[i][ex][ey]-SUM[I][SX-1][ey]-Sum[i][ex][sy-1] + SUM[I][SX-1][sy-1]; -Dp[sx][sy][ex][ey] = max (Dp[sx][sy][ex][ey] +ptr, ptr); -Ans =Max (Dp[sx][sy][ex][ey], ans); - } in } -cout << ans <<Endl; to return 0; +}
View Code
[Swust OJ 589]--Eat watermelon (three-dimensional matrix compression)