枚举正方行边长为r的
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 6 const int N = 5e3 + 5; 7 int g[N][N]; 8 int main(){ 9 int N,R; 10 cin >> N >> R; 11 int n = R, m = R; 12 for(int i = 0, x, y, w; i < N; ++ i) 13 { 14 cin >> x >> y >> w; 15 x++,y++; 16 n = max(x,n), m = max(m, y); 17 g[x][y] += w; 18 } 19 for(int i = 1; i <= n; ++ i)//求前缀和 20 for(int j = 1; j <= m; ++ j) 21 g[i][j] += g[i-1][j] + g[i][j-1] - g[i-1][j-1]; 22 int ans = 0; 23 for(int i = R; i <= n; ++ i) 24 for(int j = R; j <= m; ++ j) 25 ans = max(ans, g[i][j] - g[i-R][j] - g[i][j-R] + g[i-R][j-R]);//枚举正方形 26 cout << ans << endl; 27 return 0; 28 }