Problem solution · Python

ABC159 E — Dividing Chocolate

ABC159 E — Dividing Chocolate: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
73 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

Direct simulation

For ABC159 E — Dividing Chocolate, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 73 lines of Python from the credited upstream file abc159_e.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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 codeABC159 E — Dividing Chocolate · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def can_add(j, k, t, group, group_id):    for index in range(group_id):        group[index] += t[index][j]         if group[index] > k:            return False, group     return True, group  def main():    import sys     input = sys.stdin.readline     h, w, k = map(int, input().split())    s = [list(input().rstrip()) for _ in range(h)]    t = [[0 for __ in range(w)] for _ in range(h)]    ans = float("inf")     for bit in range(1 << (h - 1)):        id = [0 for _ in range(h)]        current_id = 0         for j in range(h):            id[j] = current_id             if bit & (1 << j):                current_id += 1         current_id += 1         # Initialize        for wi in range(w):            for hi in range(h):                t[hi][wi] = 0         # Count white choco in each group.        for wi in range(w):            for hi in range(h):                t[id[hi]][wi] += int(s[hi][wi])         ok = True         for wi in range(w):            for hi in range(h):                if t[id[hi]][wi] > k:                    ok = False         if not ok:            continue         candidate = current_id - 1        group = [0 for _ in range(current_id)]         for wj in range(w):            flag, group = can_add(wj, k, t, group, current_id)             if not flag:                candidate += 1                group = [t[i][wj] for i in range(current_id)]         ans = min(ans, candidate)     print(ans)  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 ↗