标签:typedef tar class cpp wing ref href long can
网址:https://www.acwing.com/problem/content/101/
给出一个矩阵,求边长为$R$的正方形子矩阵和的最大值。
求二维前缀和然后枚举左上角点即可。
二维前缀和求法:$S[i][j]=S[i-1][j]+S[i][j-1]-S[i-1][j-1]+a[i][j]$。其中因为$S[i-1][j-1]$加了两次,所以需要减去一次。
$O(1)$求子矩阵和:$sum=S[i][j]-S[i-k][j]-S[i][j-l]+S[i-k][j-l]$。
AC代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 5005; int s[MAXN][MAXN]; int main() { int n, r, a, b, v; scanf("%d%d", &n, &r); for (int i = 0; i < n; ++i) scanf("%d%d%d", &a, &b, &v), s[a + 1][b + 1] = v; for (int i = 1; i < MAXN; ++i) for (int j = 1; j < MAXN; ++j) s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; int maxn = 0; for (int i = r; i < MAXN; ++i) for (int j = r; j < MAXN; ++j) maxn = max(maxn, s[i][j] + s[i - r][j - r] - s[i][j - r] - s[i - r][j]); printf("%lld\n", maxn); return 0; }
标签:typedef tar class cpp wing ref href long can
原文地址:https://www.cnblogs.com/Aya-Uchida/p/11873891.html