二维数组前缀和!!!
acwing 99 激光炸弹
题解有人写扫描线,Orz Orz Orz
前缀和嘛,固定区间,具体上图
公式都摆出来了,上代码
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #define maxn 5010 using namespace std; int map[maxn][maxn]; int n, m; int main() { scanf("%d %d", &n, &m); int nn = m, mm=m; int x, y, val; while (n--) { scanf("%d%d%d", &x, &y, &val); x++; y++; map[x][y] = val; nn = max(nn, x); mm = max(mm, y); } for (int i = 1; i <= nn; i++) { for (int j = 1; j <= mm; j++) { map[i][j] = map[i - 1][j] + map[i][j - 1] - map[i - 1][j - 1] + map[i][j]; } } int ans = 0; for (int i = m; i <= nn; i++) { for (int j = m; j <= mm; j++) { ans = max(ans, map[i][j] + map[i - m][j - m] - map[i - m][j] - map[i][j - m]); } } printf("%d\n", ans); return 0; }
寻找真正的热爱