Uva108-maximum sum (maximum continuous sum)

Source: Internet
Author: User

Question:Returns the largest submatrix from a n * n matrix.


Ideas:Returns the maximum continuous sum. We can convert two-dimensional data into one-dimensional data for calculation. Sum [I] [J] indicates the sum of a matrix with (1, 1) AND (I, j) as diagonal. Then, you only need to enumerate each possible value one by one and save the maximum value..


# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int INF = 0x3f3f3f; const int maxn = 105; int arr [maxn] [maxn], sum [maxn] [maxn], s [maxn] [maxn]; int main () {int N; while (scanf ("% d", & N )! = EOF) {memset (S, 0, sizeof (s); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {scanf ("% d", & arr [I] [J]); s [I] [J] = s [I] [J-1] + arr [I] [J];} memset (sum, 0, sizeof (SUM )); for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) sum [I] [J] = sum [I-1] [J] + s [I] [J];} int max =-INF; For (INT I = 1; I <= N; I ++) {for (Int J = 0; j <I; j ++) {int T, min; min = 0; for (int K = 1; k <= N; k ++) {T = sum [I] [k]-sum [J] [k]-min; if (T> MAX) max = T; If (sum [I] [k]-sum [J] [k] <min) min = sum [I] [k]-sum [J] [k] ;}} printf ("% d \ n", max);} 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.