Problem solution · Python

ABC269 E — Last Rook

ABC269 E — Last Rook: 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
40 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 ABC269 E — Last Rook, 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

  • 40 lines of Python from the credited upstream file abc269_e.py.
  • The implementation keeps its working state in language-native values and containers.
  • 1 loop block 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 codeABC269 E — Last Rook · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys     input = sys.stdin.readline     n = int(input())    x, y = 1, 1     # 各軸に対して二分探索    for ti in range(2):        wa, ac = 0, n         while (wa + 1) < ac:            wj = (ac + wa) // 2             if ti == 0:                print("?", 1, wj, 1, n, flush=True)            else:                print("?", 1, n, 1, wj, flush=True)             result = int(input())             if result != wj:                ac = wj            else:                wa = wj                    x = ac        x, y = y, x     print("!", x, y, flush=True)   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 ↗