POJ 1050 To the Max (动规)
2014-07-25 16:55
225 查看
To the Max
Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*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 * 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
Sample Output
Source
Greater New York 2001
在一维情况下最大连续子段和的求法是从左到有顺序扫描数据,以0为边界,当累加和小于0时则重置为0.动态规划的状态转移方程为
s=max{si-1+ai,ai},该方程和前面的描述是等价的。本题是对一维最大子段和的扩展,思路是从上到下找出所有的连续行(如第i行到第j行),然后计算每列从第i行到第j行的和,之后对这n个列的和进行一维最大子段和的计算,并找出最大的值。
最大子矩阵,首先一行数列很简单求最大的子和,我们要把矩阵转化成一行数列,就是从上向下在输入的时候取和,map[i][j]表示在J列从上向下的数和,这样就把一列转化成了一个点,再用双重,循环,任意i行j列开始的一排数的最大和,就是最终的最大和
假设最大子矩阵的结果为从第r行到k行、从第i列到j列的子矩阵,如下所示(ari表示a[r][i],假设数组下标从1开始):
| a11 …… a1i ……a1j ……a1n |
| a21 …… a2i ……a2j ……a2n |
| . . . . . . . |
| . . . . . . . |
| ar1 …… ari ……arj ……arn |
| . . . . . . . |
| . . . . . . . |
| ak1 …… aki ……akj ……akn |
| . . . . . . . |
| an1 …… ani ……anj ……ann |
那么我们将从第r行到第k行的每一行中相同列的加起来,可以得到一个一维数组如下:
(ar1+……+ak1, ar2+……+ak2, ……,arn+……+akn)
由此我们可以看出最后所求的就是此一维数组的最大子断和问题,到此我们已经将问题转化为上面的已经解决了的问题了。
代码:
else if(sum>max) max=sum;
}
}
printf("%d\n",max);
return 0;
}[/code]
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 40302 | Accepted: 21329 |
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*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 * 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
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
Sample Output
15
Source
Greater New York 2001
在一维情况下最大连续子段和的求法是从左到有顺序扫描数据,以0为边界,当累加和小于0时则重置为0.动态规划的状态转移方程为
s=max{si-1+ai,ai},该方程和前面的描述是等价的。本题是对一维最大子段和的扩展,思路是从上到下找出所有的连续行(如第i行到第j行),然后计算每列从第i行到第j行的和,之后对这n个列的和进行一维最大子段和的计算,并找出最大的值。
最大子矩阵,首先一行数列很简单求最大的子和,我们要把矩阵转化成一行数列,就是从上向下在输入的时候取和,map[i][j]表示在J列从上向下的数和,这样就把一列转化成了一个点,再用双重,循环,任意i行j列开始的一排数的最大和,就是最终的最大和
假设最大子矩阵的结果为从第r行到k行、从第i列到j列的子矩阵,如下所示(ari表示a[r][i],假设数组下标从1开始):
| a11 …… a1i ……a1j ……a1n |
| a21 …… a2i ……a2j ……a2n |
| . . . . . . . |
| . . . . . . . |
| ar1 …… ari ……arj ……arn |
| . . . . . . . |
| . . . . . . . |
| ak1 …… aki ……akj ……akn |
| . . . . . . . |
| an1 …… ani ……anj ……ann |
那么我们将从第r行到第k行的每一行中相同列的加起来,可以得到一个一维数组如下:
(ar1+……+ak1, ar2+……+ak2, ……,arn+……+akn)
由此我们可以看出最后所求的就是此一维数组的最大子断和问题,到此我们已经将问题转化为上面的已经解决了的问题了。
代码:
#include <iostream> using namespace std; #define M 110 int main() { int a[M][M]={0},c[M][M]={0}; //a[M][M]用来存数,c[M][M]就是存这行到这一列的和。 int i,j,n,max=0,sum,k; scanf("%d",&n); for(i=0;i<n;i++) for(j=1;j<=n;j++) { scanf("%d",&a[i][j-1]); c[i][j]=c[i][j-1]+a[i][j-1]; //一行的下一个数和上面所有数的和。 } for(i=0;i<n;i++) for(j=i;j<=n;j++) { sum=0; for(k=0;k<n;k++) { sum+=c[k][j]-c[k][i]; if(sum<0) sum=0;[code] //小于0就相当于不用取了,直接去掉
else if(sum>max) max=sum;
}
}
printf("%d\n",max);
return 0;
}[/code]
相关文章推荐
- POJ--1050--To the Max(线性动规,最大子矩阵和)
- POJ 1050 To the Max(枚举+动规)
- POJ--1050--To the Max(线性动规,最大子矩阵和)
- POJ 1050-To the Max (动态规划)
- zoj 1074 || poj 1050 To the Max(简单DP)
- poj 1050 To the Max
- POJ 1050 To the Max(最大子段)
- poj 1050 To the Max
- poj 1050 To the Max
- POJ 1050 To the Max【最大子矩阵】
- POJ 1050 To the Max (动态规划——求最大子矩阵和)
- POJ-1050-To the Max 解题报告
- POJ 1050 To the Max
- POJ 1050 To the Max
- poj 1050 To the Max
- 【原】 POJ 1050 To the Max 求二维矩阵的最大子矩阵 解题报告
- DP::Poj1050 To the max
- POJ 1050 To the Max
- poj1050 To the Max
- POJ_1050_To the Max