Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2019 /round1 /connect_the_dots.md
wjomlex's picture
2019 Problems
e329daf verified
|
raw
history blame
4.76 kB

There are N dots on a 2D grid, the _i_th of which is a point at coordinates (Xi, Yi). All coordinates are positive integers, and all N dots' positions are distinct.

You'd like to draw N 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 i, you'll draw either a horizontal line segment connecting it to the y-axis (with endpoints (0, Yi) and (Xi, Yi)), or a vertical line segment connecting it to the x-axis (with endpoints (Xi, 0) and (Xi, Yi)). 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.

No horizontal line segment is allowed to intersect with any vertical line segment. Line segments are not 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.

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 N line segments is equal to the cost of drawing the set of horizontal ones plus the cost of drawing the set of vertical ones.

You can choose to draw at most H horizontal line segments, and at most V vertical ones. What's the minimum total cost required to connect all N 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?

In order to reduce the size of the input, the dots' coordinates will not all be provided explicitly. Instead, you'll be given X1, X2, Y1, Y2, as well as 8 constants Ax, Bx, Cx, Dx, Ay, By, Cy, and Dy, and you must then compute X3..N and Y3..N as follows (bearing in mind that intermediate values may not fit within 32-bit integers):

Xi = ((Ax * Xi-2 + Bx * Xi-1 + Cx) modulo Dx) + 1, for i = 3..N

Yi = ((Ay * Yi-2 + By * Yi-1 + Cy) modulo Dy) + 1, for i = 3..N

Input

Input begins with an integer T, the number of grids. For each room, there are three lines. The first line contains the space-separated integers N, H, and V. The second line contains the space-separated integers X1, X2, Ax, Bx, Cx, and Dx. The third line contains the space-separated integers Y1, Y2, Ay, By, Cy, and Dy.

Output

For the _i_th grid, print a line containing "Case #i: " followed by the minimum total cost (in dollars) required to validly connect all N dots to the grid's axes, or -1 if it's impossible to do so.

Constraints

1 ≤ T ≤ 160
2 ≤ N ≤ 800,000
0 ≤ H, VN
0 ≤ Ax, Bx, Cx Ay, By, Cy ≤ 1,000,000,000
1 ≤ Dx, Dy ≤ 1,000,000,000
1 ≤ XiDx
1 ≤ YiDy

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.

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.

In the third case, not all of the dots can be connected.

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).

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).