AcWing 99. 激光炸弹(二维前缀和)

题目链接:点击这里
在这里插入图片描述
在这里插入图片描述

本题目标是“点”,二维前缀和是“格子”,如上图所示,实际上是求边长为 R 1 R-1 的矩形区域。

即套用二维前缀和时,以 ( i , j ) (i,j) 为右下角,以 ( i R + 1 , j R + 1 ) (i-R+1,j-R+1) 为左上角。

另外,本题注意内存限制较为严格,少开一个二维数组。

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 5010;
int a[maxn][maxn];

int main()
{
	int N, R;
	scanf("%d%d", &N, &R);
	int row = R, col = R;
	for(int i = 0; i < N; ++i)
	{
		int x, y, w;
		scanf("%d%d%d", &x, &y, &w);
		x++; y++;						//下标从1开始 
		row = max(x,row);
		col = max(y,col);
		a[x][y] += w;
	}
	
	for(int i = 1; i <= row; ++i)
		for(int j = 1; j <= col; ++j)
			a[i][j] = a[i-1][j] + a[i][j-1] - a[i-1][j-1] + a[i][j];
	
	int ans = -1;
	
	for(int i = R; i <= row; ++i)
		for(int j = R; j <= col; ++j)
			ans = max(ans, a[i][j] - a[i-R][j] - a[i][j-R] + a[i-R][j-R]);
	
	printf("%d\n", ans);
	return 0;
}
发布了727 篇原创文章 · 获赞 111 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104258411