HDU 1081 to the Max (dp+ two-dimensional one-dimensional)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1081

to the Max

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


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:

7 |
-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 Input40-2-7 0 9 2-6 2-4 1-4 1-18 0-2

Sample Output15 topic: Find a matrix inside a number and the largest matrix. The concept of the topic: the two-dimensional one-dimensional, converted into 1003 can. First, one column and one column, and this translates into one dimension. For example, I first calculate the first line to get dp[0]=0dp[1]=-2,dp[2]=-7,dp[3]=0, these are called and, and then in the 1003 method. Then the second line, will be updated, dp[0]=9,dp[1]=0,dp[2]=-13,dp[3]=2; these are still and, this is converted into a one-dimensional array, with 1003 of the method to solve, and so on ~ ~ ~ See the code.
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intdp[ the];8 intnum[ the][ the];9 Ten intMain () One { A     intN; -      while(~SCANF ("%d",&N)) -     { the          for(intI=0; i<n; i++) -              for(intj=0; j<n; J + +) -scanf"%d",&num[i][j]); -         intans=-99999; +          for(intI=0; i<n; i++) -         { +Memset (DP,0,sizeof(DP)); A              for(intJ=i; j<n; J + +) at             { -                 intmax=-1; -                  for(intk=0; k<n; k++) -                 { -DP[K]+=NUM[J][K];//calculate all the DP and -                 } in                  for(intk=0; k<n; k++)//1003 approach, code similar -                 { to                     if(max+dp[k]<Dp[k]) +max=Dp[k]; -                     Else themax=max+Dp[k]; *                     if(Ans<max)//constantly updating the maximum value $                     {Panax Notoginsengans=Max; -                         //cout<<j<< "" <<k<<endl; the                     } +                 } A             } the         } +printf ("%d\n", ans); -     } $     return 0; $}

HDU 1081 to the Max (dp+ two-dimensional one-dimensional)

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.