- To the Max
POJ - 1050
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:
input:
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
output:
15
算法设计:利用动态规划,通过将二维转一维。
- 首先先了解一维的最大子序列动态规划
#include <iostream>
#include <math.h>
using namespace std;
int maxSubArray(int nums[],int n)
{
int *dp = new int[n]; // dp记录以当前位置结尾的最大序列和
dp[0] = nums[0];
if (n == 0)
return 0; //没有数则返回0
for (int i = 1; i < n; i++)
{
//最大只有两种情况,以当前数,以前一个序列和加当前数
dp[i] = max(nums[i], dp[i - 1] + nums[i]);
}
int maxs = -9999;
for (int i = 0; i < n; i++)
{
maxs = max(dp[i], maxs); //找到最大的子序列和
}
return maxs;
}
int main()
{
int n;
cout << "pleases input a row scale :";
cin >> n;
cout << "pleases input matrix :";
int *nums = new int[n];
for (int i = 0; i < n; i++)
{
cin >> nums[i];
}
cout << maxSubArray(nums,n);
return 0;
}
- 将矩阵相邻两行对应列的元素相加形成一个新的序列,试分析该子序列中每一个元素的含义,其最大子序列的含义。
相邻两行对应列相加,形成了一个新的序列,该新序列的每一个元素代表了该矩阵中的每一列的和;求出该序列的最大子序列和即求出了该矩阵的最大子矩阵。
- 分析复杂度
如果子矩阵的行数是1,那么它可以是下面几个矩阵的子矩阵:
0 -2 -7 0
求b数组上的最大子序列得到:maxs=0
或者
9 2 -6 2
求b数组上的最大子序列得到:maxs=11
或者
-4 1 -4 1
求b数组上的最大子序列得到:maxs=1
或者
-1 8 0 -2
求b数组上的最大子序列得到:maxs=8
在这里的4种情况中,最大子矩阵就是最大子序列,ans=11
如果子矩阵的行数是2,那么它可以是下面几个矩阵的子矩阵:
0 -2 -7 0
9 2 -6 2
或者
9 2 -6 2
-4 1 -4 1
或者
-4 1 -4 1
-1 8 0 -2
在这里的3种情况中,我们还要找出一个最大的子矩阵,进行列相加得到b[]
9 0 -13 2
求b数组上的最大子序列得到:maxs=9
或者
5 3 -10 3
求b数组上的最大子序列得到:maxs=8
或者
-5 9 -4 -1
求b数组上的最大子序列得到:maxs=9
然后刷新ans最大子矩阵:最大为ans=9
如果子矩阵的行数是3,那么它可以是下面几个矩阵的子矩阵:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
或者
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
在这里的2种情况中,我们还要找出一个最大的子矩阵,进行列相加得到b[]
5 1 -17 3
求b数组上的最大子序列得到:maxs=6
4 11 -10 1
求b数组上的最大子序列得到:maxs=15
然后刷新ans最大子矩阵:最大为ans=15
如果子矩阵的行数是4,那么它可以是下面1个矩阵的子矩阵:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
在这里的1种情况中,我们还要找出一个最大的子矩阵,进行列相加得到b[]
4 9 -17 1
求b数组上的最大子序列得到:maxs=13
综上,得到最大ans=15.
首先对于一个序列求最大连续子序列和的时间复杂度为O(n),然后O(n)求出这个序列的最大子序列和,就是对应的上下边界的最优解,这样枚举出的上下边界一共需要O(n^ 2),再乘以求一维子序列的时间复杂度O(n),所以最后时间复杂度为O(n^ 3).
- 有一个二维矩阵,矩阵中元素有正有负。定义子矩阵的和为其所有元素之和,最大子矩阵为子矩阵和值最大的子矩阵。
#include <iostream>
#include <math.h>
using namespace std;
int a[100][100];
int n;
int maxSubRowArray(int b[])
{
int *dp = new int[n]; // dp记录以当前位置结尾的最大序列和
dp[0] = b[0];
if (n == 0)
return 0; //没有数则返回0
for (int i = 1; i < n; i++)
{
//最大只有两种情况,以当前数,以前一个序列和加当前数
dp[i] = max(b[i], dp[i - 1] + b[i]);
}
int maxs = -9999;
for (int i = 0; i < n; i++)
{
maxs = max(dp[i], maxs); //找到最大的子序列和
}
return maxs;
}
int trans()
{
int b[100];
//将二维转为一维
int ans = -9999;
for (int i = 0; i < n; i++) //从第一行开始枚举子矩阵
{
for(int r=0;r<n;r++)
{
b[r]=0;//初始化中间数组
}
for (int j = i; j < n; j++) //到第j行结束,组成从第i到第j行的数组
{
for (int k = 0; k < n; k++) //将每一列进行压缩为一维数组
{
b[k] = b[k] + a[j][k]; //每一列求和之后存入b
}
//更新ans
int maxs = maxSubRowArray(b);
ans = max(maxs, ans);
}
}
return ans;
}
int main()
{
cout << "pleases input matrix scale:";
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
cout << trans();
return 0;
}