結果

問題 No.1767 BLUE to RED
コンテスト
ユーザー tomerun
提出日時 2021-11-26 23:01:39
言語 Crystal
(1.19.1)
コンパイル:
crystal build -Donline_judge -o a.out --release --no-debug _filename_
実行:
./a.out
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 565 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 10,415 ms
コンパイル使用メモリ 337,240 KB
実行使用メモリ 33,536 KB
最終ジャッジ日時 2026-03-19 08:29:39
合計ジャッジ時間 11,690 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

n, m = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)
b = read_line.split.map(&.to_i)
ans = 0
bi = 0
if b[0] < a[0]
  ans += a[0] - b[0]
  while bi < m && b[bi] < a[0]
    bi += 1
  end
end
(n - 1).times do |i|
  nbi = bi
  bs = [] of Int32
  while nbi < m && b[nbi] < a[i + 1]
    bs << b[nbi]
    nbi += 1
  end
  if !bs.empty?
    ds = [bs[0] - a[i], a[i + 1] - bs[-1]]
    (bs.size - 1).times do |j|
      ds << bs[j + 1] - bs[j]
    end
    ans += a[i + 1] - a[i] - ds.max
  end
  bi = nbi
end
if b[-1] > a[-1]
  ans += b[-1] - a[-1]
end
puts ans
0