結果

問題 No.3430 Flip the Grid
コンテスト
ユーザー tomerun
提出日時 2026-01-11 15:16:02
言語 Crystal
(1.18.2)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 953 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 15,550 ms
コンパイル使用メモリ 335,564 KB
実行使用メモリ 29,304 KB
最終ジャッジ日時 2026-01-11 15:16:20
合計ジャッジ時間 16,180 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

h, w = read_line.split.map(&.to_i)
a = Array.new(h) { read_line.split.map(&.to_i) }
top = Array.new(w, 0)
ans = 0
(h - 1).times do |y|
  ps = w.times.select { |i| a[y][i] == 1 }.to_a
  0.step(to: ps.size - 2, by: 2) do |i|
    a[y + 1][ps[i]] ^= 1
    a[y + 1][ps[i + 1]] ^= 1
  end
  if ps.size % 2 == 1
    top[ps[-1]] += 1
  end
end
free = 0
w.times do |x|
  free += top[x] // 2
  top[x] %= 2
end
bottom = w.times.select { |i| a[h - 1][i] == 1 }.to_a
ans = 0
while bottom.size > 1
  if top[bottom.last] > 0
    top[bottom.last] -= 1
    bottom.pop
    top[bottom.last] += 1
    if top[bottom.last] == 2
      free += 1
      top[bottom.last] = 0
    end
    bottom.pop
  elsif free > 0
    free -= 1
    top[bottom.last] = 1
    bottom.pop
    top[bottom.last] += 1
    if top[bottom.last] == 2
      free += 1
      top[bottom.last] = 0
    end
    bottom.pop
  else
    ans += 1
    bottom.pop
  end
end
puts ans + top.sum + free * 2 + bottom.size
0