计蒜客 T2431 激光炸弹

题目链接:计蒜客 T2431 激光炸弹

题目大意:

题解:
用二维前缀和即可,注意区间是\([0, 5000]\),所以最大要到\(5001\)

#include <algorithm>
#include <iostream>
using namespace std;

int sum[5010][5010];
int n, r, ans;

int main() {
    cin >> n >> r;
    for (int i = 1; i <= n; ++i) {
        int x, y, v;
        cin >> x >> y >> v;
        sum[x + 1][y + 1] += v;
    }
    for (int i = 1; i <= 5001; ++i) {
        for (int j = 1; j <= 5001; ++j) {
            sum[i][j] += sum[i - 1][j];
        }
    }
    for (int i = 1; i <= 5001; ++i) {
        for (int j = 1; j <= 5001; ++j) {
            sum[i][j] += sum[i][j - 1];
        }
    }
    for (int i = r; i <= 5000; ++i) {
        for (int j = r; j <= 5000; ++j) {
            ans = max(ans, sum[i][j] + sum[i - r][j - r] - sum[i][j - r] - sum[i - r][j]);
        }
    }
    cout << ans;
    return 0;
}
posted @ 2021-09-02 20:42  ZZHHOOUU  阅读(47)  评论(0编辑  收藏  举报