1. 题目
网址http://poj.org/problem?id=1050
Tothe Max
Time Limit: 1000MS | Memory Limit: 10000K |
Total Submissions: 43636 | Accepted: 23126 |
Description
Given a two-dimensional array of positive and negative integers, asub-rectangle is any contiguous sub-array of size 1*1 or greater located withinthe whole array. The sum of a rectangle is the sum of all the elements in thatrectangle. In this problem the sub-rectangle with the largest sum is referredto as the maximal sub-rectangle.
给定一个包含正整数和负整数的二维数组,子矩形是任何连续的大小为1 * 1或更大的占据整个数组的子矩阵。矩形的总和是矩形中所有元素的总和。在这个问题中,具有最大的总和的子矩形被称为最大的子矩形。
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 beginswith a single positive integer N on a line by itself, indicating the size ofthe square two-dimensional array. This is followed by N^2 integers separated bywhitespace (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 toright, then all numbers in the second row, left to right, etc. N may be aslarge as 100. The numbers in the array will be in the range [-127,127].
输入由一系列N*N的整数数组构成。输入以一个单独占一行的正整数N开始,表示二维矩阵数组的大小。接下来是一系列由分隔符(空格和换行符)分开的N^2个整数。它们是这个矩阵数组的N^2个整型元素,以行顺序展示。也就是说,所有的数字在第一行,从左到右,接下来的所有数字在第二行,从左到右,等等。N可能最大为100。数组中元素的数字在[- 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
2. 题目分析
此题目是最大子序列问题的变形。将一维数列的最大子序列问题扩展至二维数组求解。
其解决思路是将二维数组的最大子矩形其转化为一维数列的最大子序列问题。将二维数组的每一列求和,就可以将其转换成一个值,此时,按行的顺序看此二维数列,就是一个一维数组。关于最大子序列问题参见C/C++目录下最大子序列问题的介绍。
3. 题目解答(c++)
(1) 思路
第一步: 设定输入的数组是input[n][n]。从input[i][]行开始,先获取第i行的最大子序列,存贮至变量max。从i=0开始循环,直至i=n-1结束。
第二步: 将input[i][]+input[i+1][]相加,将第i行和第(i+1)两行合并为一行,并求此行的最大子序列,并更新max数值
第三步:以此类推,直至将input[i][]+input[i+1][]+……+input[n-1][]行相加,更新max数值。
第四步:再将i值++,开始下一轮循环,直至i=n-1对应的循环结束。最后的max数值,就是所求
(2)代码实现
#include <iostream>
using namespace std;
/**********************************/
/***北大POJ 1050 To the Max********/
/***modified by lbs 2015.8.7******/
/**********************************/
int list[100], n; //定义一维数组(最大为100个元素)和元素个数n
//函数:计算一维数组的最大子序列和
//参数:list[n] n--数组元素个数
// 返回值:max 一维数组的最大子序列和
int My_SubSum(int list[], int n)//计算子矩阵最大子序列的和
{
int sum = 0;
int max = -1000000; //max初始化
int i = 0;
for (i = 0; i<n; i++)
{
sum += list[i]; //sum(i,j+1)=sum(i,j)+list[j]
if (sum > max)
{
max = sum;
}
if (sum < 0)
{
sum = 0;
}
}
return max;
}
int main()
{
int input[110][110]; //输入的数组
int max1;
int i, j, k;
while (cin>>n)
{
max1 = -1000000; //init
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cin >> input[i][j];//输入矩阵
}
}
for (i = 0; i<n; i++)
{
for (j = 0; j < n; j++)
{
//list[n]存储input[i][j]二维数组中第i行的元素
list[j] = input[i][j];
}
//获取input[i][j]中第i行元素的最大子序列和
max1 = My_SubSum(list, n)> max1 ? My_SubSum(list, n) : max1;
for (j = i + 1; j<n; j++)
{
for (k = 0; k<n; k++)
{
//list[n]存储input[i][j]二维数组中(第i行+第(i+1)行)的元素
list[k] += input[j][k];
}
//获取input[i][j]中(第i行+第(i+1)行)的元素的最大子序列和
max1 = My_SubSum(list, n)> max1 ? My_SubSum(list, n) : max1; //获取最大值
}
}
cout<<max1<<endl;
}
//system("pause");
return 0;
}
4. 参考资料
[1] http://blog.youkuaiyun.com/gubojun123/article/details/7797329
[2] http://www.2cto.com/kf/201307/225010.html
[3] http://blog.youkuaiyun.com/ultimater/article/details/7833184
[4] http://blog.chinaunix.net/uid-22263887-id-1778903.html
[5] http://www.tuicool.com/articles/JV7rauB
[6] http://blog.sina.com.cn/s/blog_82c81ada0100szg4.html
[7] http://www.haodaima.net/art/2838159