[Luogu] Wireless network transmitter site selection

https://www.luogu.org/problemnew/show/P2038

2D prefix sum

#include <iostream>
#include <cstdio>

using namespace std;
const int N = 150;
const int n = 129;

int A[N][N];
int d, T;

#define gc getchar()

inline int read() {
    int x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}

int main() {
    d = read(); T = read();
    for(int i = 1; i <= T; i ++) {
        int x = read(), y = read(), w = read();
        A[x + 1][y + 1] = w;
    }
    for(int i = 1; i <= n; 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 js(0), Max(0);
    for(int i = 1; i <= n; i ++) {
        for(int j = 1; j <= n; j ++) {
            int x_1 = max(0, i - d - 1), y_1 = max(0, j - d - 1), x_2 = min(n, i + d), y_2 = min(n, j + d);
            int W = A[x_2][y_2] - A[x_2][y_1] - A[x_1][y_2] + A[x_1][y_1];
            if(W > Max) {
                Max = W; js = 1;
            } else if(W == Max) js ++;
        }
    }
    cout << js << " " << Max;
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324965552&siteId=291194637