Official

D - 魚捕り / Catching Fish Editorial by admin Editorial for printing

gpt-5.3-codex

Overview

This problem asks us to find the maximum number of fish that can be caught by translating a rectangular net.
We can solve this efficiently by fixing candidate values for the “leftmost \(x\)-coordinate” and optimizing the \(y\)-direction using the two-pointer technique.

Analysis

First, although the net can be placed at real-valued coordinates, when considering an optimal solution, it is sufficient to align the left edge \(a\) with the \(x\)-coordinate of some fish.
The reason is that when catching fish in a certain configuration, shifting the net slightly to the left does not change the set of caught fish (until a new fish reaches the boundary on the left), so we can eventually shift it until it “touches the \(x\)-coordinate of a fish”.

Similarly, one might think of shifting the boundary in the \(y\)-direction as well, but testing all pairs \((a,b)\) results in too many combinations.


Naively:

  • Enumerate all candidate \((a,b)\) pairs (derived from the fish coordinates), and
  • Count the number of fish inside the rectangle each time.

In this approach, there are up to \(N^2\) candidates, and checking each takes \(O(N)\) time, resulting in an overall time complexity of \(O(N^3)\), which is too slow for \(N=3000\).


Therefore, we use the following idea:

  1. Enumerate all candidates for the left edge \(a=lx\) from the \(x\)-coordinates of the fish (at most \(N\) candidates).
  2. Extract only the fish that can possibly fall inside the net (\(lx \le x \le lx+W\)).
  3. Sort the sequence of \(y\)-coordinates of the extracted fish.
  4. Find the “maximum number of fish that fit within height \(H\)” using a two-pointer approach on \(y\) in \(O(M)\) time (where \(M\) is the length of this sequence).

This decomposes the “2D problem” into “fixing \(x\) + 1D interval maximum count problem”.

Algorithm

  1. Store the fish coordinates in an array fish.
  2. Create a sorted array xs of the unique \(x\)-coordinates of the fish.
  3. For each lx in xs:
    • rx = lx + W
    • Collect the \(y\)-coordinates of the fish satisfying the condition lx <= x <= rx into y_in
    • Sort y_in
    • Two-pointer method:
      • Left pointer l=0
      • Move the right pointer r from 0 to len(y_in)-1
      • While y_in[r] - y_in[l] > H, advance l
      • The interval length r-l+1 at that moment is the maximum number of fish that can be included with that r
    • Update the overall answer with the maximum value found for this lx
  4. Output the maximum value over all lx.

The two-pointer approach works because if y_in is sorted in ascending order, advancing the left pointer whenever the current interval exceeds the height constraint ensures that both pointers move monotonically, taking only linear time.

Complexity

  • Time Complexity: \(O(N^2 \log N)\)
    (For each \(lx\), filtering takes \(O(N)\), sorting takes \(O(M\log M)\), and the two-pointer sweep takes \(O(M)\). In the worst case \(M=N\), repeated \(N\) times.)
  • Space Complexity: \(O(N)\)
    (For temporary arrays such as y_in.)

Implementation Notes

  • As stated in the problem statement, the boundaries are inclusive, so implement the conditions as lx <= x <= lx+W and y_in[r]-y_in[l] <= H.

  • Even if multiple fish share the same coordinates, they will be counted correctly if they are simply stored in the array and counted.

  • If ans == N, it cannot increase any further, so early exit is possible (also implemented in the code).

    Source Code

import sys


def main():
    input = sys.stdin.readline
    N, W, H = map(int, input().split())
    fish = [tuple(map(int, input().split())) for _ in range(N)]

    xs = sorted(set(x for x, _ in fish))
    ys = [y for _, y in fish]

    ans = 0

    for lx in xs:
        rx = lx + W
        y_in = [y for x, y in fish if lx <= x <= rx]
        if not y_in:
            continue
        y_in.sort()

        l = 0
        best = 0
        for r in range(len(y_in)):
            while y_in[r] - y_in[l] > H:
                l += 1
            cnt = r - l + 1
            if cnt > best:
                best = cnt

        if best > ans:
            ans = best
            if ans == N:
                break

    print(ans)


if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.3-codex.

posted:
last update: