diff --git a/Luogu/P1565/P1565.cpp b/Luogu/P1565/P1565.cpp index 6d74fc88..6f16ed03 100644 --- a/Luogu/P1565/P1565.cpp +++ b/Luogu/P1565/P1565.cpp @@ -8,7 +8,36 @@ const char endl = '\n'; const int N = 205; int n, m; -long long g[N][N], ans; +long long g[N][N], s[N][N], c[N], ans; + +bool check(int x) { + long long t = 0; + + for (int i = x; i <= m; i++) { + t = std::min(t, c[i - x]); + + if (c[i] > t) return true; + } + + return false; +} + +int calc() { + int l = 1, r = n, res = 0; + + while (l <= r) { + int mid = (l + r) >> 1; + + if (check(mid)) { + res = mid; + l = mid + 1; + } else { + r = mid - 1; + } + } + + return res; +} int main() { std::ios::sync_with_stdio(false); @@ -20,19 +49,17 @@ int main() { for (int j = 1; j <= m; j++) { cin >> g[i][j]; - g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1]; + s[i][j] = s[i - 1][j] + g[i][j]; } } for (int i = 1; i <= n; i++) { - for (int j = 1; j <= m; j++) { - for (int k = i; k <= n; k++) { - for (int l = j; l <= m; l++) { - if (g[k][l] - g[k][j - 1] - g[i - 1][l] + g[i - 1][j - 1] >= 0) { - ans = std::max(ans, static_cast(k - i + 1) * (l - j + 1)); - } - } + for (int j = i; j <= n; j++) { + for (int k = 1; k <= m; k++) { + c[k] = c[k - 1] + s[j][k] - s[i - 1][k]; } + + ans = std::max(ans, static_cast(j - i + 1) * calc()); } }