Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 43725 | Accepted: 23166 |
Description
Input
Output
Sample Input
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
Sample Output
15
Source
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN = 105; const int INF = 0x7fffffff; int best; int rowsum[MAXN][MAXN]; int sum[MAXN]; int n; int ans; int main() { scanf("%d", &n); memset(rowsum, 0, sizeof(rowsum)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { scanf("%d", &rowsum[i][j]); rowsum[i][j] += rowsum[i][j - 1]; } } ans = -INF; sum[0] = 0; for (int i = 1; i <= n; ++i) for (int j = i; j <= n; ++j) { best = 0; for (int k = 1; k <= n; ++k) { sum[k] = rowsum[k][j] - rowsum[k][i - 1] + sum[k - 1]; /* 压缩 */ ans = max(ans, sum[k] - sum[best]); if (sum[best] > sum[k]) best = k; } } printf("%d\n", ans); return 0; }
原文:http://www.cnblogs.com/albert7xie/p/4729788.html