Ural 1146. Maximum Sum

Source: Internet
Author: User

1146. Maximum Sumtime limit:0.5 Second
Memory limit:64 Mbgiven A 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest Sum. The sum of a rectangle is the sum of the "the elements in" that rectangle. The problem the sub-rectangle with the largest sum are referred to as the Maximal sub-rectangle. A Sub-rectangle is any contiguous sub-array of size 1x1 or greater located within the whole array. As an example, the maximal sub-rectangle of the array:
0 −2 −7 0
9 2 −6 2
−4 1 −4 1
−1 8 0 −2
is in the Lower-left-hand corner and have the sum of 15.InputThe input consists of an NX NArray of integers. The input begins with a single positive integer NOn a line by itself indicating the size of the square and the dimensional array. This was followed by N2 integers separated by white-space (newlines and spaces). These N2 integers make to the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on th e second row, left-to-right, etc.). NMay is as large as 100. The numbers in the array would be in the range [−127, 127]. Outputthe output is the sum of the maximal sub-rectangle. Sample
input Output
40-2-7 09 2-6 2-4 1-4 1-1 8 0-2
15
Tags:Dynamic programming()difficulty:97 Test Instructions: The most important sub-matrix analysis: very classical problem. Know the maximum number of sub-segments and practices. Then enumerate the upper and lower bounds of the matrix, according to the maximum number of sub-segments and practices.
1 /**2 Create by Yzx-stupidboy3 */4#include <cstdio>5#include <cstring>6#include <cstdlib>7#include <cmath>8#include <deque>9#include <vector>Ten#include <queue> One#include <iostream> A#include <algorithm> -#include <map> -#include <Set> the#include <ctime> -#include <iomanip> - using namespacestd; -typedefLong LongLL; +typedefDoubleDB; - #defineMIT (2147483647) + #defineINF (1000000001) A #defineMLL (1000000000000000001LL) at #defineSZ (x) ((int) (x). Size ()) - #defineCLR (x, y) memset (x, y, sizeof (x)) - #definePUF Push_front - #definePub push_back - #definePOF Pop_front - #definePOB pop_back in #defineFT first - #defineSD Second to #defineMk Make_pair +  -InlineintGetint () the { *     intRet =0; $     CharCh =' ';Panax Notoginseng     BOOLFlag =0; -      while(! (Ch >='0'&& Ch <='9')) the     { +         if(Ch = ='-') Flag ^=1; ACh =GetChar (); the     } +      while(Ch >='0'&& Ch <='9') -     { $RET = RET *Ten+ Ch-'0'; $Ch =GetChar (); -     } -     returnFlag? -Ret:ret; the } - Wuyi Const intN = the; the intN, Arr[n][n]; - intSum[n][n], p[n]; Wu  -InlinevoidInput () About { $scanf"%d", &n); -      for(inti =1; I <= N; i++) -          for(intj =1; J <= N; J + +) scanf ("%d", &arr[i][j]); - } A  +InlineintWork (int*arr) the { -     intRET = arr[1], cnt = arr[1]; $      for(inti =2; I <= N; i++) the     { the         if(CNT <0) CNT =Arr[i]; the         ElseCNT + =Arr[i]; theRET =Max (ret, CNT); -     } in     returnret; the } the  AboutInlinevoidSolve () the { the     intAns =-INF; the      for(inti =1; I <= N; i++) +          for(intj =1; J <= N; J + +) -SUM[I][J] = sum[i-1][J] +Arr[i][j]; the      for(inti =1; I <= N; i++)Bayi          for(intj = i; J <= N; J + +) the         { the              for(intK =1; K <= N; k++) -P[K] = sum[j][k]-sum[i-1][k]; -             intCNT =Work (p); the             /*if (ans < CNT) the             { the ans = cnt; the printf ("%d%d%d\n", ans, I, j); -             }*/ theAns =max (ans, CNT); the         } the 94cout << ans <<Endl; the } the  the intMain ()98 { AboutFreopen ("a.in","R", stdin); - Input ();101 Solve ();102     return 0;103}
View Code

Ural 1146. Maximum Sum

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.