[Luogu] 无线网络发射器选址

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

二维前缀和

#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;
}

 

posted @ 2018-04-27 16:31  xayata  阅读(132)  评论(0编辑  收藏  举报