結果

問題 No.1767 BLUE to RED
ユーザー ks2mks2m
提出日時 2021-11-26 23:01:12
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,187 bytes
コンパイル時間 3,713 ms
コンパイル使用メモリ 74,676 KB
実行使用メモリ 123,724 KB
最終ジャッジ日時 2023-09-12 05:26:10
合計ジャッジ時間 17,996 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		sa = br.readLine().split(" ");
		TreeSet<Long> set = new TreeSet<>();
		for (int i = 0; i < n; i++) {
			set.add(Long.parseLong(sa[i]));
		}
		sa = br.readLine().split(" ");
		long[] b = new long[m];
		for (int i = 0; i < m; i++) {
			b[i] = Long.parseLong(sa[i]);
		}
		br.close();

		long inf = 2000000007;
		set.add(-inf);
		set.add(inf);
		long[] dp1 = new long[m];
		long[] dp2 = new long[m];
		for (int i = 0; i < m; i++) {
			long v1 = b[i] - set.floor(b[i]);
			if (i > 0) {
				dp1[i] = dp1[i - 1];
				v1 = Math.min(v1, b[i] - b[i - 1]);
			}
			dp1[i] += v1;

			dp2[i] += set.ceiling(b[i]) - b[i];
			if (i > 0) {
				dp2[i] += dp1[i - 1];
				if (set.ceiling(b[i - 1]) == set.ceiling(b[i])) {
					dp2[i] = Math.min(dp2[i], dp2[i - 1]);
				}
			}
		}
		System.out.println(Math.min(dp1[m - 1], dp2[m - 1]));
	}
}
0