結果

問題 No.9 モンスターのレベル上げ
ユーザー vjudge1
提出日時 2025-09-05 09:59:26
言語 Crystal
(1.14.0)
結果
AC  
実行時間 409 ms / 5,000 ms
コード長 2,034 bytes
コンパイル時間 13,628 ms
コンパイル使用メモリ 311,348 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-05 09:59:46
合計ジャッジ時間 18,554 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

# Custom Min-Heap based Priority Queue implementation
class MinHeap(T)
  @heap = [] of T

  def push(value : T)
    @heap << value
    bubble_up(@heap.size - 1)
  end

  def pop : T
    if @heap.size == 1
      return @heap.pop
    end
    
    min = @heap[0]
    @heap[0] = @heap.pop
    bubble_down(0)
    min
  end

  def top : T
    @heap[0]
  end

  def empty? : Bool
    @heap.empty?
  end

  def size : Int32
    @heap.size
  end

  def clone : MinHeap(T)
    new_heap = MinHeap(T).new
    new_heap.initialize_copy(@heap.clone)
    new_heap
  end

  protected def initialize_copy(other : Array(T))
    @heap = other
  end

  private def bubble_up(index)
    return if index <= 0
    parent = (index - 1) // 2
    
    if @heap[index] < @heap[parent]
      @heap[index], @heap[parent] = @heap[parent], @heap[index]
      bubble_up(parent)
    end
  end

  private def bubble_down(index)
    left = 2 * index + 1
    right = 2 * index + 2
    smallest = index

    if left < @heap.size && @heap[left] < @heap[smallest]
      smallest = left
    end
    
    if right < @heap.size && @heap[right] < @heap[smallest]
      smallest = right
    end
    
    if smallest != index
      @heap[index], @heap[smallest] = @heap[smallest], @heap[index]
      bubble_down(smallest)
    end
  end
end

# Main program with your input format
n = gets.not_nil!.to_i
a_input = gets.not_nil!.split.map(&.to_i)
b = gets.not_nil!.split.map(&.to_i)

# Create a min-heap of tuples (value, count)
heap = MinHeap(Tuple(Int32, Int32)).new

# Initialize priority queue with first array values
a_input.each do |value|
  heap.push({value, 0})  # (value, count)
end

ans = 10**9  # Large number

n.times do |i|
  t = 0
  ta = heap.clone  # Clone the priority queue for this iteration
  
  n.times do |j|
    p = ta.pop
    # Add b[(i+j)%n]/2 to the value and increment count
    new_value = p[0] + b[(i + j) % n] // 2
    new_count = p[1] + 1
    t = Math.max(t, new_count)
    ta.push({new_value, new_count})
  end
  
  ans = Math.min(ans, t)
end

puts ans
0