Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue

Introduction

This theme

This theme, priority queue

Problems such as the advanced version of * Solved in Ruby, Python and Java AtCoder ABC141 D Priority Queuing *.

Queues the maximum and second maximum of a given array, subtracts 1 and puts it back in the queue. This is repeated until the queue is filled with one or less, and the loop is exited.

There is also a mathematical solution.

Ruby mathematical solution

ruby.rb


k = gets.to_i
a = gets.split.map(&:to_i).max
puts [0, 2 * a - k - 1].max

I will pay attention to the maximum value and solve it, but the refreshing feeling is amazing. Ruby Heap

ruby.rb


class Heap
  attr_reader :size
  def initialize(up: false)
    @up = up
    @heap = []
    @size = 0
  end
  def sum
    x = 0
    @size.times do |i|
      x += @heap[i]
    end
    x
  end
  def push(n)
    n = -n if @up
    i = @size
    while i > 0
      pid = (i - 1) / 2
      break if n >= @heap[pid]
      @heap[i] = @heap[pid]
      i = pid
    end
    @heap[i] = n
    @size += 1
  end
  def pop
    return nil if @size == 0
    top = @heap[0]
    @size -= 1
    n = @heap[@size]
    i = 0
    while i * 2 + 1 < @size
      cid1 = i * 2 + 1
      cid2 = cid1 + 1
      if cid2 < @size && @heap[cid2] < @heap[cid1]
        cid1 = cid2
      end
      break if @heap[cid1] >= n
      @heap[i] = @heap[cid1]
      i = cid1
    end
    @heap[i] = n
    if @up
      -top
    else
      top
    end
  end
end

gets
a = gets.split.map(&:to_i)
h = Heap.new(up: true)
a.each do |x|
  h.push(x)
end
while h.size > 1
  u = h.pop
  v = h.pop
  h.push(u - 1) if u - 1 > 0
  h.push(v - 1) if v - 1 > 0
end
if h.size == 0
  puts "0"
else
  puts h.pop - 1
end

up.rb


  def initialize(up: false)
    @up = up

heap.rb


while h.size > 1
  u = h.pop
  v = h.pop
  h.push(u - 1) if u - 1 > 0
  h.push(v - 1) if v - 1 > 0
end

Take two, subtract them, and if they are 1 or more, put them back in the queue.

Python mathematical solution

python.py


from sys import stdin

def main():
    input = stdin.readline
    k, _ = map(int, input().split())
    a = max(map(int, input().split()))
    print(max(0, 2 * a - k - 1))
main()

Python heapq

python.py


from sys import stdin

def main():
    import heapq
    input = stdin.readline
    input()
    a = [-1 * int(i) for i in input().split()]
    heapq.heapify(a)
    while len(a) > 1:
        u = heapq.heappop(a)
        v = heapq.heappop(a)
        if u + 1 < 0:
            heapq.heappush(a, u + 1)
        if v + 1 < 0:
            heapq.heappush(a, v + 1)
    if len(a) == 0:
        print(0)
    else:
        print(abs(a[0] + 1))
main()

If the operation is complicated, it is better to prepare a function for code conversion.

Ruby mathematical solution Ruby Heap Python mathematical solution Python heapq
Code length(Byte) 76 1120 184 458
Execution time(ms) 7 26 17 22
memory(KB) 1788 1788 2940 3064

Summary

Referenced site

Recommended Posts

Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solving with Ruby AtCoder ABC110 C String Manipulation
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
Scraping with Node, Ruby and Python
Solved AtCoder ABC 114 C-755 with Python3
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
Encrypt with Ruby (Rails) and decrypt with Python
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
Easy web scraping with Python and Ruby
RaspberryPi L Chika with Python and C #
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
Solving the Lorenz 96 model with Julia and Python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
AtCoder ARC080 D simulation solved in Ruby and Python
Benchmark for C, Java and Python with prime factorization
Algorithm learned with Python 18th: Sorting (stack and queue)
Interprocess communication between Ruby and Python (POSIX message queue)
Solve Atcoder ABC176 (A, B, C, E) in Python
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
Solve AtCoder 167 with python
Ruby, Python and map
Python and Ruby split