Hdu1081to the Max

Source: Internet
Author: User

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8992 Accepted Submission (s): 4359


Problem Descriptiongiven a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub- Array of size 1 x 1 or greater located within the whole array. 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.

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 corner:

9 2
-4 1
-1 8

and has a sum of 15.

Inputthe input consists of an n x n array of integers. The input begins with a single positive integer N in a line by itself, indicating the size of the square two-dimensional a Rray. This was followed by N 2 integers separated by whitespace (spaces and newlines). These is the N 2 integers of the array, presented in Row-major order. That's, all numbers on the first row, left-to-right, then all numbers-second row, left-to-right, etc. N may be as large as 100. The numbers in the array would be in the range [-127,127].

Outputoutput the sum of the maximal sub-rectangle.

Sample Input
40-2-7 0 9 2-6 2-4 1-4 1-18 0-2

Sample Output
15

Sourcegreater New York 2001


Maximum consecutive substrings and:

There is a string of s, with a temporary variable T to maintain a ans, if t>=0 is added s[i] may be better, so there is t+=s[i],
If t<0, then become s[i] may be better, so there is t=s[i]
If it is greater than ans each time, update can

Maximum sub-matrices and:

There is a matrix s, with a temporary array A to maintain an ans, each time A is recorded s continuous I row matrix and,
This can be used as the maximum continuous substring processing, each time the maximum value updated ans, you can

#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;int a[110],pic[110][110];int n;int sub () {int ans=int_min,t=0;for (int i=0;i <n;i++) {if (t>=0) T+=a[i];elset=a[i];ans=max (ans,t);} return ans;} int matrix () {int ans=int_min;for (int i=0;i<n;i++) {memset (a,0,sizeof (a)), for (int j=i;j<n;j++) {for (Int. k=0;k <n;k++) A[k]+=pic[j][k];ans=max (Ans,sub ());}} return ans;} int main () {while (cin>>n) {for (Int. i=0;i<n;i++) for (int j=0;j<n;j++) cin>>pic[i][j];cout<< Matrix () <<endl;}}


Hdu1081to the Max

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.