| <p> | |
| You're about to put on an exciting show at your local circus — a parkour demonstration! | |
| <strong>N</strong> platforms with adjustable heights have been set up in a row, and are numbered from 1 to <strong>N</strong> in order from left to right. | |
| The initial height of platform <em>i</em> is <strong>H<sub>i</sub></strong> metres. | |
| </p> | |
| <p> | |
| When the show starts, <strong>M</strong> parkourists will take the stage. | |
| The <em>i</em>th parkourist will start at platform <strong>A<sub>i</sub></strong>, with the goal of reaching a different platform <strong>B<sub>i</sub></strong>. | |
| If <strong>B<sub>i</sub></strong> > <strong>A<sub>i</sub></strong>, they'll repeatedly jump to the next platform to their right until they reach <strong>B<sub>i</sub></strong>. | |
| If <strong>B<sub>i</sub></strong> < <strong>A<sub>i</sub></strong>, they'll instead repeatedly jump to the next platform to their left until they reach <strong>B<sub>i</sub></strong>. | |
| All of the parkourists will complete their routes simultaneously (but don't worry, they've been trained well to not impede one another). | |
| </p> | |
| <p> | |
| Not all parkourists are equally talented, and there are limits on how far up or down they can jump between successive platforms. | |
| The <em>i</em>th parkourist's maximum upwards and downwards jump heights are <strong>U<sub>i</sub></strong> and <strong>D<sub>i</sub></strong>, respectively. | |
| This means that they're only able to move directly from platform <em>x</em> to some adjacent platform <em>y</em> if | |
| <strong>H<sub>x</sub></strong> - <strong>D<sub>i</sub></strong> ≤ <strong>H<sub>y</sub></strong> ≤ | |
| <strong>H<sub>x</sub></strong> + <strong>U<sub>i</sub></strong>, | |
| where <strong>H<sub>x</sub></strong> and <strong>H<sub>y</sub></strong> are the current heights of platforms <em>x</em> and <em>y</em>, respectively. | |
| </p> | |
| <p> | |
| With the show about to begin, a disastrous flaw has just occurred to you — | |
| it may not be possible for all of the parkourists to actually complete their routes with the existing arrangement of platforms! | |
| If so, you will need to quickly adjust some of the platforms' heights first. The height of each platform may be adjusted upwards or downwards at a rate of 1 metre per second, | |
| to any non-negative real-valued height of your choice, and multiple platforms may be adjusted simultaneously. | |
| As such, if the initial height of platform <em>i</em> is <strong>H<sub>i</sub></strong> and its final height is <strong>P<sub>i</sub></strong>, | |
| then the total time required to make your chosen height adjustments will be | |
| <code>max{</code>|<strong>H<sub>i</sub></strong> - <strong>P<sub>i</sub></strong>|<code>}</code> over <em>i</em>=1..<strong>N</strong>. | |
| </p> | |
| <p> | |
| Determine the minimum amount of time required to set up the platforms such that all <strong>M</strong> parkourists will then be able to complete their required routes. | |
| Note that you may not perform further height adjustments once the show starts. The platform heights must all remain constant while all <strong>M</strong> parkourists complete their routes. | |
| </p> | |
| <p> | |
| In order to reduce the size of the input data, you're given <strong>H<sub>1</sub></strong> and <strong>H<sub>2</sub></strong>. | |
| <strong>H<sub>3..N</sub></strong> may then be generated as follows using given constants | |
| <strong>W</strong>, <strong>X</strong>, <strong>Y</strong>, and <strong>Z</strong> (please watch out for integer overflow during this process): | |
| </p> | |
| <p> | |
| <strong>H<sub>i</sub></strong> = | |
| (<strong>W</strong> * <strong>H<sub>i-2</sub></strong> + <strong>X</strong> * <strong>H<sub>i-1</sub></strong> + <strong>Y</strong>) | |
| % <strong>Z</strong> (for <em>i</em>=3..<strong>N</strong>) | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of shows. | |
| For each show, there is first a line containing the space-separated integers <strong>N</strong> and <strong>M</strong>. | |
| The next line contains the space-separated integers <strong>H<sub>1</sub></strong>, <strong>H<sub>2</sub></strong>, | |
| <strong>W</strong>, <strong>X</strong>, <strong>Y</strong>, and <strong>Z</strong>. | |
| Then, <strong>M</strong> lines follow. The <em>i</em>th of these lines contains the space-separated integers | |
| <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>, <strong>U<sub>i</sub></strong>, and <strong>D<sub>i</sub></strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th show, print a line containing "Case #<em>i</em>: " | |
| followed by 1 real number, the minimum amount of time required to set up the platforms (in seconds). Absolute and relative errors of up to 10<sup>-6</sup> will be ignored. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 85 <br /> | |
| 2 ≤ <strong>N</strong> ≤ 200,000 <br /> | |
| 1 ≤ <strong>M</strong> ≤ 20 <br /> | |
| 0 ≤ <strong>H<sub>i</sub></strong> < <strong>Z</strong> <br /> | |
| 0 ≤ <strong>W</strong>, <strong>X</strong>, <strong>Y</strong> < <strong>Z</strong> <br /> | |
| 1 ≤ <strong>Z</strong> ≤ 1,000,000 <br /> | |
| 1 ≤ <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong> ≤ <strong>N</strong> <br /> | |
| 0 ≤ <strong>U<sub>i</sub></strong>, <strong>D<sub>i</sub></strong> ≤ 1,000,000 <br /> | |
| <strong>A<sub>i</sub></strong>, ≠ <strong>B<sub>i</sub></strong> <br /> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first case, <strong>H</strong> = [0, 10]. | |
| You can increase the first platform's height by 3.5 and decrease the second's by 3.5 in 3.5 seconds, yielding <strong>P</strong> = [3.5, 6.5]. | |
| The single parkourist will then be able to successfully complete their route from platform 1 to platform 2 by jumping upwards by a height of at most 3. | |
| </p> | |
| <p> | |
| In the second case, <strong>H</strong> = [50, 59, 55, 51, 47]. One optimal possibility is <strong>P</strong> = [54.0, 54.5, 53.5, 52.5, 51.5]. | |
| </p> | |
| <p> | |
| In the third case, <strong>H</strong> = [46, 38, 38, 22, 8]. | |
| </p> | |
| <p> | |
| In the fourth case, <strong>H</strong> = [53, 25, 24, 81, 77, 40, 29, 21]. | |
| </p> | |