diff --git a/TangDou/Topic/PrefixAndSuffix/P1719_1.cpp b/TangDou/Topic/PrefixAndSuffix/P1719_1.cpp new file mode 100644 index 0000000..f7da2e9 --- /dev/null +++ b/TangDou/Topic/PrefixAndSuffix/P1719_1.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; +const int N = 130; +int n; +int a[N][N]; // 存储题目中的矩阵 +int s[N][N]; // 二维前缀和 +int qz[N][N]; // qz[i][j]指的是第i行到j的前缀和 + +int main() { + cin >> n; + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + cin >> a[i][j]; + qz[i][j] = qz[i][j - 1] + a[i][j]; // 求前缀和 + s[i][j] = qz[i][j] + s[i - 1][j]; // 计算s + } + } + + int mx = INT_MIN; // 存储答案 + // 遍历左上角坐标,与,右下角坐标 + for (int x1 = 1; x1 <= n; x1++) { + for (int y1 = 1; y1 <= n; y1++) { + for (int x2 = 1; x2 <= n; x2++) + for (int y2 = 1; y2 <= n; y2++) { + if (x2 < x1 || y2 < y1) continue; // 如果左上角比右下角还要大,就不用求了,下一个 + mx = max(mx, s[x2][y2] + s[x1 - 1][y1 - 1] - s[x2][y1 - 1] - s[x1 - 1][y2]); // 求最大值 + } + } + } + cout << mx << endl; // 输出 + return 0; +} \ No newline at end of file diff --git a/TangDou/Topic/PrefixAndSuffix/P1719.cpp b/TangDou/Topic/PrefixAndSuffix/P1719_2.cpp similarity index 69% rename from TangDou/Topic/PrefixAndSuffix/P1719.cpp rename to TangDou/Topic/PrefixAndSuffix/P1719_2.cpp index 2196122..761b2c1 100644 --- a/TangDou/Topic/PrefixAndSuffix/P1719.cpp +++ b/TangDou/Topic/PrefixAndSuffix/P1719_2.cpp @@ -13,14 +13,14 @@ int main() { // O(N^3)算法 for (int i = 1; i <= n; i++) { - for (int j = 0; j < i; j++) { - int _s = 0; + for (int j = 0; j < i; j++) { // 前缀和需要下标从0开始 + int sum = 0; for (int k = 1; k <= n; k++) { - _s = max(_s, 0) + s[i][k] - s[j][k]; - res = max(res, _s); + sum = max(sum, 0) + s[i][k] - s[j][k]; + res = max(res, sum); } } } - cout << res << '\n'; + cout << res << endl; return 0; }