結果

問題 No.1767 BLUE to RED
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-11-27 00:06:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 331 ms / 2,000 ms
コード長 679 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 86,888 KB
実行使用メモリ 139,192 KB
最終ジャッジ日時 2023-09-12 05:44:52
合計ジャッジ時間 7,290 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,048 KB
testcase_01 AC 74 ms
71,168 KB
testcase_02 AC 73 ms
71,132 KB
testcase_03 AC 74 ms
71,408 KB
testcase_04 AC 90 ms
75,148 KB
testcase_05 AC 78 ms
75,004 KB
testcase_06 AC 74 ms
71,364 KB
testcase_07 AC 80 ms
76,248 KB
testcase_08 AC 81 ms
76,420 KB
testcase_09 AC 231 ms
109,556 KB
testcase_10 AC 268 ms
114,612 KB
testcase_11 AC 240 ms
111,888 KB
testcase_12 AC 236 ms
109,328 KB
testcase_13 AC 290 ms
136,884 KB
testcase_14 AC 327 ms
138,848 KB
testcase_15 AC 327 ms
138,732 KB
testcase_16 AC 330 ms
138,952 KB
testcase_17 AC 331 ms
138,752 KB
testcase_18 AC 321 ms
138,660 KB
testcase_19 AC 330 ms
138,860 KB
testcase_20 AC 331 ms
138,640 KB
testcase_21 AC 326 ms
139,192 KB
testcase_22 AC 328 ms
138,828 KB
testcase_23 AC 330 ms
138,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [(i, 0) for i in a]
b = [(i, 1) for i in b]
l = sorted(a + b)
que = []
INF = 10 ** 18
cur = -INF
ans = 0
for i, c in l:
	if c == 0:
		if not que: cur = i; continue
		if len(que) == 1: ans += min(i - min(que), max(que) - cur); cur = i; que = []; continue
		cost = min(max(que) - cur, i - min(que))
		mal, mir = -INF, que[0]
		for j in range(len(que)):
			mal = que[j]
			mir = que[j + 1] if j + 1 < len(que) else -INF
			cost = min(cost, (mal - cur) + (i - mir))
		ans += cost
		cur = i
		que = []
	else:
		que.append(i)
if que: ans += max(que) - cur; que = []
print(ans)
0