Tiles
Time limit2sMemory limit1024 MB
Given a simple axis-aligned polygon, find the largest k at most M such that the part left of x = k can be covered by non-overlapping 2 by 2 tiles.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Geometry
- Solved
- No attempts yet
Statement
Soon after converting to Christianity, the first and only Lithuanian king, Mindaugas, is believed to have ordered the construction of the Vilnius Cathedral. The construction is nearly complete. Only the floor remains, and it must be covered with ornamented glazed ceramic tiles.
The cathedral floor is a polygon in a 2D plane with a Cartesian coordinate system. The polygon has distinct vertices, numbered from 1 to . For each , vertex is at point , where and are nonnegative integers. Vertex is joined to vertex for each , and vertex is joined to vertex 1. The vertices are listed in either clockwise or counterclockwise order.
The cathedral is an axis-aligned polygon: every edge is parallel to the -axis or the -axis. It is also simple. Exactly two edges meet at each vertex, and any two edges meet only at a shared vertex.
The builders have infinitely many square tiles, each with side length 2. They pick a vertical line and want to cover the part of the cathedral to the left of it. For any integer , let be the vertical line with -coordinate . A covering of the part left of is a placement of tiles in the plane such that:
- every point inside the polygon with -coordinate less than is covered by some tile;
- no point outside the polygon, or with -coordinate greater than , is covered by any tile;
- the interiors of the tiles do not overlap.
The minimum -coordinate of any vertex is 0. Let be the maximum -coordinate of any vertex. Find the largest integer with such that a covering of the part left of exists. By definition, a covering of the part left of exists, using 0 tiles.
Input
The first line contains two integers and , the number of vertices and the maximum -coordinate of any vertex. Each of the next lines contains two integers and , the coordinates of vertex .
Output
Print the maximum such that and a covering of the part of the cathedral to the left of exists.
Constraints
- for each
- The cathedral forms an axis-aligned simple polygon.
- The minimum of is 0, and the maximum is .