結果

問題 No.448 ゆきこーだーの雨と雪 (3)
ユーザー 37zigen
提出日時 2016-11-17 17:07:57
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,268 ms / 2,000 ms
コード長 1,864 bytes
コンパイル時間 4,215 ms
コンパイル使用メモリ 78,568 KB
実行使用メモリ 71,948 KB
最終ジャッジ日時 2024-12-13 15:20:19
合計ジャッジ時間 33,991 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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);

		int[] count = new int[N];
		int preTime = -K;
		for (int i = 0; i < N; ++i) {
			if (D[i] > max) {
				preTime = T[i];
			} else {
				if (preTime + K <= T[i]) {
					++count[i];
				}
			}
		}
		preTime = T[N - 1] + K;
		for (int i = N - 1; i >= 0; --i) {
			if (D[i] > max) {
				preTime = T[i];
			} else {
				if (preTime >= T[i] + K) {
					++count[i];
				}
			}
		}

		int[] pendingIndex = new int[N];
		int pos = 0;
		for (int i = 0; i < N; ++i) {
			if (count[i] == 2) {
				pendingIndex[pos++] = i;
			}
		}

		pendingIndex = Arrays.copyOf(pendingIndex, pos);


		long[] dp = new long[pos];
		int pre = -1;
		for (int i = 0; i < pos; ++i) {
			while (pre + 1 < pos && T[pendingIndex[i]] >= T[pendingIndex[pre + 1]] + K) {
				++pre;
			}

			if (i > 0)
				dp[i] = dp[i - 1];
			dp[i] = Math.max(dp[i], D[pendingIndex[i]] + (pre >= 0 ? dp[pre] : 0));
		}
		long sum = 0;
		for (int i = 0; i < N; ++i) {
			if (D[i] <= max) {
				sum += D[i];
			}
		}
		System.out.println(sum - (pos > 0 ? dp[pos - 1] : 0));

	}

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