题目分析
好久之前就做过,直接将2维转化为类似一维的解法即可。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 505;
const int INF = 0x3f3f3f3f;
int sum[maxn][maxn];
int main(){
int n, m, x;
while(scanf("%d%d", &n, &m) != EOF){
memset(sum, 0, sizeof(sum));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
scanf("%d", &x);
sum[i][j] += sum[i][j-1] + x;
}
}
int Max = -INF;
for(int i = 0; i <= m; i++){
for(int j = i+1; j <= m; j++){
int ans = 0;
for(int k = 1; k <= n; k++)
{
if(ans >= 0)
ans += sum[k][j] - sum[k][i];
else
ans = sum[k][j] - sum[k][i];
Max = max(Max, ans);
}
}
}
printf("%d\n", Max);
}
return 0;
}