To The Max

To The Max

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 7
Problem Description
Given 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 all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is 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.
 

Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will 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
 
题目大意:
第一行输入一个数字N,然后构建N*N大小,且带有数值的矩阵。求出在这大矩阵中的小矩阵,要求数值和最大的。
差不多是第一题目的升级版吧,需要把为二维的思想扩展到二维上。所以,我设置了一个Now[]数值来实现维度的转变。
Now[]是用来存储每次从上到下,依次累加纵行的值,把二维转化成一维,然后再从Now[]中寻找连续和最大的值,既为相关区域中矩阵的和的最大值。依次寻找大矩阵中的小矩阵,求得最大数值和。
算法复杂度:
                        O(n^3)(大概吧...)
 
状态转移方程:
                        Now[i]=NUM[i][1]+NUM[i][2]+NUM[i][3]+.......+NUM[i][N];(i=1,2,3,4,.....N)
                        Max_S=Max(Sign,Sign+Now[i])
                        Max=Max(Max,Max_S)
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <string.h>
 4 int main()
 5 {
 6     int i,j,N,Max,sum,k,Sign,ii,Max_s;
 7     int NUM[105][105];          /*NUM[]是用来保存存入的大矩阵数值的数据*/
 8     int Now[105];                /*Now[]是用来实现维度的扩展,用来存储纵行的累加值*/   
 9     while(scanf("%d",&N)!=EOF)      /*输入矩阵大小*/
10     {
11         for(i=1;i<=N;i++)
12             for(j=1;j<=N;j++)
13                 scanf("%d",&NUM[i][j]);     /*输入N*N大小的矩阵的数值*/
14         for(i=1,Max=0;i<=N;i++)             /*i为Now[]的起始位置,从上到下,需要初始化Now[]N次操作*/
15         {
16             memset(Now,0,sizeof(Now));      /*每次从新的一行重新累加,需要重新初始化Now[]*/
17             for(j=i;j<=N;j++)
18             {
19                 for(k=1;k<=N;k++)
20                     Now[k]+=NUM[j][k];      /*获取新的一行中的累加值*/
21                 for(ii=1,Max_s=0,Sign=0; ii<=N; ii++)
22                 {
23                     if(Sign>0)              
24                         Sign+=Now[ii];
25                     else
26                         Sign=Now[ii];
27                     if(Max_s<Sign)      /*寻找Now中连续的最大值,既为某一区域的矩阵中的最大值,*/                
28                         Max_s=Sign;     /*把每次找到的最大值存储在Mas_S中, */
29                 }
30                 if(Max<Max_s)           /*把本趟循环中的最大值与从开始到之前的最大值做比较,看是否替换最大值*/
31                     Max=Max_s;
32             }
33         }
34         printf("%d\n", Max);        /*全部循环结束输入,输出存储的最大值,即为大矩阵区域中,数值和最大的小矩阵*/
35     }
36     return 0;
37 }
View Code

 

posted @ 2014-08-06 13:15  Wurq  阅读(141)  评论(0)    收藏  举报