公式

D - 魚捕り / Catching Fish 解説 by admin

Claude 4.6 Opus (Thinking)

Overview

In this problem, we are given a plane with \(N\) fish and want to maximize the number of fish caught by placing a \(W \times H\) rectangular net once. Although the net can be placed at any real coordinates, we need an efficient way to search for the optimal placement.

Analysis

Key Insight: We Can Align the Net’s Edges with the Coordinates of the Fish

Since the net can be placed at any real coordinates, at first glance, it might seem necessary to test infinitely many placements. However, the following observation is key:

As we shift the net slightly to the right, the number of fish inside changes only at the exact moments when a fish passes through the left or right edge of the net.

In other words, it is sufficient to examine only placements where the \(x\)-coordinate of the net’s left edge matches the \(x\)-coordinate of some fish. Similarly, it is sufficient to examine only placements where the \(y\)-coordinate of the net’s bottom edge matches the \(y\)-coordinate of some fish.

Intuitively, aligning the left edge of the net exactly with a fish corresponds to a placement where the fish is “just barely included”; shifting it further left loses that fish, and shifting it right does not gain anything until another fish enters.

Naive Approach

If we exhaustively search all pairs of fish \(x\)-coordinates (at most \(N\) choices) and \(y\)-coordinates (at most \(N\) choices), and check all \(N\) fish for each placement, the time complexity becomes \(O(N^3)\). For \(N = 3000\), this requires around \(2.7 \times 10^{10}\) operations, which will result in TLE.

Optimization

When an \(x\)-coordinate \(a\) is fixed, we can extract only the fish that satisfy the \(x\)-condition \(a \leq x \leq a + W\) and sort their \(y\)-coordinates. Then, the number of fish satisfying the \(y\)-condition \(b \leq y \leq b + H\) can be found in \(O(\log N)\) using binary search. This reduces the overall computational complexity.

Algorithm

  1. Create a list xs of the fish’s \(x\)-coordinates with duplicates removed.
  2. For each \(a \in\) xs (aligning the left edge of the net with \(x = a\)):
    • Iterate through all fish, collect the \(y\)-coordinates of fish that satisfy \(a \leq X_i \leq a + W\), and sort them (list ys_in).
    • For each unique \(y\)-coordinate \(b\) in ys_in (aligning the bottom edge of the net with \(y = b\)):
      • Count the number of elements in ys_in satisfying \(b \leq y \leq b + H\) using binary search (bisect_left and bisect_right).
      • If this count exceeds the current maximum, update it.
  3. Output the maximum value.

Concrete Example: Suppose fish are at \((1,2), (2,5), (3,3)\) and \(W=2, H=3\). If we choose \(a=1\), then \(x \in [1,3]\), so all 3 fish are candidates. Among the \(y\)-coordinates \([2,3,5]\), choosing \(b=2\) gives \(y \in [2,5]\), allowing all 3 fish to be caught.

Complexity

  • Time Complexity: \(O(N^2 \log N)\)
    • There are at most \(N\) unique \(x\)-coordinates. For each \(x\)-coordinate, extracting the fish takes \(O(N)\) and sorting them takes \(O(N \log N)\). After that, a binary search taking \(O(\log N)\) is performed for each unique \(y\)-coordinate (at most \(N\)).
  • Space Complexity: \(O(N)\)

When \(N = 3000\), \(3000^2 \times \log 3000 \approx 1 \times 10^8\) operations, which easily runs within the time limit.

Implementation Details

  • Coordinate compression is unnecessary: We can use the actual coordinates of the fish directly and count the elements using binary search.

  • Fish on the boundary of the net are also caught: Note that the condition is \(a \leq x \leq a+W\) (inclusive). Since bisect_right(ys_in, b + H) returns the rightmost insertion point of elements less than or equal to \(b+H\), fish located on the boundary are counted correctly.

  • Fast I/O: Reading the entire input at once using sys.stdin.buffer.read() allows for fast input processing even in Python.

    Source Code

import sys
from bisect import bisect_left, bisect_right

def main():
    data = sys.stdin.buffer.read().split()
    idx = 0
    N = int(data[idx]); idx += 1
    W = int(data[idx]); idx += 1
    H = int(data[idx]); idx += 1
    
    fish = []
    for i in range(N):
        x = int(data[idx]); idx += 1
        y = int(data[idx]); idx += 1
        fish.append((x, y))
    
    xs = sorted(set(f[0] for f in fish))
    
    ans = 0
    
    for a in xs:
        ys_in = []
        for x, y in fish:
            if a <= x <= a + W:
                ys_in.append(y)
        ys_in.sort()
        
        if not ys_in:
            continue
        
        unique_ys = sorted(set(ys_in))
        
        for b in unique_ys:
            lo = bisect_left(ys_in, b)
            hi = bisect_right(ys_in, b + H)
            cnt = hi - lo
            if cnt > ans:
                ans = cnt
    
    print(ans)

main()

This editorial was generated by claude4.6opus-thinking.