結果

問題 No.448 ゆきこーだーの雨と雪 (3)
ユーザー 37zigen37zigen
提出日時 2016-11-17 16:14:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,187 ms / 2,000 ms
コード長 2,897 bytes
コンパイル時間 5,693 ms
コンパイル使用メモリ 78,660 KB
実行使用メモリ 71,420 KB
最終ジャッジ日時 2023-08-24 15:48:53
合計ジャッジ時間 32,714 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,272 KB
testcase_01 AC 122 ms
56,048 KB
testcase_02 AC 120 ms
55,784 KB
testcase_03 AC 120 ms
55,636 KB
testcase_04 AC 172 ms
55,996 KB
testcase_05 AC 185 ms
55,696 KB
testcase_06 AC 194 ms
60,344 KB
testcase_07 AC 168 ms
56,092 KB
testcase_08 AC 181 ms
56,460 KB
testcase_09 AC 187 ms
56,032 KB
testcase_10 AC 173 ms
56,400 KB
testcase_11 AC 188 ms
56,388 KB
testcase_12 AC 186 ms
56,376 KB
testcase_13 AC 535 ms
60,500 KB
testcase_14 AC 527 ms
60,628 KB
testcase_15 AC 812 ms
65,032 KB
testcase_16 AC 955 ms
68,712 KB
testcase_17 AC 954 ms
68,912 KB
testcase_18 AC 468 ms
60,200 KB
testcase_19 AC 1,121 ms
71,276 KB
testcase_20 AC 745 ms
64,872 KB
testcase_21 AC 1,069 ms
70,976 KB
testcase_22 AC 753 ms
64,864 KB
testcase_23 AC 1,044 ms
71,064 KB
testcase_24 AC 659 ms
60,504 KB
testcase_25 AC 1,119 ms
71,028 KB
testcase_26 AC 457 ms
60,528 KB
testcase_27 AC 970 ms
68,668 KB
testcase_28 AC 914 ms
65,524 KB
testcase_29 AC 781 ms
65,100 KB
testcase_30 AC 1,076 ms
70,940 KB
testcase_31 AC 504 ms
60,480 KB
testcase_32 AC 552 ms
60,536 KB
testcase_33 AC 1,083 ms
70,580 KB
testcase_34 AC 1,111 ms
71,052 KB
testcase_35 AC 1,187 ms
70,660 KB
testcase_36 AC 1,050 ms
70,748 KB
testcase_37 AC 1,100 ms
70,936 KB
testcase_38 AC 1,063 ms
71,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.*;

public class Q448 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] T = new int[N];
		int[] D = new int[N];
		int max = -1;
		for (int i = 0; i < N; ++i) {
			T[i] = sc.nextInt();
			D[i] = sc.nextInt();
			max = Math.max(max, D[i]);
		}

		int left = -1;
		int right = max;

		outer: while (right - left > 1) {
			int middle = (left + right) / 2;

			int preTime = -K;
			for (int i = 0; i < N; ++i) {
				if (D[i] > middle) {
					if (T[i] - preTime < K) {
						left = middle;
						continue outer;
					} else
						preTime = T[i];
				}
			}

			right = middle;
		}

		max = right;
		System.out.println(max);

		SegTree seg = new SegTree(N);

		int preTime = -K;
		for (int i = 0; i < N; ++i) {
			if (D[i] <= max) {
				long opt = Math.max(seg.query(binarySearchUpper(T, preTime + K), binarySearchLower(T, T[i] - K) + 1),
						seg.query(binarySearchLower(T, preTime), binarySearchLower(T, preTime) + 1));
				seg.setVal(i, opt + D[i]);
			} else {
					seg.setVal(i,
							Math.max(seg.query(binarySearchUpper(T, preTime + K), binarySearchLower(T, T[i] - K) + 1),
									seg.query(binarySearchLower(T, preTime), binarySearchLower(T, preTime) + 1)));

				preTime = T[i];
			}
		}

		long sum = 0;

		for (int i = 0; i < N; ++i) {
			if (D[i] <= max)
				sum += D[i];
		}

		System.out.println(sum - Math.max(seg.query(binarySearchLower(T, preTime), binarySearchLower(T, preTime) + 1),
				seg.query(binarySearchUpper(T, preTime + K), N)));

	}

	static int binarySearchLower(int[] a, int key) {
		int left = -1;
		int right = a.length;
		while (right - left > 1) {
			int middle = (right + left) / 2;
			if (key >= a[middle]) {
				left = middle;
			} else {
				right = middle;
			}
		}
		return left;
	}

	static int binarySearchUpper(int[] a, int key) {
		int left = -1;
		int right = a.length;
		while (right - left > 1) {
			int middle = (right + left) / 2;
			if (key <= a[middle]) {
				right = middle;
			} else {
				left = middle;
			}
		}
		return right;
	}

	static class SegTree {
		int n = 1;
		long[] max;

		public SegTree(int n_) {
			while (n < n_) {
				n *= 2;
			}
			max = new long[2 * n - 1];
		}

		void setVal(int k, long val) {
			k += n - 1;
			max[k] = val;
			while (k > 0) {
				k = (k - 1) / 2;
				max[k] = Math.max(max[2 * k + 1], max[2 * k + 2]);
			}
		}

		long query(int a, int b) {
			return query(a, b, 0, n, 0);
		}

		long query(int a, int b, int l, int r, int k) {
			if (a >= r || b <= l) {
				return 0;
			} else if (a <= l && r <= b) {
				return max[k];
			} else {
				long vl = query(a, b, l, (l + r) / 2, 2 * k + 1);
				long vr = query(a, b, (l + r) / 2, r, 2 * k + 2);
				return Math.max(vl, vr);
			}
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0