题意:已知一个矩阵,求其中的最大子矩阵
链接:http://acm.timus.ru/problem.aspx?space=1&num=1146
思路:维护矩阵的每一行的前缀和,按列统计求n^2个最大连续子段和,求出其中的最大值
注意点:无
以下为AC代码:
ID | Date | Author | Problem | Language | Judgement result | Test # | Execution time | Memory used |
---|---|---|---|---|---|---|---|---|
6248594 |
20:31:07
15 May 2015 | luminous11 | 1146. Maximum Sum | G++ 4.9 C++11 | Accepted | 0.046 | 450 KB |
/*
***********************************************
*# @Author : Luminous11 (573728051@qq.com)
*# @Date : 2015-05-11 06:19:00
*# @Link : http://blog.csdn.net/luminous11
***********************************************
*/
#include <bits/stdc++.h>
#define clr(a, v) memset( a , v , sizeof(a) )
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1.0);
int num[105][105];
int main()
{
ios::sync_with_stdio ( false );
int n;
int x;
while ( scanf ( "%d", &n ) != EOF ){
memset ( num, 0, sizeof ( num ) );
for ( int i = 1; i <= n; i ++ ){
for ( int j = 1; j <= n; j ++ ){
scanf ( "%d", &x );
num[i][j] += num[i][j-1] + x;
}
}
int _max = -0x3f3f3f3f;
for ( int i = 1; i <= n; i ++ ){
for ( int j = 1; j <= i; j ++ ){
for ( int sum = 0, k = 1; k <= n; k ++ ){
sum += num[k][i] - num[k][j-1];
if ( sum > _max )_max = sum;
if ( sum < 0 ) sum = 0;
}
}
}
printf ( "%d\n", _max );
}
return 0;
}