AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python

Introduction

This theme

AtCoder Beginner Contest D - Enough Array Difficulty: 859

This theme, cumulative sum + binary search

Problem statement (Condition) The sum of the values of all the elements contained in the continuous subsequence is K or more. You can see from that the cumulative sum is used, but since 1 ≤ N ≤ 100_000, if you turn the loop twice, it becomes TLE. Therefore, since the cumulative sum is a monotonous increase, a binary search is also used to reduce the amount of calculation. Ruby

ruby.rb


n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
acc = 0
x = [0] + a.map{|v| acc += v}
cnt = 0
x.each do |y|
  j = x.bsearch_index{|z| z - y >= k}
  if j == nil
    break
  else
    cnt += n - j + 1
  end
end
puts cnt

accumulate.rb


acc = 0
x = [0] + a.map{|v| acc += v}

The cumulative sum is calculated here. ** Addition ** I modified it to the code you received in the comments.

bsearch.rb


  j = x.bsearch_index{|z| z - y >= k}

Although it is a binary search, lower_bound in *** C ++ *** becomes bsearch or bsearch_index in *** Ruby ***. Python

python.py


from sys import stdin

def main():
    import bisect
    import itertools

    input = stdin.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    x = [0] + list(itertools.accumulate(a))
    cnt = 0
    for z in x:
        j = bisect.bisect_left(x, k + z)
        if j <= n:
            cnt += n - j + 1
    print(cnt)
main()

accumulate.py


    x = [0] + list(itertools.accumulate(a))

*** Python *** has a function called ʻaccumulate` that finds the cumulative sum.

bisect.py


        j = bisect.bisect_left(x, k + z)

It is a binary search, but use bisect or bisect_left bisect_right. If the array contains a number to search for, bisect returns the position to the right of that number and bisect_left returns the position to the left.

for.py


    for z in x:
    for i in range(n):

When I turned the loop with range (), it was TLE.

Ruby Python
Code length(Byte) 238 377
Execution time(ms) 165 101
memory(KB) 11780 14196

Summary

Referenced site

instance method Array#bsearch

Recommended Posts

AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
AtCoder ABC168 A case expression solved in Ruby and Python
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
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, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
AtCoder ABC 182 Python (A ~ D)
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solved AtCoder ABC 114 C-755 with Python3
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Algorithm in Python (ABC 146 C Binary Search
Solve AtCoder ABC168 with python (A ~ D)
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
AtCoder ABC 174 Python
AtCoder ABC187 Python
AtCoder ABC188 Python
AtCoder ABC 175 Python
[Python] Cumulative sum ABC186D
Binary search in Python
[Python] Binary Acing 2020D
Ruby, Python and map
Binary search (python2.7) memo
[Python] Binary search ABC155D
Python and Ruby split
Binary search with python
Binary search with Python3
Binary search in Python (binary search)
[Python] Cumulative sum ABC179D
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
AtCoder ABC151 Problem D Speed comparison in C ++ / Python / PyPy
AtCoder ABC156 Problem D Bouquet mod calculation and permutations, etc.
AtCoder ABC 177 Python (A ~ E)
Python on Ruby and angry Ruby on Python
AtCoder ABC 178 Python (A ~ E)
Atcoder ABC164 A-C in Python
Python and ruby slice memo
Learn cumulative sum in Python
Cumulative sum and potato method
AtCoder ABC 176 Python (A ~ E)
Atcoder ABC167 A-D in Python
Binary search in Python / C ++
Algorithm in Python (binary search)
Solve ABC175 D in Python
Ruby and Python syntax ~ branch ~
Atcoder ABC165 A-D in Python