結果

問題 No.448 ゆきこーだーの雨と雪 (3)
ユーザー 37zigen37zigen
提出日時 2016-11-17 15:44:43
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,831 bytes
コンパイル時間 3,327 ms
コンパイル使用メモリ 78,148 KB
実行使用メモリ 63,544 KB
最終ジャッジ日時 2024-11-26 03:01:39
合計ジャッジ時間 33,290 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
40,664 KB
testcase_01 AC 118 ms
41,100 KB
testcase_02 AC 119 ms
41,160 KB
testcase_03 AC 122 ms
41,280 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 172 ms
41,912 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 929 ms
58,528 KB
testcase_16 AC 1,056 ms
61,016 KB
testcase_17 AC 1,064 ms
62,252 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1,147 ms
62,700 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 905 ms
58,204 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 509 ms
47,060 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 1,158 ms
61,624 KB
testcase_37 WA -
testcase_38 AC 1,162 ms
61,712 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;
					}
					preTime = T[i];
				}
			}

			right = middle;
		}

		max = right;

		System.out.println(max);
		// SegTree seg = new SegTree(T[N - 1] + 1);
		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(binarySearchUpper(T, preTime), binarySearchUpper(T, preTime) + 1));
				seg.setVal(i, opt + D[i]);
			} else {
				seg.setVal(i, seg.query(binarySearchUpper(T, preTime + K), binarySearchLower(T, T[i] - K) + 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(binarySearchUpper(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