problems / 157

The Rotating Camera

medium geometry sorting sliding-window two-pointers 1000 ms · 256 MB

The Rotating Camera

A warehouse floor is a flat plane with integer coordinates. A security camera is mounted at the point (cx, cy), and n objects lie on the floor, each at an integer point. Objects are tiny and never hide one another.

The camera sits on a swivel and can be turned to face any direction in the plane, not just whole-degree directions. Its lens has a field of view exactly θ degrees wide. Once the camera is turned, the bearing of an object is the direction of the ray from the camera to that object. The camera sees an object if its bearing lies inside the field of view or exactly on either of its two edges. In other words, the field of view is a closed angular sector of width θ with its apex at the camera. With θ = 360 the sector is the whole plane. An object lying exactly at the camera's position (cx, cy) has no bearing, and the camera always sees it, whichever way it faces.

The guard wants to turn the camera so that it sees as many objects as possible at the same moment. Find that maximum number.

Input

The first line contains two integers n and θ (1 ≤ n ≤ 10^5, 0 ≤ θ ≤ 360): the number of objects and the width of the field of view in degrees.

The second line contains two integers cx and cy, the camera's position.

Each of the next n lines contains two integers x_i and y_i, the position of the i-th object. Several objects may share a position, and any number of them may lie exactly at the camera's position.

All coordinates satisfy -10^9 ≤ cx, cy, x_i, y_i ≤ 10^9.

For θ < 360 the test data also guarantees the following. Take any two objects A and B that are not at the camera's position, and let φ be the angle, measured in degrees in [0, 360), through which the ray towards A must be turned counterclockwise to reach the ray towards B. Then either φ = θ exactly, or |φ − θ| ≥ 10^-6. Exact equality can and does happen (for example, the bearings towards (1, 0) and (1, 1) from (0, 0) are exactly 45 degrees apart), but you never have to resolve a difference smaller than 10^-6 degrees. So computing bearings in double precision and comparing them with a small tolerance, such as 10^-9 radians, is safe.

Output

Print one integer: the largest number of objects the camera can see at once.

Notes

In the first example the camera is at (1, 1), and θ = 90. Seen from the camera, the objects at (4, 1), (3, 3) and (1, 4) lie due east, northeast and due north. If the field of view runs from east to north, the first and the third of them lie exactly on its edges, so all three are seen. The object at (1, 1) is at the camera's position and is always seen, which gives 4. No turn of the camera sees four objects that are not at its position.

In the second example the camera is at (-2, 3). The objects at (3, 2), (2, 4) and (4, 5) have bearings of about 348.7, 14.0 and 18.4 degrees counterclockwise from east. A 60-degree field of view that starts slightly south of east covers all three. Remember that a field of view may cross the due-east direction.

Example 1

Input

7 90
1 1
4 1
3 3
1 4
0 2
1 1
-1 -2
2 -3

Output

4

Example 2

Input

6 60
-2 3
3 2
2 4
4 5
-5 3
-2 1
-3 7

Output

3

Hints · reveal one at a time if you're stuck

Hint 1

Objects at the camera's position are seen whatever the direction, so set them aside. For the rest, the distance is irrelevant: only each object's bearing matters. Think of the bearings as points on a circle and the field of view as an arc of fixed width.

Hint 2

An optimal arc can be turned until one of its edges sits on an object's bearing. Sort the bearings, then count with a sliding window; a window that crosses the wrap-around point needs special handling, so consider duplicating the sorted list with a full turn added.

Hint 3

Use a small tolerance when checking whether a bearing lies on the closed edge, since integer points create exact 45-degree style ties. Be careful with θ = 360, where a window must not count the same object twice.

report an issue Prefer your own editor? sooks prep 157 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.