URAL 1146 Maximum Sum 最大子矩阵和
题目:click here
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef unsigned long long ll; 4 const int INF = 0x3f3f3f3f; 5 const int M = 2e2+3; 6 7 int n; 8 int a[M][M]; // a[i][j] 表示从[i][0]到[i][j]的和 9 int main() { 10 while( ~scanf("%d", &n ) ) { 11 memset( a, 0, sizeof(a) ); 12 for( int i=1; i<=n; i++ ) { 13 for( int j=1; j<=n; j++ ) { 14 scanf("%d", &a[i][j] ); 15 a[i][j] += a[i][j-1]; 16 } 17 } 18 int ret = -INF; 19 for( int i=1; i<=n; i++ ) { 20 for( int j=0; j<i; j++ ) { 21 int sum = 0; 22 for( int h=1; h<=n; h++ ) { 23 sum += a[h][i] - a[h][j]; 24 ret = max( ret, sum ); 25 if( sum < 0 ) sum = 0; 26 } 27 } 28 } 29 printf("%d\n", ret ); 30 } 31 return 0; 32 }