Catching Fish
Time limit1sMemory limit256 MB
Given up to 100 fish on a large grid and a fixed net perimeter, find the placement of the net that covers the most fish.
- Level
Medium5 of 10
- Topics
- Brute force, Prefix sum, Array
- Solved
- No attempts yet
Problem
Fish are an important source of protein in the Korean diet. However, rising sea temperatures and years of overfishing have steadily reduced the number of fish in the nearby seas, so the government has restricted both where fishing is allowed and the size of net a fishing boat may use.
Because fish live near the surface of the sea, the height of the net does not matter. The net can therefore be thought of as a single string of length that is stretched into the outline of a rectangle to catch fish. Each of the rectangle's two side lengths is an integer that is at least , and since the perimeter equals , the two side lengths sum to . For example, if , the possible nets are the four rectangles , , , and .
The fishing area is an grid of cells. Each cell has a coordinate: the top-left cell is and the bottom-right cell is . There are fish, each living on a distinct cell, and the fish do not move.
The boat picks one cell, treats it as the top-left corner of the net, and casts the net to the right and downward. That is, starting from the boat's cell the net covers a rectangular region of rows and columns (with ), and it catches every fish inside that region, including those on its border. The net may only be cast so that it stays completely inside the grid.
The figure below shows an example with , , and fish at , , , , , and , where the boat on cell casts a net. In this case it catches fish.

Given the size of the grid, the positions of the fish, and the length of the net, write a program that finds the maximum number of fish that can be caught with a single cast.
Input
The first line contains three integers: the grid size , the net length , and the number of fish , separated by single spaces (, , ). is even and satisfies .
Each of the next lines contains the coordinates of one fish, given as its row and column separated by a single space. The fish are listed in an arbitrary order.
Output
Print, as a single integer, the maximum number of fish that can be caught with one cast of the net.