AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python

Introduction

This theme

AtCoder Beginner Contest C - Tsundoku Difficulty: 878

This theme, cumulative sum + binary search

At first, I thought it was dynamic programming, but I got TLE ~~ a big loss of time ~~, and switched to binary search. In input example 1, in each row of the following table, find the maximum number of desks A and B that do not exceed K minutes.

1 2 3 4 5 6 7
60 90 120 80 150 80 150
60 90 80 150 80 150
60 80 150 80 150
80 150 80 150

Ruby

ruby.rb


n, m, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
c = Array.new(n + 1, 0)
n.times do |i|
  c[i + 1] = c[i] + a[i]
end
d = Array.new(m + 1, 0)
m.times do |i|
  d[i + 1] = d[i] + b[i]
end
cnt = 0
0.upto(n) do |i|
  break if c[i] > k
  e = d.bsearch_index{_1 > k - c[i]}
  if e.nil?
    cnt = i + m if cnt < i + m
  else
    cnt = i + e - 1 if cnt < i + e - 1
  end
end
puts cnt

ruby.rb


c = Array.new(n + 1, 0)
n.times do |i|
  c[i + 1] = c[i] + a[i]
end
d = Array.new(m + 1, 0)
m.times do |i|
  d[i + 1] = d[i] + b[i]
end

Cumulative sum.

ruby.rb


  e = d.bsearch_index{_1 > k - c[i]}
  if e.nil?
    cnt = i + m if cnt < i + m
  else
    cnt = i + e - 1 if cnt < i + e - 1
  end

It is a binary search. If the maximum value of the array is exceeded, nil is returned, so that process is included. Python

python.py


from sys import stdin
import bisect

def main():
    input = stdin.readline
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = [0] * (n + 1)
    for i in range(n):
        c[i + 1] = c[i] + a[i]
    d = [0] * (m + 1)
    for i in range(m):
        d[i + 1] = d[i] + b[i]
    cnt = 0
    for i in range(n + 1):
        if c[i] > k:
            break
        e = bisect.bisect_right(d, k - c[i])
        if cnt < i + e - 1:
            cnt = i + e - 1
    print(cnt)
main()

python.py


        e = bisect.bisect_right(d, k - c[i])
        if cnt < i + e - 1:
            cnt = i + e - 1

It is a binary search. If the maximum value of the array is exceeded, the element number + 1 of the array is returned.

Ruby Python
Code length(Byte) 433 570
Execution time(ms) 349 246
memory(KB) 44860 47636

Summary

Referenced site

Recommended Posts

AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solved AtCoder ABC 114 C-755 with Python3
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
AtCoder ABC168 A case expression solved in Ruby and Python
Algorithm in Python (ABC 146 C Binary Search
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Binary search in Python / C ++
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Socket communication by C language and Python
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
AtCoder ABC 174 Python
AtCoder ABC187 Python
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
AtCoder ABC188 Python
Solving with Ruby AtCoder ABC110 C String Manipulation
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
AtCoder ABC 175 Python
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solve Atcoder ABC176 (A, B, C, E) in Python
[Python] Cumulative sum ABC186D
Binary search in Python
Ruby, Python and map
[Python] Binary search ABC155D
Python and Ruby split
Binary search with python
Binary search with Python3
ABC147 C --HonestOrUnkind2 [Python]
Binary search in Python (binary search)
[Python] Cumulative sum ABC179D
Solved AtCoder ABC 114 C-755 with Python3
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
Mandelbrot Benchmark (C, PHP, HHVM, Ruby, Python, PyPy, and Kinx)
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
AtCoder ABC151 Problem D Speed comparison in C ++ / Python / PyPy
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
AtCoder ABC 177 Python (A ~ E)
C language ALDS1_4_B Binary Search
Python on Ruby and angry Ruby on Python
ABC163 C problem with python3
AtCoder ABC 178 Python (A ~ E)