Official

D - 魚捕り / Catching Fish Editorial by admin

GPT 5.4 High

Overview

This problem asks for the maximum number of fish that can be caught with a rectangular net.
Using the property that “the bottom-left corner of the optimal net can be aligned with the coordinates of some fish,” we fix \(x\) and optimize in the \(y\) direction using the two-pointer technique.

Analysis

First, since the position of the net can be chosen freely as real numbers, we cannot search all possibilities directly.

Key Observation 1: It suffices to consider the coordinates of fish as candidates for the bottom-left corner

Suppose an optimal net has: - \(x\)-range: \([a, a+W]\) - \(y\)-range: \([b, b+H]\)

Let \(x_{\min}\) be the minimum \(x\)-coordinate among the fish caught in this net.

Then, even if we shift the net to the right so that its left boundary is at \(x_{\min}\), all the fish that were originally caught are still within the range.
This is because the \(x\)-coordinates of all originally caught fish satisfy

\[ a \le x \le a+W \]

and since \(x_{\min} \ge a\), it follows that

\[ x \le a+W \le x_{\min}+W \]

The left side is also satisfied since \(x_{\min} \le x\).

In other words, we can see that the left boundary of the optimal solution can be aligned with the \(x\)-coordinate of some fish.
Similarly, the bottom boundary can be aligned with the \(y\)-coordinate of some fish.


Naive Approach

If we try all candidate pairs by: - setting the left boundary to each \(X_i\) - setting the bottom boundary to each \(Y_j\)

there are \(N\) candidates for each.
For each pair \((X_i, Y_j)\), counting all the fish takes \(O(N)\), resulting in an overall time complexity of

\[ O(N^3) \]

For \(N \le 3000\), this is too slow.


How to Improve

Consider fixing the left boundary at \(x = \text{left}\).

At this point, the only fish that could possibly be inside the net are those satisfying

\[ \text{left} \le x \le \text{left} + W \]

Extracting only the \(y\)-coordinates of these fish reduces the problem to a one-dimensional problem:

Given a sequence \(y_1, y_2, \dots\), find the maximum number of elements contained in an interval \([b, b+H]\) of length \(H\).

This can be solved in \(O(M)\) time by sorting \(y\) and using the two-pointer technique.
Here, \(M\) is the number of fish falling within that \(x\)-range.


Why the Two-Pointer Technique Works

Let ys be the array of \(y\)-coordinates sorted in ascending order.
When fixing the left end at ys[ll], if we extend the right end ys[rr] as far as possible such that

\[ ys[rr] - ys[ll] \le H \]

is satisfied, the number of fish contained in that interval is rr - ll (or rr - ll + 1 depending on index convention).

Even when ll is advanced by 1, rr never needs to move backward, allowing the entire process to run in linear time.

Algorithm

  1. Sort the fish by their \(x\)-coordinates.
  2. Iterate through each unique \(x\)-coordinate left as a candidate for the left boundary.
  3. Extract the fish satisfying left <= x <= left+W using binary search.
    • Since the fish are sorted by \(x\), this range forms a contiguous segment.
  4. Collect the \(y\)-coordinates of these fish and sort them.
  5. Apply the two-pointer technique on the sorted \(y\) array to find the maximum number of elements with a difference of at most \(H\).
  6. Take the maximum value over all left as the final answer.

Complexity

  • Time Complexity: \(O(N^2 \log N)\)
  • Space Complexity: \(O(N)\)

Implementation Notes

  • The range check for \(x\) includes endpoints, so it is left <= x <= left+W.
    In the implementation, this is handled using:
    • Left boundary: bisect_left
    • Right boundary: bisect_right
  • The \(y\) direction also includes endpoints, so in the two-pointer technique, we expand under the condition:

$\( ys[rr] - ys[ll] \le H \)$

  • Even if multiple fish share the same coordinates, each fish must be counted separately.
    Since this implementation does not deduplicate the fish themselves, they are counted correctly.

  • Testing the same \(x\) coordinate multiple times as a left boundary candidate is redundant, so duplicates are removed using unique_xs.

    Source Code

import sys
import bisect

def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    if not data:
        return

    it = iter(data)
    N = next(it)
    W = next(it)
    H = next(it)

    points = [(next(it), next(it)) for _ in range(N)]
    points.sort()
    xs = [x for x, _ in points]

    unique_xs = []
    prev = None
    for x in xs:
        if x != prev:
            unique_xs.append(x)
            prev = x

    ans = 0

    for left in unique_xs:
        l = bisect.bisect_left(xs, left)
        r = bisect.bisect_right(xs, left + W)

        ys = [points[i][1] for i in range(l, r)]
        ys.sort()

        rr = 0
        m = len(ys)
        for ll in range(m):
            while rr < m and ys[rr] - ys[ll] <= H:
                rr += 1
            cnt = rr - ll
            if cnt > ans:
                ans = cnt

    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.4-high.