diff --git a/TangDou/Topic/PrefixAndSuffix/P1719.cpp b/TangDou/Topic/PrefixAndSuffix/P1719.cpp new file mode 100644 index 0000000..6e8ace8 --- /dev/null +++ b/TangDou/Topic/PrefixAndSuffix/P1719.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +const int N = 130; +int a[N][N], s[N][N]; +int main() { + int n, res = INT_MIN; + cin >> n; + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) { + cin >> a[i][j]; + s[i][j] = s[i - 1][j] + a[i][j]; + } + + for (int i = 1; i <= n; ++i) { + for (int j = 0; j < i; ++j) { + int _s = 0; + for (int k = 1; k <= n; ++k) { + _s = max(_s, 0) + s[i][k] - s[j][k]; + res = max(res, _s); + } + } + } + cout << res << '\n'; + return 0; +} diff --git a/TangDou/Topic/【前缀和与差分】题单.md b/TangDou/Topic/【前缀和与差分】题单.md index ba476bd..5f0c5b6 100644 --- a/TangDou/Topic/【前缀和与差分】题单.md +++ b/TangDou/Topic/【前缀和与差分】题单.md @@ -272,7 +272,41 @@ signed main() { } ``` -#### [$P1719$ 最大加权矩阵](https://www.luogu.com.cn/problem/P3406) +#### [$P1719$ 最大加权矩阵](https://www.luogu.com.cn/problem/P1719) + +**解题思路** +这道题和$P1115$最大子段和思路类似。 +只是将一维升到二维,很重要的解决思路:**矩阵压缩**。 +如果能把二维降到一维,那就好处理了。 + +假设有一个矩阵: +```cpp {.line-numbers} +-5 6 4 +1 -2 6 +2 1 -3 +``` +想得到其中任意一个矩阵,我们不妨先确定列。 +很明显用两个循环嵌套可以解决。 +假设我们取$2,3$列 +```cpp {.line-numbers} +6 4 +-1 6 +1 -3 +``` + +列取好之后那么每一行的和值就是确定的了。 + +**注意**:用前缀和可以优化时间。 +```cpp {.line-numbers} +10 +5 +-2 +``` +接下来我们再在这个一维数组中求最大子序列的和。 + +就和$P1115$的做法一样了! + + #### [$P2004$ 领地选择](https://www.luogu.com.cn/problem/P2004)