結果

問題 No.165 四角で囲え!
ユーザー tentententen
提出日時 2023-01-26 17:41:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 489 ms / 5,000 ms
コード長 3,472 bytes
コンパイル時間 3,840 ms
コンパイル使用メモリ 92,456 KB
実行使用メモリ 53,992 KB
最終ジャッジ日時 2023-09-09 17:52:26
合計ジャッジ時間 8,558 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 243 ms
51,324 KB
testcase_01 AC 43 ms
49,852 KB
testcase_02 AC 42 ms
49,436 KB
testcase_03 AC 43 ms
49,432 KB
testcase_04 AC 44 ms
49,400 KB
testcase_05 AC 334 ms
52,988 KB
testcase_06 AC 134 ms
52,924 KB
testcase_07 AC 43 ms
49,336 KB
testcase_08 AC 42 ms
49,508 KB
testcase_09 AC 43 ms
49,464 KB
testcase_10 AC 42 ms
49,480 KB
testcase_11 AC 177 ms
53,236 KB
testcase_12 AC 127 ms
53,336 KB
testcase_13 AC 128 ms
52,932 KB
testcase_14 AC 113 ms
52,856 KB
testcase_15 AC 122 ms
52,888 KB
testcase_16 AC 403 ms
53,216 KB
testcase_17 AC 307 ms
53,152 KB
testcase_18 AC 489 ms
52,868 KB
testcase_19 AC 380 ms
53,992 KB
testcase_20 AC 324 ms
53,044 KB
testcase_21 AC 331 ms
53,100 KB
testcase_22 AC 336 ms
53,508 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