[AcWing 99] laser bombs - and two-dimensional prefix

(Face questions from AcWing)

A novel laser bombs that can destroy a side length  R  of all targets within the square.

There are now on the map  N  target, integer the X- i , the Y- i represents the position of the target on the map, each target has a value of W i .

Laser bombs served by satellite positioning, but it has a disadvantage that its explosion range, i.e., the side length  R sides of the square must be of the X , Y -axis.

If the target is located on the edge of the square blasting, the target will not be destroyed.

Seeking a bomb blew up on the map up to a total value of how much of the target.

Input Format

The first line of the input positive integer  N  and  R & lt  , represents the number of objects on the side of the square, and the map data separated by spaces.

Next N lines of a set of input data, each set of data comprises three integers X- I , the Y I , W is I , representing the target x -coordinate, Y -coordinate and value of data separated by a space.

Output Format

Output a positive integer representing up to blow up a bomb on the value of the total number of map objects.

data range

0<N10000,
0Xi,Yi5000

   

  After a two-dimensional prefix and violence can enumerate each square. Pit two points:

  1, first recording xi, yi of the maximum value of n, m, n and m as the lower right end of the boundary map to enumerate. Since the side length of the range is not given, R & lt there may be greater than the size of the entire map, and thus the initial value of m to n to r.

  2, the original map is to place bombs on the grid, and the boundary is not included in the explosive range, hard to deal with. We consider the problem from another point of view, the transformation of the map: target on the fixed grid, the entire "board" as moved (0.5, 0.5) units to the right. In this case, each target in the center of the movement after the checkerboard, as long as each enumeration and a side length of the square of r-1 can be fried to the square originally included in the calculation target. This operation is not good imagination, it is recommended to validate their own simulation.

Code:

  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cctype>  
  4. #define rep(i, a, b) for(int i = a; i <= b; ++i)  
  5. #define per(i, b, a) for(int i = b; i >= a; --i)  
  6. #define maxn 5010  
  7. using namespace std;  
  8. int s[maxn][maxn], n, r;  
  9. int main () {  
  10.     cin >> n >> r;  
  11.     int N = r, M = r, u, v, w;  
  12.     rep(i, 1, n) {  
  13.         cin >> u >> v >> w;  
  14.         ++ u, ++ v;  
  15.         s[u][v] += w;  
  16.         N = max(N, u), M = max(M, v);  
  17.     }  
  18.     rep(i, 1, N)  
  19.         rep(j, 1, M)  
  20.             s[i][j] += s[i-1][j] + s[i][j-1] - s[i-1][j-1];  
  21.     int years = 0;  
  22.     rep(i, r, N)  
  23.         rep(j, r, M)   
  24.             ans = max(ans, s[i][j] - s[i-r][j] - s[i][j-r] + s[i-r][j-r]);  
  25.     cout << years;  
  26.     return 0;  
  27. }  

Guess you like

Origin www.cnblogs.com/TY02/p/11307584.html