Problem solution · Python

ABC176 E — Bomber

ABC176 E — Bomber: 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
63 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 ABC176 E — Bomber, 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

  • 63 lines of Python from the credited upstream file abc176_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 codeABC176 E — Bomber · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys    input = sys.stdin.readline     h, w, m = map(int, input().split())    h_count = [0 for _ in range(h)]    w_count = [0 for _ in range(w)]    pairs = set()     # KeyInsight:    # x方向とy方向を独立に考える    # 爆破できる数: x方向の最大値 + y方向の最大値    #             x, y方向が交差する場合は-1    for i in range(m):        hi, wi = map(int, input().split())        hi -= 1        wi -= 1         h_count[hi] += 1        w_count[wi] += 1         pairs.add((hi, wi))     # 各方向の最大値を計算    h_max = max(h_count)    w_max = max(w_count)    hk = list()    wk = list()     # 各方向で、最大値となる行もしくは列を抽出    for index, hj in enumerate(h_count):        if hj == h_max:            hk.append(index)     for index, wj in enumerate(w_count):        if wj == w_max:            wk.append(index)     # 答えの候補    ans = h_max + w_max     # 範囲を限定して全探索    # 合計が最大値となる組み合わせを全探索    for _h in hk:        for _w in wk:            hw = (_h, _w)             if hw in pairs:                continue             # 一つでも条件に合致したら、計算を打ち切る            print(ans)            exit()     print(ans - 1)  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 ↗