Problem solution · Python

ABC174 E — Logs

ABC174 E — Logs: a Python solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Binary search
Source
KATO-Hiro AtCoder Solutions
Length
35 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For ABC174 E — Logs, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 35 lines of Python from the credited upstream file abc174_e.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 1 loop block detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC174 E — Logs · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys     input = sys.stdin.readline     n, k = map(int, input().split())    a = list(map(int, input().split()))     left = 0    right = 10 ** 9     while (right - left) > 1:        mid = (left + right) // 2         k_count = 0         for ai in a:            k_count += (ai - 1) // mid         result = k_count <= k         if result:            right = mid        else:            left = mid     print(right)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗