#P3055. Escape from the Tangled Rope

    ID: 16313 Type: Default 1000ms 256MiB

Escape from the Tangled Rope

Escape from the Tangled Rope

Bessie the cow is tied down with a rope that forms a closed polygon. The rope is described by a sequence of M line segments and starts and ends at Bessie’s position (bx, by). Meanwhile, N fence posts are arranged along a vertical line (all having the same x coordinate, which is less than bx).

Although no fence post lies exactly on a rope segment, the rope may cross over itself and wind around some of the posts. In order for Bessie to pull free and run to the right without the rope catching on any post, some posts may need to be removed. Your task is to determine the minimum number of fence posts that must be removed so that the rope does not catch on any remaining post when Bessie escapes.

Note: A fence post is considered to be entangled by the rope if the rope winds around it, i.e. the total turning angle (or winding number) computed by summing the angles between successive rope segments (with respect to that post) is nonzero (typically about ±2π). Use the \(\theta\) and \(2\pi\) notation as needed in any formulas.

The winding number W for a given post P with respect to the rope endpoints {P0, P1, ..., PM-1} can be expressed as:

[ W(P) = \sum_{i=0}^{M-1} \Delta\theta_i, \quad \text{where} \quad \Delta\theta_i = \theta(P_{i+1} - P) - \theta(P_i - P), ]

with the angles normalized to lie in \( (-\pi, \pi] \). If \(|W(P)| \ge 2\pi\) (or equivalently, if \(|W(P)| > \pi\) as a threshold), the post is wrapped by the rope.

inputFormat

The first line contains two integers N and M (1 ≤ N ≤ 10 and 3 ≤ M ≤ 10000), representing the number of fence posts and the number of rope endpoints (which describe M-1 segments) respectively.

The second line contains two integers bx and by (0 ≤ bx, by ≤ 10000), representing Bessie’s position. Bessie is located to the right of the fence posts.

The next N lines each contain two integers x and y, giving the coordinates of a fence post. All fence posts share the same x coordinate and this value is less than bx.

The following M lines each contain two integers xi and yi, describing the endpoints of the rope segments in order. The first and last endpoints are Bessie’s position. It is guaranteed that no fence post lies on any rope segment.

outputFormat

Output a single integer representing the minimum number of fence posts that must be removed so that Bessie can pull free (i.e. the rope does not wind around any remaining post), allowing her to run to the right without hindrance.

sample

3 4
10 0
0 -1
0 0
0 1
10 0
-5 -5
-5 5
10 0
3