|
<p> |
|
There are <strong>N</strong> dots on a 2D grid, the <em>i</em>th of which is a point at coordinates (<strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong>). |
|
All coordinates are positive integers, and all <strong>N</strong> dots' positions are distinct. |
|
</p> |
|
|
|
<p> |
|
You'd like to draw <strong>N</strong> line segments, each of which is either horizontal or vertical, to "connect" each of the dots to one of the grid's axes. |
|
In particular, for each dot <em>i</em>, you'll draw either a horizontal line segment connecting it to the y-axis |
|
(with endpoints (0, <strong>Y<sub>i</sub></strong>) and (<strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong>)), |
|
or a vertical line segment connecting it to the x-axis (with endpoints (<strong>X<sub>i</sub></strong>, 0) and (<strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong>)). |
|
Each line segment only counts as "connecting" the single dot located at its endpoint, even if it happens to pass through other dots along the way. |
|
</p> |
|
|
|
<p> |
|
No horizontal line segment is allowed to intersect with any vertical line segment. Line segments are <strong>not</strong> considered to intersect at either of their endpoints — |
|
for example, it's permitted for a horizontal line segment to pass through the endpoint of a vertical one, or vice versa. |
|
Horizontal line segments are allowed to overlap with other horizontal ones, as are vertical line segments with other vertical ones. |
|
</p> |
|
|
|
<p> |
|
The cost of drawing a non-empty set of horizontal line segments is equal to the length of the longest one (in dollars), while the cost of drawing no horizontal line segments is $0. |
|
The cost of drawing a set of vertical line segments is similarly equal to the length of the longest one (if any), |
|
and the total cost of drawing all <strong>N</strong> line segments is equal to the cost of drawing the set of horizontal ones plus the cost of drawing the set of vertical ones. |
|
</p> |
|
|
|
<p> |
|
You can choose to draw at most <strong>H</strong> horizontal line segments, and at most <strong>V</strong> vertical ones. |
|
What's the minimum total cost required to connect all <strong>N</strong> dots to the grid's axes, |
|
without using too many of either type of line segment or causing any horizontal line segments to intersect with vertical ones, if that can be done at all? |
|
</p> |
|
|
|
<p> |
|
In order to reduce the size of the input, the dots' coordinates will not all be provided explicitly. Instead, you'll be given |
|
<strong>X<sub>1</sub></strong>, <strong>X<sub>2</sub></strong>, <strong>Y<sub>1</sub></strong>, <strong>Y<sub>2</sub></strong>, as well as 8 constants |
|
<strong>A<sub>x</sub></strong>, <strong>B<sub>x</sub></strong>, <strong>C<sub>x</sub></strong>, <strong>D<sub>x</sub></strong>, |
|
<strong>A<sub>y</sub></strong>, <strong>B<sub>y</sub></strong>, <strong>C<sub>y</sub></strong>, and <strong>D<sub>y</sub></strong>, |
|
and you must then compute <strong>X<sub>3..N</sub></strong> and <strong>Y<sub>3..N</sub></strong> |
|
as follows (bearing in mind that intermediate values may not fit within 32-bit integers): |
|
</p> |
|
|
|
<p> |
|
<strong>X<sub>i</sub></strong> = |
|
((<strong>A<sub>x</sub></strong> * <strong>X<sub>i-2</sub></strong> + |
|
<strong>B<sub>x</sub></strong> * <strong>X<sub>i-1</sub></strong> + |
|
<strong>C<sub>x</sub></strong>) modulo <strong>D<sub>x</sub></strong>) + 1, for <em>i</em> = 3..<strong>N</strong> |
|
</p> |
|
|
|
<p> |
|
<strong>Y<sub>i</sub></strong> = |
|
((<strong>A<sub>y</sub></strong> * <strong>Y<sub>i-2</sub></strong> + |
|
<strong>B<sub>y</sub></strong> * <strong>Y<sub>i-1</sub></strong> + |
|
<strong>C<sub>y</sub></strong>) modulo <strong>D<sub>y</sub></strong>) + 1, for <em>i</em> = 3..<strong>N</strong> |
|
</p> |
|
|
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of grids. |
|
For each room, there are three lines. |
|
The first line contains the space-separated integers <strong>N</strong>, <strong>H</strong>, and <strong>V</strong>. |
|
The second line contains the space-separated integers |
|
<strong>X<sub>1</sub></strong>, <strong>X<sub>2</sub></strong>, |
|
<strong>A<sub>x</sub></strong>, <strong>B<sub>x</sub></strong>, <strong>C<sub>x</sub></strong>, and <strong>D<sub>x</sub></strong>. |
|
The third line contains the space-separated integers |
|
<strong>Y<sub>1</sub></strong>, <strong>Y<sub>2</sub></strong>, |
|
<strong>A<sub>y</sub></strong>, <strong>B<sub>y</sub></strong>, <strong>C<sub>y</sub></strong>, and <strong>D<sub>y</sub></strong>. |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th grid, print a line containing "Case #<em>i</em>: " |
|
followed by the minimum total cost (in dollars) required to validly connect all <strong>N</strong> dots to the grid's axes, or -1 if it's impossible to do so. |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 160 <br /> |
|
2 ≤ <strong>N</strong> ≤ 800,000 <br /> |
|
0 ≤ <strong>H</strong>, <strong>V</strong> ≤ <strong>N</strong> <br /> |
|
0 ≤ <strong>A<sub>x</sub></strong>, <strong>B<sub>x</sub></strong>, <strong>C<sub>x</sub></strong> |
|
<strong>A<sub>y</sub></strong>, <strong>B<sub>y</sub></strong>, <strong>C<sub>y</sub></strong> ≤ 1,000,000,000 <br /> |
|
1 ≤ <strong>D<sub>x</sub></strong>, <strong>D<sub>y</sub></strong> ≤ 1,000,000,000 <br /> |
|
1 ≤ <strong>X<sub>i</sub></strong> ≤ <strong>D<sub>x</sub></strong> <br /> |
|
1 ≤ <strong>Y<sub>i</sub></strong> ≤ <strong>D<sub>y</sub></strong> <br /> |
|
</p> |
|
|
|
<p> |
|
In the first case, the dots are at coordinates (6, 2) and (3, 4). The cheapest option is to connect both dots using vertical line segments, having lengths 2 and 4 and altogether costing $4 to draw. |
|
The lack of horizontal line segments costs an additional $0, bringing the total to $4 + $0 = $4. |
|
</p> |
|
|
|
<img src={{PHOTO_ID:282249922896220}} width=200 /> |
|
|
|
<p> |
|
The second case is the same as the first, except that at most one vertical line may be drawn. |
|
The cheapest valid option is now to connect the second dot using a horizontal line segment (of length 3) while still connecting the first dot using a vertical one (of length 2). |
|
These two line segments do not intersect, and cost a total of $3 + $2 = $5 to draw. |
|
</p> |
|
|
|
<img src={{PHOTO_ID:292271182178799}} width=200 /> |
|
|
|
<p> |
|
In the third case, not all of the dots can be connected. |
|
</p> |
|
|
|
<p> |
|
In the fourth case, the dots are at coordinates (1, 1), (1, 2), (2, 1), and (2, 2). |
|
You can connect the first dot using a horizontal line segment (of length 1), and the other dots with vertical ones (of lengths at most 2), for a total cost of $1 + $2 = $3. |
|
Note that this causes two vertical line segments to overlap (the ones connecting the third and fourth points). |
|
</p> |
|
|
|
<img src={{PHOTO_ID:262631865014685}} width=114 /> |
|
|
|
<p> |
|
In the fifth case, the dots are at coordinates (15, 34), (19, 3), (2, 38), (13, 17), (18, 14), (25, 15), (42, 18), (9, 11), (26, 34), and (41, 19). |
|
</p> |
|
|