結果

問題 No.165 四角で囲え!
ユーザー tentententen
提出日時 2023-01-26 17:41:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 489 ms / 5,000 ms
コード長 3,472 bytes
コンパイル時間 2,499 ms
コンパイル使用メモリ 96,320 KB
実行使用メモリ 55,060 KB
最終ジャッジ日時 2024-06-27 10:30:54
合計ジャッジ時間 8,710 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 289 ms
53,676 KB
testcase_01 AC 53 ms
50,312 KB
testcase_02 AC 54 ms
50,328 KB
testcase_03 AC 52 ms
50,324 KB
testcase_04 AC 53 ms
50,128 KB
testcase_05 AC 337 ms
52,596 KB
testcase_06 AC 141 ms
52,128 KB
testcase_07 AC 54 ms
50,340 KB
testcase_08 AC 52 ms
50,432 KB
testcase_09 AC 53 ms
50,028 KB
testcase_10 AC 54 ms
50,076 KB
testcase_11 AC 196 ms
52,932 KB
testcase_12 AC 141 ms
52,524 KB
testcase_13 AC 144 ms
52,388 KB
testcase_14 AC 123 ms
52,496 KB
testcase_15 AC 163 ms
55,060 KB
testcase_16 AC 484 ms
52,368 KB
testcase_17 AC 430 ms
52,792 KB
testcase_18 AC 489 ms
52,696 KB
testcase_19 AC 369 ms
52,700 KB
testcase_20 AC 338 ms
52,528 KB
testcase_21 AC 337 ms
52,868 KB
testcase_22 AC 317 ms
52,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int b = sc.nextInt();
        int[] xArr = new int[n];
        int[] yArr = new int[n];
        int[] pArr = new int[n];
        TreeMap<Integer, Integer> xCompress = new TreeMap<>();
        TreeMap<Integer, Integer> yCompress = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            xArr[i] = sc.nextInt();
            yArr[i] = sc.nextInt();
            pArr[i] = sc.nextInt();
            xCompress.put(xArr[i], null);
            yCompress.put(yArr[i], null);
        }
        int w = 1;
        for (int x : xCompress.keySet()) {
            xCompress.put(x, w++);
        }
        int h = 1;
        for (int x : yCompress.keySet()) {
            yCompress.put(x, h++);
        }
        int[][] points = new int[h][w];
        int[][] counts = new int[h][w];
        for (int i = 0; i < n; i++) {
            int c = xCompress.get(xArr[i]);
            int r = yCompress.get(yArr[i]);
            points[r][c] += pArr[i];
            counts[r][c]++;
        }
        for (int i = 1; i < h; i++) {
            for (int j = 1; j < w; j++) {
                points[i][j] += points[i - 1][j] + points[i][j - 1] - points[i - 1][j - 1];
                counts[i][j] += counts[i - 1][j] + counts[i][j - 1] - counts[i - 1][j - 1];
            }
        }
        int ans = 0;
        for (int left = 0; left < w - 1; left++) {
            for (int right = left + 1; right < w; right++) {
                int up = 0;
                for (int j = 0; j < h; j++) {
                    while (up < h) {
                        int score = points[up][right] - points[up][left] - points[j][right] + points[j][left];
                        if (score > b) {
                            break;
                        }
                        int sum = counts[up][right] - counts[up][left] - counts[j][right] + counts[j][left];
                        ans = Math.max(ans, sum);
                        up++;
                    }
                }
            }
        }
        System.out.println(ans);
    }
    

}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0