結果

問題 No.165 四角で囲え!
ユーザー kenkooookenkoooo
提出日時 2015-03-14 02:31:46
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,382 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 75,168 KB
実行使用メモリ 62,124 KB
最終ジャッジ日時 2023-09-11 08:19:07
合計ジャッジ時間 14,869 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import static java.util.Arrays.binarySearch;

import java.io.IOException;
import java.util.TreeSet;

public class Main {

	public static void main(String[] args) {
		int N = nextInt();
		int B = nextInt();
		int[] xs = new int[N];
		int[] ys = new int[N];
		int[] point = new int[N];
		for (int i = 0; i < N; i++) {
			xs[i] = nextInt();
			ys[i] = nextInt();
			point[i] = nextInt();
		}

		TreeSet<Integer> xTreeSet = new TreeSet<Integer>();
		TreeSet<Integer> yTreeSet = new TreeSet<Integer>();

		for (int i = 0; i < N; i++) {
			xTreeSet.add(xs[i]);
			yTreeSet.add(ys[i]);
		}
		xTreeSet.add((int) Integer.MAX_VALUE);
		xTreeSet.add((int) Integer.MIN_VALUE);
		yTreeSet.add((int) Integer.MAX_VALUE);
		yTreeSet.add((int) Integer.MIN_VALUE);

		Integer[] xIntegers = xTreeSet.toArray(new Integer[0]);
		Integer[] yIntegers = yTreeSet.toArray(new Integer[0]);

		int w = xIntegers.length;
		int h = yIntegers.length;

		int[][] treasures = new int[h + 1][w + 1];
		int[][] tPoints = new int[h + 1][w + 1];

		for (int i = 0; i < N; i++) {
			int x = binarySearch(xIntegers, xs[i]);
			int y = binarySearch(yIntegers, ys[i]);
			treasures[y + 1][x + 1]++;
			tPoints[y + 1][x + 1] += point[i];
		}

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				treasures[i + 1][j + 1] += treasures[i + 1][j] + treasures[i][j + 1] - treasures[i][j];
				tPoints[i + 1][j + 1] += tPoints[i + 1][j] + tPoints[i][j + 1] - tPoints[i][j];
			}
		}

		int tMax = 0;
		for (int x1 = 0; x1 < w + 1; x1++) {
			for (int x2 = x1 + 1; x2 < w + 1; x2++) {
				for (int y1 = 0; y1 < h + 1; y1++) {
					for (int y2 = y1 + 1; y2 < h + 1; y2++) {
						int treasureNum = treasures[y2][x2] - treasures[y1][x2] - treasures[y2][x1] + treasures[y1][x1];
						int pointNum = tPoints[y2][x2] - tPoints[y1][x2] - tPoints[y2][x1] + tPoints[y1][x1];
						if (pointNum <= B) {
							tMax = Math.max(tMax, treasureNum);
						}
					}
				}
			}
		}
		System.out.println(tMax);
	}

	static int nextInt() {
		int c;
		try {
			c = System.in.read();
			while (c != '-' && (c < '0' || c > '9'))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			while (c >= '0' && c <= '9') {
				res = res * 10 + c - '0';
				c = System.in.read();
			}
			return res;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

}
0