激光炸弹
问题链接:BZOJ1218
思路
先统计各位置的总价值,然后直接原数组求出二维前缀和。根据几何关系算(i,j)到(i+R-1,j+R-1)的正方形内部的价值,边算边更新最大值。注意i+R-1和j+R-1可能会越界,和5000取min即可。
ac代码
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int s[5001][5001] = { 0 };
int main() {
int n, r;
cin >> n >> r;
for (int k = 0; k < n; k++) {
int x, y, v;
cin >> x >> y >> v;
s[x][y] += v;
}
for (int i = 0; i <= 5000; i++)
for (int j = 1; j <= 5000; j++)
s[i][j] += s[i][j - 1];
for (int j = 0; j <= 5000; j++)
for (int i = 1; i <= 5000; i++)
s[i][j] += s[i - 1][j];
int maxvalue = 0;
for (int i = 0; i <= 5000; i++) {
for (int j = 0; j <= 5000; j++) {
int vr;
int x = min(5000, i + r - 1), y = min(5000, j + r - 1);
if (i == 0 && j == 0)
vr = s[x][y];
else if (i == 0)
vr = s[x][y] - s[x][j - 1];
else if (j == 0)
vr = s[x][y] - s[i - 1][y];
else {
vr = s[x][y] + s[i - 1][j - 1] - s[i - 1][y] - s[x][j - 1];
}
maxvalue = max(maxvalue, vr);
}
}
cout << maxvalue;
}
但我在vs上运行的时候感觉有点慢啊……
来源:CSDN
作者:没有自旋的π介子
链接:https://blog.csdn.net/qq_45616764/article/details/104157009