最大子矩阵和 URAL 1146 Maximum Sum
1 /*
2 最大子矩阵和:把二维降到一维,即把列压缩;然后看是否满足最大连续子序列;
3 好像之前做过,没印象了,看来做过的题目要经常看看:)
4 */
5 #include <cstdio>
6 #include <iostream>
7 #include <cstring>
8 #include <algorithm>
9 using namespace std;
10
11 const int MAXN = 1e2 + 10;
12 const int INF = 0x3f3f3f3f;
13 int a[MAXN][MAXN];
14 int dp[MAXN][MAXN][MAXN];
15
16 int main(void) //URAL 1146 Maximum Sum
17 {
18 //freopen ("D.in", "r", stdin);
19
20 int n;
21 while (scanf ("%d", &n) == 1)
22 {
23 int ans = -INF;
24 memset (dp, 0, sizeof (dp));
25 for (int i=1; i<=n; ++i)
26 {
27 for (int j=1; j<=n; ++j)
28 {
29 scanf ("%d", &a[i][j]);
30 }
31 }
32
33 for (int i=1; i<=n; ++i)
34 {
35 for (int j=1; j<=n; ++j)
36 {
37 int sum = 0;
38 for (int k=j; k>=1; --k)
39 {
40 sum += a[i][k];
41 dp[i][j][k] = max (sum + dp[i-1][j][k], sum);
42 ans = max (ans, dp[i][j][k]);
43 }
44 }
45 }
46
47 printf ("%d\n", ans);
48 }
49
50 return 0;
51 }
编译人生,运行世界!