ACM-最大子矩陣和
阿新 • • 發佈:2019-01-09
最大子矩陣和問題是對最大子序列和問題的擴充套件,即從一維擴充套件到了二維。但是解決此問題的方法和原來的方法並沒有太大的差別,這裡就以同樣的動態規劃的實錄思路來求解此問題。原來subSum[i]代表包含ai並且以ai結束的子序列的最大和,狀態轉移方程為subSum[i+1] = subSum[i]<0 ? 0 : subSum[i]+a[i+1],因為subSum[i]為負將對總和做負貢獻,所以此時將其丟棄。而現在,序列已經變成了矩陣,subSum[i]也不能再表示原來的意義了,需要將其擴充套件為二維,subSum[k]表示包含第k行第i至j列元素並且以其結尾的子矩陣的最大和,定義sum[i,j]表示表示第k行第i至j列元素和,那麼同樣的有轉移方程subSum[k+1] = subSum[k]<0 ? 0 : subSum[k]+sum[i,j],最後的問題就是如何計算sum[i,j],因為已經有三層迴圈列舉i、j、k了,如果這裡再用迴圈計算,複雜度將會增加,所以可以在輸入資料的時候將sum[i,j]處理成第i行前j列的和,所以原來轉移方程中的sun[i,j]就可以直接寫成sum[k,j]-sum[k,i-1],不用再迴圈計算了。實現程式碼如下:
#define MAX 105 // 第i行前j個元素和 int sum[MAX][MAX]; // 矩陣的行列數 int n; // 返回最大子矩陣和,並且儲存該子矩陣的起始元素和結束元素的行列值 int MaxSubMatSum(int &sX, int &sY, int &eX, int &eY) { int ans=1e-6; // 列舉第k行,第i列至j列的和 for(int i=1; i<=n; ++i) { for(int j=i; j<=n; ++j) { int subSum = 0; for(int k=1; k<=n; ++k) { // 動態規劃思想,subSum<0將會減少總和,所以此時將其置0 if(subSum < 0) { subSum = 0; sX = k; sY = i; } subSum += sum[k][j] - sum[k][i-1]; if(ans < subSum) { ans = subSum; eX = k; eY = j; } } } } return ans; }
以一道題為例,應用上述演算法,HDOJ:1081,時空轉移(點選開啟連結),題目如下:
To The Max
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8952 Accepted Submission(s): 4328
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 4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
Sample Output 15 題意:
給出一個n*n的矩陣,找出其最大子矩陣和,只需要輸出和即可。
分析:
典型的計算最大子矩陣和的題目,如前面分析求解即可。但是不需要輸出子矩陣本身,所以可以對前面的演算法簡化,直接寫入主程式即可。
原始碼:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 105
// 第i行前j個元素和
int sum[MAX][MAX];
int main()
{//freopen("sample.txt", "r", stdin);
int n;
while(~scanf("%d", &n))
{
int data, ans=1e-6;
memset(sum, 0, sizeof(sum));
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=n; ++j)
{
scanf("%d", &data);
// 累加第i行前j個元素和
sum[i][j] += sum[i][j-1] + data;
}
}
// 列舉第k行,第i列至j列的和
for(int i=1; i<=n; ++i)
{
for(int j=i; j<=n; ++j)
{
int subSum = 0;
for(int k=1; k<=n; ++k)
{
// 動態規劃思想,subSum<0將會減少總和,所以此時將其置0
if(subSum < 0) subSum = 0;
subSum += sum[k][j] - sum[k][i-1];
ans = max(ans, subSum);
}
}
}
printf("%d\n", ans);
}
return 0;
}