AcWing
99. 激光炸弹
注意边界,题目给出的坐标,每一维是从0~5000
,共5001
个数。可以整体将坐标+1
,这样,将0
作为特殊边界。之后就是遍历前缀和数组,取某段二维区间的最大值。
#include <bits/stdc++.h>
using namespace std;
int n, r;
const int N = 5010;
int s[N][N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> r;
for(int i = 1; i <= n; i++){
int x, y, z;
cin >> x >> y >> z;
s[x + 1][y + 1] += z;
}
for(int i = 1; i <= N - 1; i++) for(int j = 1; j <= N - 1; j++)
s[i][j] = s[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
if(r >= 5001){
cout << s[N - 1][N - 1];
return 0;
}
int maxs = -1;
for(int i = r; i <= N - 1; i++){
for(int j = r; j <= N - 1; j++){
int t = s[i][j] - s[i - r][j] - s[i][j - r] + s[i - r][j - r];
if(t > maxs) maxs = t;
}
}
cout << maxs;
return 0;
}