Files
python/TangDou/Topic/PrefixAndSuffix/P1719_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

27 lines
1005 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}