AcWing 99. 激光炸弹 (前缀和)

Problem

这个题蛮有意思,题面保证覆盖范围是一个正方形,战场的长宽在5k以内,可以一次遍历范围,计算区域内总和可以联想到二维前缀和。
然后再处理一些代码中的边界,细节问题即可。

顺便再看一下JVM优化过的Java代码,学习到了静态块的应用以及JVM最基础的优化代码模块。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
    static BufferedReader br;
    static PrintWriter pw;
    static int N;
    static int n;
    static int r;
    static int[][] w;
    static int[][] size;

    public Main() {
    }

    public static void main(String[] args) throws Exception {
        String[] s = br.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        r = Integer.parseInt(s[1]);

        int max;
        int i;
        int j;
        for(max = 1; max <= n; ++max) {
            s = br.readLine().split(" ");
            i = Integer.parseInt(s[0]);
            j = Integer.parseInt(s[1]);
            ++i;
            ++j;
            w[i][j] = Integer.parseInt(s[2]);
        }

        for(max = 1; max <= 5005; ++max) {
            for(i = 1; i <= 5005; ++i) {
                size[max][i] = size[max - 1][i] + size[max][i - 1] - size[max - 1][i - 1] + w[max][i];
            }
        }

        max = -2147483648;

        for(i = r; i <= 5001; ++i) {
            for(j = r; j <= 5001; ++j) {
                max = Math.max(max, size[i][j] - size[i - r][j] - size[i][j - r] + size[i - r][j - r]);
            }
        }

        pw.println(max);
        pw.flush();
        pw.close();
        br.close();
    }

    static {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(System.out);
        N = 5010;
        w = new int[N][N];
        size = new int[N][N];
    }
}

发布了167 篇原创文章 · 获赞 3 · 访问量 3437

猜你喜欢

转载自blog.csdn.net/qq_43515011/article/details/104180272
今日推荐