結果

問題 No.1767 BLUE to RED
ユーザー ks2mks2m
提出日時 2021-11-26 23:09:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 919 ms / 2,000 ms
コード長 1,407 bytes
コンパイル時間 2,856 ms
コンパイル使用メモリ 75,292 KB
実行使用メモリ 123,700 KB
最終ジャッジ日時 2023-09-12 05:32:33
合計ジャッジ時間 17,062 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,644 KB
testcase_01 AC 44 ms
49,136 KB
testcase_02 AC 43 ms
49,388 KB
testcase_03 AC 44 ms
49,620 KB
testcase_04 AC 56 ms
49,564 KB
testcase_05 AC 63 ms
50,904 KB
testcase_06 AC 49 ms
49,096 KB
testcase_07 AC 77 ms
51,588 KB
testcase_08 AC 76 ms
51,616 KB
testcase_09 AC 762 ms
86,260 KB
testcase_10 AC 919 ms
95,400 KB
testcase_11 AC 777 ms
79,512 KB
testcase_12 AC 876 ms
86,720 KB
testcase_13 AC 722 ms
112,800 KB
testcase_14 AC 768 ms
112,168 KB
testcase_15 AC 770 ms
122,052 KB
testcase_16 AC 802 ms
123,700 KB
testcase_17 AC 790 ms
114,548 KB
testcase_18 AC 814 ms
122,100 KB
testcase_19 AC 850 ms
118,268 KB
testcase_20 AC 766 ms
114,800 KB
testcase_21 AC 723 ms
114,204 KB
testcase_22 AC 815 ms
121,540 KB
testcase_23 AC 807 ms
115,776 KB
権限があれば一括ダウンロードができます

ソースコード

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;

			long v2 = set.ceiling(b[i]) - b[i];
			dp2[i] += v2;
			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]);
				} else {
					dp1[i] = Math.min(dp1[i], dp2[i - 1] + v1);
					dp2[i] = Math.min(dp2[i], dp2[i - 1] + v2);
				}
			}
		}
//		System.out.println(Arrays.toString(dp1));
//		System.out.println(Arrays.toString(dp2));
		System.out.println(Math.min(dp1[m - 1], dp2[m - 1]));
	}
}
0