結果
問題 | No.1767 BLUE to RED |
ユーザー |
![]() |
提出日時 | 2021-11-26 23:01:12 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,187 bytes |
コンパイル時間 | 1,904 ms |
コンパイル使用メモリ | 78,248 KB |
実行使用メモリ | 119,848 KB |
最終ジャッジ日時 | 2024-06-29 18:25:48 |
合計ジャッジ時間 | 14,251 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 1 WA * 20 |
ソースコード
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]));}}