POJ 1050 to the Max

Source: Internet
Author: User

to the Max
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 45906 Accepted: 24276

Description

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 o R 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:

7 |
-4 1
-1 8
and has a sum of 15.

Input

The input consists of an n * 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].

Output

Output 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

Source

Greater New York 2001

Translation:
Total time limit:
1000ms
Memory Limit:
65536kB
Describe
The size of the known matrix is defined as the and of all elements in the matrix. Given a matrix, your task is to find the largest non-empty (size is at least 1 * 1) sub-matrices.

For example, the following matrix of 4 * 4

0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2

The maximum sub-matrix is

7 |
-4 1
-1 8

The size of this sub-matrix is 15.
Input
The input is a matrix of n * N. The first line of input gives N (0 < n <= 100). Then, in a few rows later, the n integers of the first row are given first from left to right, and then the n integers of the second row are given from left to right ... ) gives the N2 integers in the matrix, separated by whitespace characters (spaces or blank lines) between the integers. The range of integers in the known matrix is in [-127, 127].
Output
The size of the output maximum sub-matrix.
Sample input
40-2-7 0 9 2-6 2-4 1-4  1-18  0-2
Sample output
15

First Kind
/*prepare an array f[i,j] to save to i,j Matrix and (similar to prefix and) for a sub-matrix [x1,y1,x2,y2]//x1,x2 represents the upper left and bottom right corner of the horizontal axis has s[x1,y1,x2,y2] = f[x2,y2]-F[x1-1, Y2]-f[x2,y1-1] + f[x1,y1];*/#include<cstdio>#include<iostream>using namespacestd;#defineN 101intans=-0x7f, n,a[n][n],f[n][n];intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++)         for(intj=1; j<=n;j++) {scanf ("%d",&A[i][j]); F[I][J]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+A[i][j]; }     for(intI=1; i<=n;i++)         for(intj=1; j<=n;j++)             for(intk=0; k+i<=n;k++)                 for(intL=0; l+j<=n;l++){                    intxx=i+k,yy=j+l; Ans=max (ans,f[xx][yy]-f[i-1][yy]-f[xx][j-1]+f[i-1][j-1]); } printf ("%d\n", ans); return 0;}

The second Kind
#include <cstdio>#include<iostream>using namespacestd;#defineN 101inta[n][n],n,ans=0;intMain () {scanf ("%d",&N);  for(intI=1, x;i<=n;i++)         for(intj=1; j<=n;j++) scanf ("%d", &x), a[i][j]=a[i-1][j]+x; //Column prefixes and  for(intI=1; i<=n;i++)         for(intj=i;j<=n;j++){            inttmp=0;  for(intk=1; k<=n;k++){                intnum=a[j][k]-a[i-1][k]; if(tmp>0) tmp+=num; Elsetmp=num; Ans=Max (ans,tmp); }} printf ("%d\n", ans); return 0;}

POJ 1050 to 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.