結果

問題 No.165 四角で囲え!
ユーザー GrenacheGrenache
提出日時 2016-06-24 22:50:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 459 ms / 5,000 ms
コード長 3,389 bytes
コンパイル時間 4,116 ms
コンパイル使用メモリ 80,900 KB
実行使用メモリ 46,840 KB
最終ジャッジ日時 2024-06-07 19:11:37
合計ジャッジ時間 9,345 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 251 ms
41,056 KB
testcase_01 AC 60 ms
37,228 KB
testcase_02 AC 60 ms
37,368 KB
testcase_03 AC 58 ms
37,272 KB
testcase_04 AC 59 ms
37,272 KB
testcase_05 AC 290 ms
41,496 KB
testcase_06 AC 152 ms
40,468 KB
testcase_07 AC 59 ms
37,436 KB
testcase_08 AC 57 ms
37,392 KB
testcase_09 AC 57 ms
37,088 KB
testcase_10 AC 58 ms
37,276 KB
testcase_11 AC 193 ms
41,036 KB
testcase_12 AC 148 ms
40,444 KB
testcase_13 AC 153 ms
40,780 KB
testcase_14 AC 135 ms
40,144 KB
testcase_15 AC 136 ms
40,508 KB
testcase_16 AC 373 ms
46,840 KB
testcase_17 AC 308 ms
41,304 KB
testcase_18 AC 459 ms
41,684 KB
testcase_19 AC 341 ms
41,568 KB
testcase_20 AC 313 ms
41,748 KB
testcase_21 AC 210 ms
41,560 KB
testcase_22 AC 202 ms
41,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder165_2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int n = sc.nextInt();
        int b = sc.nextInt();

        int[] x = new int[n];
        int[] y = new int[n];
        int[] p = new int[n];
        SortedSet<Integer> tsx = new TreeSet<Integer>();
        SortedSet<Integer> tsy = new TreeSet<Integer>();
        int m = 0;
        for (int i = 0; i < n; i++) {
        	x[i] = sc.nextInt();
        	y[i] = sc.nextInt();
        	p[i] = sc.nextInt();

        	tsx.add(x[i]);
        	tsy.add(y[i]);
        	m++;
        }

        if (m == 0) {
        	pr.println(0);

        	pr.close();
        	sc.close();
        	return;
        }

        Map<Integer, Integer> hmx = new HashMap<>();
        Map<Integer, Integer> hmy = new HashMap<>();
        int ix = 1;
        for (int e : tsx) {
        	hmx.put(e, ix++);
        }
        int iy = 1;
        for (int e : tsy) {
        	hmy.put(e, iy++);
        }

        int cx = tsx.size();
        int cy = tsy.size();
        int[][] xy = new int[cx + 1][cy + 1];
        int[][] xyc = new int[cx + 1][cy + 1];
        for (int i = 0; i < n; i++) {
        	xy[hmx.get(x[i])][hmy.get(y[i])] = p[i];
        	xyc[hmx.get(x[i])][hmy.get(y[i])] = 1;
        }

        for (int i = 1; i <= cy; i++) {
        	for (int j = 1; j <= cx; j++) {
        		xy[j][i] += xy[j - 1][i];
        		xyc[j][i] += xyc[j - 1][i];
        	}
        }
    	for (int j = 1; j <= cx; j++) {
    		for (int i = 1; i <= cy; i++) {
        		xy[j][i] += xy[j][i - 1];
        		xyc[j][i] += xyc[j][i - 1];
        	}
        }

//        pr.printf("%d %d\n", cx, cy);

        int max = 0;
        for (int x1 = 0; x1 <= cx; x1++) {
        	for (int x2 = x1 + 1; x2 <= cx; x2++) {
        		int y1 = 0;
        		int y2 = 1;
        		while (y2 <= cy) {
        			int point = xy[x2][y2] - xy[x2][y1] - xy[x1][y2] + xy[x1][y1];

        			if (point <= b) {
        				int cnt = xyc[x2][y2] - xyc[x2][y1] - xyc[x1][y2] + xyc[x1][y1];
        				max = Math.max(max, cnt);

        				y2++;
        			} else {
        				y1++;
        			}
        		}
        	}
        }

        pr.println(max);

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0