Ultraviolet A 10755-garbage heap (three-dimensional maximum submatrix)

Source: Internet
Author: User

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 460 & page = show_problem & problem = 1696

The idea is the same as that of the two-dimensional largest submatrix, but it is changed to three-dimensional.

Enumerate the upper and lower bounds of the layers, and then "compress" all layers between the upper and lower bounds into one layer. On this layer, the two-dimensional method is used for calculation.

Use long

Code:

# Include <cstdio> # include <cstring> # include <iostream> # include <cctype> using namespace STD; typedef long int64; const int maxn = 25; int A, B, c; int64 cube [maxn] [maxn] [maxn]; inline int64 read_int64 () {char CH = getchar (); While (! Isdigit (CH) & Ch! = '-') CH = getchar (); bool neg = false; If (CH = '-') neg = true, CH = getchar (); int64 ans = 0; while (isdigit (CH) {ans = ans * 10 + CH-'0'; CH = getchar () ;}if (NEG) Return-ans; return ans ;} inline void input () {scanf ("% d", & A, & B, & C); memset (cube, 0, sizeof (cube )); for (int K = 1; k <= A; ++ K) {for (INT I = 1; I <= B; ++ I) {for (Int J = 1; j <= C; ++ J) {cube [k] [I] [J] = read_int64 (); cube [k] [I] [J] + = cube [k] [I] [J-1]-cube [k] [I-1] [J-1] + cube [k] [I-1] [J]-(cube [k-1] [I] [J-1]-cube [k-1] [I-1] [J-1] + cube [k-1] [I-1] [J]) + cube [k-1] [I] [J];} // column} // row} // layer} int64 solve () {// int64 ans =-2147483647-1; for (INT down = 0; down <A; ++ down) {for (INT up = down + 1; up <= A; ++ up) {// "row" on the enumeration layer for (int d = 0; D <B; ++ d) {for (INT u = d + 1; U <= B; ++ U) {int64 Minx = 0; For (int K = 1; k <= C; ++ K) {int64 sum = (cube [Up] [u] [k]-cube [Down] [u] [k]) -(cube [Up] [d] [k]-cube [Down] [d] [k]); ans = max (ANS, Sum-Minx ); if (sum <Minx) Minx = sum ;}}} return ans ;}int main () {int ncase; scanf ("% d", & ncase ); while (ncase --) {input (); cout <solve () <Endl; If (ncase) putchar ('\ n');} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.