This page is still under construction.

Parts of this page are still being built. What you see may change.

Shy Polygon

Time limit2sMemory limit128 MB

Summary
Given two simple polygons that can be slid only along the x-axis, find the minimum total x-extent such that every point of one polygon stays at least distance L from every point of the other.
Level

Hard8 of 10

Topics
Geometry, Binary search, Simulation
Solved
No attempts yet

Problem

You are given two solid polygons together with their positions on the xyxy-plane. You may slide one of the two polygons along the xx-axis; the two polygons are allowed to overlap while it is being moved. You may not translate it in any other direction, and you may not rotate it.

Place the two polygons as compactly as possible while satisfying this condition: the distance between every point of one polygon and every point of the other polygon must be at least a given value LL. Because the polygons are solid, a "point of a polygon" may lie on its boundary or in its interior; in particular, if the two polygons overlap, the distance between them is 00.

The width of a placement is the difference between the largest and the smallest xx-coordinate taken over all points of the two polygons. Write a program that computes the minimum possible width over all placements that satisfy the condition above.

For example, if the two polygons in Figure 13 are placed with L=10.0L = 10.0, the minimum width is 100100. Figure 14 shows one such optimal placement.

Figure 13: Initial positions of the two polygons

Figure 14: One optimal placement (L=10.0L = 10.0)

Input

The input consists of several datasets. Each dataset has the following format:

L
Polygon1
Polygon2

LL is a decimal number, the required minimum distance between the two polygons, with 0.1<L<50.00.1 < L < 50.0.

Each polygon is given as:

n
x1 y1
x2 y2
...
xn yn

nn is the number of vertices of the polygon, with 2<n<152 < n < 15. Each of the next nn lines contains two nonnegative integers, the xx- and yy-coordinates of a vertex separated by a single space; both coordinates are less than 500500.

Edges connect consecutive vertices, and also the last vertex back to the first. The vertices are listed in counterclockwise order, and every polygon is simple: its boundary neither crosses nor touches itself.

You may assume the result is numerically stable. For a fixed pair of polygons, let w(l)w(l) denote the minimum width as a function of the required distance ll; then ∣w(L±10−7)−w(L)∣<10−4|w(L \pm 10^{-7}) - w(L)| < 10^{-4}.

The end of the input is a line containing a single 00; it is not part of any dataset.

Output

For each dataset, print one line containing the minimum width, rounded to exactly six digits after the decimal point.

Examples2

  1. Example 1

    Input
    10.5235
    3
    0 0
    100 100
    0 100
    4
    0 50
    20 50
    20 80
    0 80
    10.0
    4
    120 45
    140 35
    140 65
    120 55
    8
    0 0
    100 0
    100 100
    0 100
    0 55
    80 90
    80 10
    0 45
    10.0
    3
    0 0
    1 0
    0 1
    3
    0 100
    1 101
    0 101
    10.0
    3
    0 0
    1 0
    0 100
    3
    0 50
    100 50
    0 51
    0
    
    Expected output
    114.882476
    100.000000
    1.000000
    110.500500
    
  2. Example 2

    Input
    5.0
    4
    0 0
    10 0
    10 10
    0 10
    4
    100 0
    110 0
    110 10
    100 10
    0
    
    Expected output
    25.000000