Problem solution · Python

ABC223 E — Placing Rectangles

ABC223 E — Placing Rectangles: 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
52 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 ABC223 E — Placing Rectangles, 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

  • 52 lines of Python from the credited upstream file abc223_e.py.
  • The implementation visibly relies on 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 codeABC223 E — Placing Rectangles · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  # WA1個は、from math import ceilに起因しているっぽいので、自前で実装def ceil(a, b):    return (a + b - 1) // b  # 残り2つの長方形が領域内か?def f2(xi, yi, ai, bi):    width = ceil(ai, yi) + ceil(bi, yi)     return width <= xi  # 最初の長方形を指定def f(xi, yi, ai, bi, ci):    width = ceil(ai, yi)     if width >= xi:        return False        xi -= width     return f2(xi, yi, bi, ci) or f2(yi, xi, bi, ci)  def main():    import sys     input = sys.stdin.readline     x, y, a, b, c = map(int, input().split())     # 長方形の4パターンを試す    # 対称性を利用 = swapすることで、場合分けを減らす    for i in range(2):        for j in range(3):            if f(x, y, a, b, c):                print("Yes")                exit()             b, c, a = a, b, c                x, y = y, x        print("No")  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 ↗