Icpc 2021
Icpc 2021
Crystal Crosswind
Time limit: 5 seconds
You are part of a scientific team developing a new technique to image crystal structures at the molecular
level. The technique involves blowing a very fine wind over the surface of the crystal at various angles to
detect boundaries (indicated by molecules that are exposed to the wind). This is repeated with different
wind directions and the boundaries observed for each direction are recorded. Your team has already
collected the data, but – as is often the case with applied science – now the real work, analysis, must
begin.
For a given crystal, you will receive the directions in which wind blew over the surface, and the locations
of all boundaries encountered by each of these winds. For a wind blowing in direction (wx , wy ), a
boundary is defined as a location (x, y) such that a molecule exists at (x, y) and no molecule exists at
(x − wx , y − wy ). Note that for technical reasons wx and wy are not necessarily relatively prime.
The data might not uniquely determine the structure of the crystal. You must find the two unique struc-
tures with the minimal and maximal number of molecules consistent with the observations.
For example, in the first sample input, nine different molecules are directly encountered by the given
winds. There must be a molecule at location (3, 3) because otherwise (4, 2) would be a boundary for
the third wind. For similar reasons, there must be molecules at (4, 4) and (5, 5). There cannot be any
further molecules as they would result in additional observations for some of the winds.
Input
The first line of input contains three integers dx , dy , and k, where dx and dy (1 ≤ dx , dy ≤ 103 ) are
the maximum dimensions of the crystal structure, and k (1 ≤ k ≤ 10) is the number of times wind was
blown over the crystal.
Each of the remaining k lines specifies the data for one wind. These lines each start with two integers
wx and wy (−dx ≤ wx ≤ dx and −dy ≤ wy ≤ dy , but not both zero) denoting the direction of the wind.
Then comes an integer b (0 ≤ b ≤ 105 ) giving the number of boundaries encountered by this wind. The
line finishes with b distinct pairs of integers x, y (1 ≤ x ≤ dx and 1 ≤ y ≤ dy ) listing each observed
boundary.
You may assume the input is consistent with at least one crystal and that no molecules exist outside the
specified dimensions.
Output
Output two textual representations of the crystal structure separated by an empty line. Each structure
has dy rows of dx characters, with the top-left corner corresponding to location (1, 1). The first is
the structure with the minimal number of molecules consistent with the observations, the second is the
maximal one. Use ‘#’ for a location where a molecule exists and ‘.’ for a location where no molecule
exists.
..#...
.#.#..
#.#.#.
.#.#.#
..#.#.
...#..
##.##
.##.#
.###.
..###
There are q different scenarios that Alice and Bob wish to examine. In the ith scenario, the player starts
in room si , the key is in room ki , and the trap is in room ti . For each scenario, compute the minimum
amount of time needed to explore the entire dungeon without getting trapped.
Input
The first line of input contains two integers n and q, where n (3 ≤ n ≤ 2 000) is the number of rooms
and q (1 ≤ q ≤ 200 000) is the number of scenarios to consider. Rooms are numbered from 1 to n. The
next n − 1 lines each contain three integers u, v, and w indicating that there is a corridor between rooms
u and v (1 ≤ u, v ≤ n, u 6= v) that takes time w (1 ≤ w ≤ 109 ) to traverse.
Then follow q lines: the ith of these lines contains three distinct integers si , ki , and ti (1 ≤ si , ki , ti ≤
n) indicating the room where the player starts, the room with the key, and the room with the trap,
respectively.
Output
For each scenario, output the minimum amount of time needed to visit every room at least once. If it is
impossible to visit every room at least once, output impossible.
Input
The input contains one line with two integers n and m, where n (6 ≤ n ≤ 106 ) is the number of pirates
including Cap’n Red and m (1 ≤ m ≤ 1018 ) is the total value of their loot.
Output
Output one line with two positive integers p and q, where f = pq as specified above. If there are multiple
suitable fractions, choose one with the smallest q. Among multiple suitable fractions with the same
smallest q choose the one with the smallest p. If there is no suitable fraction, output impossible
instead and hope for mercy.
Input
The first line of input contains an integer n (3 ≤ n ≤ 100), the number of vertices that describe the
polygon. This is followed by n lines each containing two integers x and y (0 ≤ x, y ≤ 1 000), giving
the coordinates of the polygon vertices in counterclockwise order. The next line contains two integers
xg and yg , which specify the location of the guard. Finally, the last line contains two integers xs and ys ,
which specify the location of the center of the sculpture. The polygon is simple, that is, its vertices are
distinct and no two edges of the polygon intersect or touch, other than consecutive edges which touch at
their common vertex. In addition, no two consecutive edges are collinear.
Output the minimum distance that the guard has to move to be able to see at least half the sculpture.
Your answer must have an absolute or relative error of at most 10−6 .
Input
The input contains one line with several integers. The first integer gives k (2 ≤ k ≤ 10), the number of
cards that will be selected. The second integer gives m (1 ≤ m ≤ 10), the number of ways of marking
the cards. The line is completed by m positive integers, giving the number of cards marked with each
distinct method. The sum of these m integers is n (k ≤ n ≤ 109 ), which is the size of the deck.
Output
Output the highest possible probability of guessing the k th card correctly, accurate up to an absolute
error of 10−9 .
θ θ
Figure F.1: A view of the plane, shown (a) Three islands (shown in (b) The shaded areas represent
head-on. Its camera points downward and black) and two flight paths (red the ground visible on the two
and green). Altitudes are not flight paths for an optimally cho-
can see the part of the ground underneath shown. sen θ.
the plane that is shown in green. How
much is visible depends on the aperture Figure F.2: Surveying three islands via two flight paths.
angle θ. This corresponds to the first sample input.
Output
Output the smallest angle θ (in degrees) that allows for a complete survey of the islands with the given
flights. The answer should be exact to an absolute or relative error of 10−6 . If there is no such angle,
then output impossible. The input is chosen such that if the coordinates of the island vertices are
changed by at most ±10−8 , then the answer will not change more than the allowed rounding error.
Input
The first line of input contains two integers rp and cp , where rp and cp (1 ≤ rp , cp ≤ 1 000) are the
number of rows and columns in the motif. Then rp lines follow, each with cp integers in the range
[0, 100], denoting the color of the motif at that position. A value of 0 denotes an empty square.
The next line of input contains two integers rq and cq where rq and cq (1 ≤ rq , cq ≤ 1 000) are the
number of rows and columns in the mosaic. Then rq lines follow, each with cq integers in the range
[1, 100], denoting the color of the mosaic at that position.
On the first line, output k, the total number of matches. Then output k lines, each of the form r c where
r is the row and c is the column of the top left tile of the match. Sort matches by increasing r, breaking
ties by increasing c.
• (), or
Input
The first line of input contains one integer n (1 ≤ n ≤ 106 ), the number of tablets. Each of the remaining
n lines describes a tablet, and contains a non-empty string of opening and closing parentheses; symbols
unrelated to the nesting structure are omitted. The strings are numbered from 1 to n in the order that
they appear in the input. The input contains at most 107 parentheses.
Output
Output a permutation of the numbers from 1 to n such that concatenating the strings in this order re-
sults in a properly nested structure. If this happens for multiple permutations, any one of them will be
accepted. If there is no such permutation, output impossible instead.
strand
4
bridge
Figure I.1: The path starting from strand 4 on the spiderweb in Sample Input 1.
The first line of input has three integers n, m, and s, where n (3 ≤ n ≤ 200 000) is the number of
strands, m (0 ≤ m ≤ 500 000) is the number of bridges, and s (1 ≤ s ≤ n) is Charlotte’s favorite
strand. Strands are labeled from 1 to n in counterclockwise order. Each of the remaining m lines
contains two integers d and t describing a bridge, where d (1 ≤ d ≤ 109 ) is the bridge’s distance from
the center of the spiderweb and t (1 ≤ t ≤ n) is the first strand of the bridge in counterclockwise order.
Specifically, if 1 ≤ t < n, then the bridge connects strands t and t+1. If t = n, then the bridge connects
strands 1 and n. All bridge distances d are distinct.
Output
Output n lines, where the ith line is the minimum number of bridges Charlotte needs to add in order to
end at strand s after walking on autopilot from strand i.
The overall network has one input, which is the sequence of positive integers 1, 2, 3, . . . , m. Any output
of any node can be queried. A query will seek to identify the k th number in the sequence of numbers for
a given output and a given k. Your task is to implement such queries efficiently.
Input
The first line of input contains three integers m, n, and q, where m (1 ≤ m ≤ 109 ) is the length of the
input sequence, n (1 ≤ n ≤ 104 ) is the number of nodes, and q (1 ≤ q ≤ 103 ) is the number of queries.
The next n lines describe the network, one node per line. A split node has the format S x y z, where
x, y and z identify its input, first output and second output, respectively. A merge node has the format
M x y z, where x, y and z identify its first input, second input and output, respectively. Identifiers x, y
and z are distinct positive integers. The overall input is identified by 1, and the remaining input/output
identifiers form a consecutive sequence beginning at 2. Every input identifier except 1 appears as exactly
one output. Every output identifier appears as the input of at most one node.
Output
For each query x and k output one line with the k th number in the output sequence identified by x, or
none if there is no element with that index number.
Input
The first line of input contains an integer n (1 ≤ n ≤ 104 ), giving the total number of nodes in the
tournament tree. The next n lines each describe a single tree node indexed from 1 to n. The line for
node i starts with an integer ki (0 ≤ ki ≤ 100), the number of children of that node. If ki is 0, then node
i is an input meme and there will be two more integers xi and yi (−103 ≤ xi , yi ≤ 103 ) describing it. If
ki > 0, then ki different integers j (i < j ≤ n) will follow, giving the indices of the ki nodes entering
this voting step.
All input memes will eventually be merged into the final output meme at node 1. The complete tree will
have a height of no more than 10.
Output
Output the largest possible quality for the champion meme at node 1.
× ... 9 10 11 12
× 23 8 1 2 13
22 7 0 3 14
× × 21 6 5 4 15
× 20 19 18 17 16
(a) Grid with markers (×) corresponding to the first (b) The order in which a test subject visits grid cells,
sample input. relative to starting cell 0.
Figure L.1: Sample grid and the order in which test subjects explore the grid.
In the trial, test subjects will explore their environment by following an expanding clockwise spiral
whose first few steps are shown in Figure L.1(b). The starting cell is labeled “0”, and the numbers show
the order in which other cells are visited. The test subjects can see a marker only if they are at its grid
cell, and they will stop their exploration as soon as they know where they are based on the grid cells
that they have seen so far. That means that the observed sequence of empty and marked grid cells could
have begun only at a single possible starting position. The grid is unbounded, but the exploration will be
finite since once a test subject has seen all markers on the grid, they’ll definitely know where they are.
Having hundreds of test subjects literally running in circles can be expensive, so the ICPC figures that
writing a simulation will be cheaper and faster. Maybe you can help?
The input describes a single grid. It starts with a line containing two integers dx , dy (1 ≤ dx , dy ≤ 100).
The following dy lines contain dx characters each, and describe part of the test grid. The ith character of
the j th line of this grid description specifies the contents of the grid cell at coordinate (i, dy − j + 1). The
character is either ‘.’ or ‘X’, meaning that the cell is either empty, or contains a marker, respectively.
The total number of markers in the grid will be between 1 and 100, inclusive. All grid cells outside the
range described by the input are empty.
Output
In ICPC’s experiment, a test subject knows they will start at some position (x, y) with 1 ≤ x ≤ dx ,
1 ≤ y ≤ dy .
Output three lines. The first line should have the expected number of steps needed to identify the starting
position, assuming that the starting position is chosen uniformly at random. This number needs to be
exact to within an absolute error of 10−3 .
The second line should have the maximum number of steps necessary until one can identify the starting
position.
The third line should list all starting coordinates (x, y) that require that maximum number of steps. The
coordinates should be sorted by increasing y-coordinates, and then (if the y-coordinates are the same)
by increasing x-coordinates.