不了解前缀和的小伙伴,点击下方
(https://blog.csdn.net/tobeyours/article/details/79746095)
#include<iostream>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<set>
const int N = 5050;//C++中的特性:也可以在main函数中定义一个
int a[N][N];
int R;
int x11, y11, v11;//原来y1在math.h函数内已经定义过了
int m = 0, n = 0;
using namespace std;
int main()
{
int N, R;
cin >> N >> R;
m = n = R;
while (N--)
{
cin >> x11 >> y11 >> v11;
x11++; y11++;
a[x11][y11] += v11;//同一个位置 可能会有多个价值点
m = max(m, x11);//确保边界>=R 为什么:因为会出现R大于矩阵的情况 所以X11很有可能取到比R小的数 作为后面循环条件的m至少要>=R
n = max(n, y11);
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
{
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
int max_s = -99999999;
/*
m为矩阵的列 n为矩阵的行
*/
for(int i = R; i <= m; i++)//都是枚举R*R矩阵右下角的点 直接从R开始循环 只因为比R小的矩阵都会被R包括在内
//假设R=3 就可以从(3,3)开始找,因为(2,2)等等之类的矩阵和都会比(3,3)小
for (int j = R; j <= n; j++)
{
max_s = max(max_s, a[i][j] - a[i - R][j] - a[i][j - R] + a[i - R][j - R]);
}
cout << max_s << endl;
}
/*
2 1
0 0 1
1 1 1
*/