electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...

Ant

Time Limit: 1000msMemory Limit: 256MB
View Submissions

Problem Description

The ant is on a board shaped like a convex polygon. It moves straight in the direction it is facing, but does not know which direction that is. If it reaches the edge of the board, it will fall off. Given the position of the ant and the positions of the vertices that make up the convex polygon of the board, determine the shortest distance it can travel before falling off the board. All positions are given in 2D coordinates.

Input

The first line contains two integers xxx and yyy, representing the coordinates of the ant (−100≤x≤100-100 \le x \le 100−100≤x≤100, −100≤y≤100-100 \le y \le 100−100≤y≤100).

The second line contains an integer NNN, representing the number of vertices of the polygon (3≤N≤103 \le N \le 103≤N≤10).

The next NNN lines contain integers xix_ixi​ and yiy_iyi​ (−100≤xi,yi≤100-100 \le x_i, y_i \le 100−100≤), representing the coordinates of the vertices of the polygon, given in counterclockwise order.

All input values are integers. The point (x,y)(x,y)(x,y) is guaranteed to be strictly inside the polygon (not on the boundary), and the polygon is guaranteed to be convex.

Output

Output the shortest distance that the ant can travel before falling off the board, on a single line. The output will be accepted if the absolute error or relative error does not exceed 10−610^{-6}10−6.

Scoring System

SubtasksAdditional ConstraintsPointsRequired Subtasks
0Example0—
1N=3N = 3N=325—
2N=4N = 4N=4, guaranteed that the sides are parallel to the coordinate axes15

Examples

Example 1
Input
0 0
4
100 100
-100 100
-100 -100
100 -100
Output
100
Example 2
Input
10 10
3
0 100
-100 -100
100 -100
Output
31.3049516850
Example 3
Input
34 6
7
-43 -65
-23 -99
54 -68
65 92
16 83
-18 43
-39 2
Output
25.0284205314

© 2026 Electicode. All rights reserved.

xi​,yi​≤
100
—
3—600, 1, 2