You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
1005 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 150;
int a[N][N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
a[i][j] += a[i - 1][j]; // 按列计算前缀和
}
int ans = INT_MIN; // 预求最大,先设最小
for (int i = 1; i <= n; i++) // 最底下一行
for (int k = 1; k <= i; k++) { // 往上k行
int dp[N] = {0}; // dp[j]表示压缩的矩形前j列的最大累加值
for (int j = 1; j <= n; j++) { // 第j列
int s = a[i][j] - a[i - k][j]; // 求压缩的矩形第j列的值
dp[j] = max(dp[j - 1] + s, s); // 动态规划,到j列为止最大的连续累加和
ans = max(ans, dp[j]); // 更新答案
}
}
cout << ans << endl; // 愉快AC
return 0;
}