二维前缀和 || 类子序列和的DP
后者快,压二维为一维的方法
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 const int N = 108; 9 int n, ans = -2147483633, up[N][N], dp[N], a, b[N]; 10 11 int main() 12 { 13 while (~scanf("%d", &n)) 14 { 15 ans = -2147483633; 16 for (int i = 1; i <= n; ++i) 17 for (int j = 1; j <= n; ++j) 18 scanf("%d", &a), up[i][j] = a + up[i - 1][j]; 19 for (int line = 1; line <= n; ++line) 20 for (int high = 1; high <= line; ++high) 21 { 22 for (int i = 1; i <= n; ++i) 23 dp[i] = 0, b[i] = up[line][i] - up[high - 1][i]; 24 for (int i = 1; i <= n; ++i) 25 dp[i] = max(dp[i - 1], 0) + b[i], ans = max(dp[i], ans); 26 } 27 printf("%d ", ans); 28 } 29 return 0; 30 }