#P4605. Maximizing Illuminated Shield Length
Maximizing Illuminated Shield Length
Maximizing Illuminated Shield Length
Little T is preparing for his upcoming physics lab experiment. In the experiment, an infinite straight rail is placed on a two‐dimensional plane. On this rail a laser emitter of length \(L\) is mounted. The emitter fires two laser beams in the directions perpendicular to the rail. Each beam has a width of \(L\) (in the perpendicular direction).
There are also \(n\) shields placed on the plane. Each shield is represented as a line segment. It is guaranteed that no shield touches the rail, and the acute angle between any shield and the rail does not exceed \(85^\circ\). Furthermore, any two shields do not touch each other. A shield is considered to be "illuminated" only when the laser beam hits it. Note that the laser beams cannot penetrate the shields — they are completely absorbed upon contact, and no reflection occurs.
For a given placement of the emitter on the rail, a shield might be partially illuminated. More precisely, assume that we choose a coordinate system so that the rail is the horizontal \(x\)-axis. Suppose the emitter is placed with its left endpoint at \(a\) so that its interval is \([a, a+L]\). Then, the upward beam covers the rectangle \([a, a+L] \times [0,L]\) and the downward beam covers \([a, a+L] \times [-L,0]\). For each shield (which lies entirely above or entirely below the \(x\)-axis), only the beam on its side may illuminate some portion of it. Only the part of the shield that lies within the corresponding beam rectangle contributes to the total illuminated length.
Your task is to choose a position \(a\) for the emitter (i.e. choose the horizontal placement of the emitter on the rail) so that the sum of the illuminated lengths over all shields is maximized. For each shield, if only a portion of it is hit by the beam, only that illuminated portion is counted.
Note on calculation: Suppose a shield has endpoints \((x_1,y_1)\) and \((x_2,y_2)\) and lies on the same side of the rail. Let \(d_x=x_2-x_1\) and \(d_y=y_2-y_1\), and let \(k=\sqrt{d_x^2+d_y^2}\) be its length. For a shield that is not vertical (\(d_x \neq 0\)), one may compute the portion of the shield that is within the beam via its projection on the \(x\)-axis. If the active part of the shield (i.e. the part within the vertical limits \([0,L]\) for shields above the rail or \([-L,0]\) for shields below the rail) projects onto the \(x\)-interval \([A, B]\) (with \(A \le B\)), then when the emitter is placed so that its interval is \([a,a+L]\), the illuminated length from that shield is given by:
[ f(a)=\frac{k}{|d_x|}\cdot\max(0,\min(a+L,B)-\max(a,A)). ]
If the shield is vertical (\(d_x=0\)), then its \(x\)-coordinate is constant. In that case, if the constant \(x\)-coordinate lies in \([a,a+L]\), the entire active portion of the shield is illuminated; otherwise, none of it is.
inputFormat
The first line contains two numbers \(n\) and \(L\) (where \(n\) is the number of shields and \(L\) is a positive real number as described above).
Each of the next \(n\) lines contains four real numbers: \(x_1\), \(y_1\), \(x_2\), \(y_2\), representing the endpoints of a shield. It is guaranteed that the shield does not touch the \(x\)-axis, and that for each shield the acute angle with the \(x\)-axis is at most \(85^\circ\). No two shields touch each other.
All numbers are given with at most 6 digits after the decimal point.
outputFormat
Output a single real number, the maximum total illuminated length, with at least 6 digits of precision.
sample
1 10
0 5 10 5
10.000000
</p>