結果

問題 No.165 四角で囲え!
ユーザー tentententen
提出日時 2021-05-19 12:38:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 549 ms / 5,000 ms
コード長 2,746 bytes
コンパイル時間 2,307 ms
コンパイル使用メモリ 79,628 KB
実行使用メモリ 41,580 KB
最終ジャッジ日時 2024-04-17 23:07:42
合計ジャッジ時間 8,063 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 207 ms
40,540 KB
testcase_01 AC 55 ms
37,340 KB
testcase_02 AC 54 ms
36,988 KB
testcase_03 AC 54 ms
37,092 KB
testcase_04 AC 54 ms
37,076 KB
testcase_05 AC 304 ms
40,696 KB
testcase_06 AC 148 ms
39,764 KB
testcase_07 AC 55 ms
37,340 KB
testcase_08 AC 54 ms
37,116 KB
testcase_09 AC 55 ms
36,984 KB
testcase_10 AC 54 ms
37,080 KB
testcase_11 AC 215 ms
40,288 KB
testcase_12 AC 136 ms
40,148 KB
testcase_13 AC 137 ms
40,144 KB
testcase_14 AC 126 ms
39,776 KB
testcase_15 AC 130 ms
39,852 KB
testcase_16 AC 328 ms
40,912 KB
testcase_17 AC 253 ms
40,812 KB
testcase_18 AC 549 ms
41,200 KB
testcase_19 AC 457 ms
41,148 KB
testcase_20 AC 266 ms
41,580 KB
testcase_21 AC 264 ms
41,532 KB
testcase_22 AC 250 ms
41,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int b = sc.nextInt();
        TreeMap<Integer, Integer> compX = new TreeMap<>();
        TreeMap<Integer, Integer> compY = new TreeMap<>();
        Point[] points = new Point[n];
        for (int i = 0; i < n; i++) {
            points[i] = new Point(sc.nextInt(), sc.nextInt(), sc.nextInt());
            compX.put(points[i].x, null);
            compY.put(points[i].y, null);
        }
        int xx = 1;
        for (int x : compX.keySet()) {
            compX.put(x, xx);
            xx++;
        }
        int yy = 1;
        for (int x : compY.keySet()) {
            compY.put(x, yy);
            yy++;
        }
        int[][] scores = new int[xx][yy];
        int[][] counts = new int[xx][yy];
        for (Point p : points) {
            scores[compX.get(p.x)][compY.get(p.y)] += p.value;
            counts[compX.get(p.x)][compY.get(p.y)] ++;
        }
        for (int i = 1; i < xx; i++) {
            for (int j = 1; j < yy; j++) {
                scores[i][j] +=  scores[i - 1][j] + scores[i][j - 1] - scores[i - 1][j - 1];
                counts[i][j] +=  counts[i - 1][j] + counts[i][j - 1] - counts[i - 1][j - 1];
            }
        }
        int max = 0;
        for (int i = 1; i < xx; i++) {
            for (int j = i; j < xx; j++) {
                int right = 1;
                for (int k = 1; k < yy; k++) {
                    while (right < yy && scores[j][right] - scores[j][k - 1] - scores[i - 1][right] + scores[i - 1][k - 1] <= b) {
                        max = Math.max(max, counts[j][right] - counts[j][k - 1] - counts[i - 1][right] + counts[i - 1][k - 1]);
                        right++;
                    }
                }
            }
        }
        System.out.println(max);
    }
    
    static class Point {
        int x;
        int y;
        int value;
        
        public Point(int x, int y, int value) {
            this.x = x;
            this.y = y;
            this.value = value;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0