Problem Set
Problem Set
A-Skew-ed Reasoning
Time limit: 2 seconds
The following is based on a true story – the names have been changed because. . . well, because you
always change names in stories like this one.
Professor Taylor Swift is grading a homework assignment on integer skew heaps. A skew heap is a
binary tree with an integer stored in each node such that the value in any node is less than or equal to the
values in any of its children. Note that the skew heap need not be a perfect binary tree; that is, the left
and/or right subtree of any node may be empty.
Inserting a value x into a skew heap H is done using the following recursive procedure:
4 4
9 5 5 9
20 25 6 11 7 6 20 25
17 11
17
Figure A.1: Example of inserting the value 7 into a skew heap. The nodes storing 4 and 5
(marked in blue) have their children swapped, while the node storing 11 becomes the left
child of the newly inserted node (marked in red).
Now, back to Professor Swift. The homework problem she has assigned asks the students to show the
heap that results from inserting a given permutation of the numbers from 1 to n, in the given order,
into an empty heap. Surprisingly, some of the students have wrong answers! That got Professor Swift
wondering: For a given heap, is there an input permutation that would have produced this heap? And if
so, what are the lexicographically minimal and maximal such input permutations?
Input
The first line of input contains an integer n (1 ≤ n ≤ 2 · 105 ), the number of nodes in the tree. These
nodes contain the numbers from 1 to n exactly. This is followed by n lines, the ith of which contains
two integers ℓi and ri (i < ℓi ≤ n or ℓi = 0; i < ri ≤ n or ri = 0), describing the values of the left and
right children of the node storing i, where a value of 0 is used to indicate that the corresponding child
does not exist. It is guaranteed that this data describes a binary tree.
Output the lexicographically minimal input permutation that produces the given tree under the insertion
method for skew heaps, followed by the lexicographically maximal such input permutation. These per-
mutations may coincide, in which case you still need to output both. If no input permutation producing
the given tree exists, output impossible.
Input
The first line of input contains an integer t (1 ≤ t ≤ 40), which is the number of test cases. The
descriptions of t test cases follow.
Each test case consists of a single line containing an integer n (2 ≤ n ≤ 107 ), which is the largest
number written on the blackboard.
Over all test cases, the sum of n is at most 107 .
Output
For each test case, if the first player has a winning strategy for the given n, output the word first,
followed by an even integer – any valid first move that can be extended to a winning strategy. If the
second player has a winning strategy, output just the word second.
Explanation of Sample 1: For n = 5, the first player loses the game regardless of the first move.
• If the first player starts with 2, the second player circles 4, and there are no more valid moves left.
• If the first move is 4, the second player circles 2. The first player must then circle 1, and the
second player may pick either of the remaining two numbers (3 or 5) to win.
1
40%
2
80% 10% 30%
50% 40% 60% 50%
100%
3 4 5 2 3
Input
The first line of input contains three integers s, r, and d, where s (1 ≤ s ≤ 10 000) is the number of
stations, r (1 ≤ r ≤ 3) is the number of reservoirs, and d (s ≤ d ≤ 20 000) is the number of ducts. The
stations are numbered from 1 to s and the reservoirs are numbered from s + 1 to s + r, in decreasing
order of altitude. The factory’s Flubber initially flows into station 1.
49th ICPC World Championship Problem C: Bride of Pipe Stream © ICPC Foundation 5
Each of the remaining d lines starts with two integers i and n, where i (1 ≤ i ≤ s) is the station that
can drain into this duct, and n (1 ≤ n ≤ 10) is the number of outputs of this duct. The remainder of the
line contains n pairs of integers o and p, where o (i < o ≤ s + r) is a station or reservoir to which this
duct drains, and p (1 ≤ p ≤ 100) is the percentage of the Flubber entering the duct that will drain to o.
The o values for a given duct are distinct. Every station has at least one duct that it can drain into. The
percentages for a given duct’s outputs will sum to at most 100.
Output
Output a single percentage f , which is the highest possible percentage such that, for some configuration
of station drainage, all reservoirs receive at least f % of the factory’s produced Flubber. Your answer
should have an absolute error of at most 10−6 .
49th ICPC World Championship Problem C: Bride of Pipe Stream © ICPC Foundation 6
Problem D
Buggy Rover
Time limit: 2 seconds
The International Center for Planetary Cartography (ICPC) uses
rovers to explore the surfaces of other planets. As we all know,
other planets are flat surfaces which can be perfectly and evenly
discretized into a rectangular grid structure. Each cell in this grid
is either flat and can be explored by the rover, or rocky and cannot.
Today marks the launch of their brand-new Hornet rover. The
rover is set to explore the planet using a simple algorithm. Inter-
nally, the rover maintains a direction ordering, a permutation of Mars rover being tested near the Paranal Observatory.
the directions north, east, south, and west. When the rover makes CC BY-SA 4.0 by ESO/G.
Hudepohl on Wikimedia Commons
a move, it goes through its direction ordering, chooses the first
direction that does not move it off the face of the planet or onto
an impassable rock, and makes one step in that direction.
Between two consecutive moves, the rover may be hit by a cosmic ray, replacing its direction ordering
with a different one. ICPC scientists have a log of the rover’s moves, but it is difficult to determine by
hand if and when the rover’s direction ordering changed. Given the moves that the rover has made, what
is the smallest number of times that it could have been hit by cosmic rays?
Input
The first line of input contains two integers r and c, where r (1 ≤ r ≤ 200) is the number of rows on the
planet, and c (1 ≤ c ≤ 200) is the number of columns. The rows run north to south, while the columns
run west to east.
The next r lines each contain c characters, representing the layout of the planet. Each character is either
‘#’, a rocky space; ‘.’, a flat space; or ‘S’, a flat space that marks the starting position of the rover.
There is exactly one ‘S’ in the grid.
The following line contains a string s, where each character of s is ‘N’, ‘E’, ‘S’, or ‘W’, representing the
sequence of the moves performed by the rover. The string s contains between 1 and 10 000 characters,
inclusive. All of the moves lead to flat spaces.
Output
Output the minimum number of times the rover’s direction ordering could have changed to be consistent
with the moves it made.
Explanation of Sample 1: The rover’s direction ordering could be as follows. In the first move, it either
prefers to go north, or it prefers to go south and then north. Note that in the latter case, it cannot move
south as it would fall from the face of the planet. In the second move, it must prefer to go north. In the
third move, it must prefer to go east. In the fourth move, it can either prefer to go north, or east and
then north. It is therefore possible that it was hit by exactly one cosmic ray between the second and
third move, changing its direction ordering from N??? to EN?? where ‘?’ stands for any remaining
direction.
Explanation of Sample 2: It is possible the rover began with the direction ordering NESW, which is
consistent with all moves it makes.
Input
The first line of input contains two integers n and m, where n (2 ≤ n ≤ 2 · 105 ) is the number of cities,
and m (1 ≤ m ≤ 4 · 105 ) is the number of couriers that will be hired. Couriers are numbered 1 to m, in
the order they are hired. This is followed by m lines, the ith of which contains two distinct integers ai
and bi (1 ≤ ai , bi ≤ n), denoting the home and destination cities, respectively, for courier i.
Output
Output m integers, denoting the number of pairs of connected cities after hiring the first 1, 2, . . . , m
couriers.
Explanation of Sample 1:
1. After the first courier is hired, cities 1 and 2 are connected.
2. After the second courier is hired, cities 2 and 3 are connected. Note, however, that cities 1 and 3
are still not connected. Even though there’s a courier moving between cities 1 and 2, and a courier
moving between cities 2 and 3, they never meet each other.
3. After the third courier is hired, cities 3 and 4 are connected and cities 2 and 4 are connected. For
example, one way to deliver a package from city 2 to city 4 is:
4. After the fourth courier is hired, all six pairs of cities are connected.
Figure F.1: One possible plant ordering for the first sample test case.
You know which pot you would like each cat to stop beside. Can you find a way in which to place the
plants in the pots to achieve this?
Input
The first line of input contains an integer t (1 ≤ t ≤ 10 000), which is the number of test cases. The
descriptions of t test cases follow.
The first line of each test case contains two integers n and m, where n (1 ≤ n ≤ 2 · 105 ) is the number
of cats, and m (1 ≤ m ≤ 2 · 105 ) is the number of catnip plants (and also the number of pots). Catnip
plants are numbered from 1 to m.
The following n lines each describe one cat. The line starts with two integers p and k, where p (1 ≤
p ≤ m) is the pot at which the cat should stop, and k (1 ≤ k ≤ m) is the number of catnip plants the cat
likes. The remainder of the line contains k distinct integers, which are the numbers of the plants that the
cat likes.
Over all test cases, the sum of n is at most 2 · 105 , the sum of m is at most 2 · 105 , and the sum of all k
is at most 5 · 105 .
For each test case, output either yes if it is possible to arrange the catnip plants as described above, or
no if not.
Explanation of Sample 1: In the first test case, a possible ordering of the plants is [2, 1, 5, 3, 4]. This
way, cat 1 will stop at pot 2, as it is the first pot with a plant variety that it likes. Cat 2 will stop there as
well. Cat 3 will continue all the way to pot 4, as shown in Figure F.1.
Figure G.1: Illustration of the sample test cases. Shading denotes elevation, and the thick
red lines denote optimal moats.
Input
The first line of input contains an integer t (1 ≤ t ≤ 10 000), which is the number of test cases. The
descriptions of t test cases follow.
The first line of each test case contains four integers w, ℓ, n, and m, where w (1 ≤ w ≤ 106 ) is
the extent of the borderlands from west to east, ℓ (1 ≤ ℓ ≤ 106 ) is the extent from south to north,
n (4 ≤ n ≤ 50 000) is the number of vertices, and m (n − 2 ≤ m ≤ 2n − 6) is the number of triangles
in the provided triangulation.
Output
For each test case, if it is possible to construct a lava moat at a single elevation that connects the western
border to the eastern border, output the minimum length of such a moat, with an absolute or relative
error of at most 10−6 . Otherwise, output impossible.
Unfortunately, you just realized that you are going to need a score display in order to run the games. In
Contact Bridge, the score for a team starts at 0 and, after various repeatable actions, may be incremented
by certain fixed amounts. There is also a maximum value – if the team’s score would be incremented
above the maximum, it will instead be capped there. You want the team’s score to be visible at all times,
so you will need to prepare some signs, each with a single digit printed on it, that can be arranged to
show the score.
Unfortunately the dean’s “funding” is running short, and these signs are expensive. Figure out the
minimum set of signs you need to purchase to show any score that is possible to achieve during the
game. Note that you won’t need any 9 signs, as any 6 sign can be turned upside-down to make a 9.
Input
The first line of input contains two integers m and n, where m (1 ≤ m ≤ 1018 ) is the maximum score
value, and n (1 ≤ n ≤ 10) is the number of different ways of scoring. This is followed by n lines, each
containing an integer p (1 ≤ p ≤ 1 000), which is the number of points awarded for a type of action in
the game. No two types of action are awarded the same number of points.
Output
For each digit from 0 to 8 in increasing order, output two integers: the digit and the number of signs
with that digit that you need to purchase. Omit digits where the number of signs needed is 0.
You are standing behind the machine and notice that a maintenance panel has been left open. When
you stick your hand inside, you are able to secretly rotate any of the wheels by any number of steps,
thus changing the symbol shown on that wheel. You want to win a jackpot, which will happen if all the
wheels show the same symbol at the same time. Unfortunately, you cannot see the symbols from your
position, so you asked your good friend to help you. The friend is standing in front of the machine and
she tells you the number of distinct symbols in the sequence she can currently see. Can you win the
jackpot by manipulating the wheels if your friend updates the information after every action you make?
Interaction
The first line of input contains an integer n (3 ≤ n ≤ 50), giving the number of wheels and symbols in
the machine.
Interaction then proceeds in rounds. In each round, one line of input becomes available, containing an
integer k (1 ≤ k ≤ n), the number of distinct symbols in the current sequence. If k > 1, output two
integers i and j (1 ≤ i ≤ n; −109 ≤ j ≤ 109 ), representing your action: rotating the ith wheel by
j positions, where negative numbers indicate rotating in the opposite direction. Otherwise, if k = 1,
indicating that all wheels show the same symbol, your program must exit without printing more output.
At most 10 000 actions are allowed – if your submission uses more rounds, it will not be accepted. It
is guaranteed that the initial configuration of wheels does not already have all wheels showing the same
symbol (k > 1 in the first round).
The judge program will not behave in an adversarial way, which means the initial configuration is fixed
before the first action.
A testing tool is provided to help you develop and test your solution.
9
8
7
6
5
4
3
2
1
0
Input
The input consists of a single line containing two integers n and h, where n (1 ≤ n ≤ 2 · 105 ) is the
number of cups and h (1 ≤ h ≤ 4 · 1010 ) is your favorite number.
Output
If it is possible to build a tower with height h, output the heights of all the cups in the order they should
be placed to achieve this. Otherwise, output impossible. If there is more than one valid ordering of
cups, any one will be accepted.
A B A B
D C D C
However, Blackbeard was as stubborn as he was cruel and would not let such pesky ambiguities stop
him. To find the perfect hiding spot for his treasure, he scoured the seven seas for a region of the ocean
where the two methods described above yield the same results for each unit square (or maybe he forced
some of his pirates to do a bit of terraforming work to achieve this – scholars disagree).
Back in the present, you are preparing an expedition to retrieve the treasure, and would like to figure out
at what depth the treasure could be buried. Specifically, given the remaining depth data of the map, you
should calculate the smallest possible depth at the treasure location.
Input
The first line of input contains five integers n, m, k, tx , and ty , where n and m (2 ≤ n, m ≤ 3 · 105 )
denote the maximum coordinates of the grid, k (1 ≤ k ≤ 3 · 105 ) is the number of known depths, and
(tx , ty ) is the location of the treasure (1 ≤ tx ≤ n; 1 ≤ ty ≤ m). Each of the next k lines contains three
integers x, y, and d (1 ≤ x ≤ n; 1 ≤ y ≤ m; 0 ≤ d ≤ 109 ), indicating that the depth at coordinate
(x, y) of the grid equals d. Each pair (x, y) appears in the input at most once.
If the provided data points can be extended to a valid map (that is, a map where, for each unit square, the
two methods of interpolation yield the same results, and all points have non-negative depth), output one
integer: the smallest possible depth of (tx , ty ) – it can be shown that this is always an integer. Otherwise,
output impossible.
Explanation of Sample 5: Even though the depth of (2, 2) is given in the input, the provided data points
cannot be extended to a valid map, so the correct answer is impossible.
N
E
N
7 •
W
E
contest
6
SW
SE
5 S
4
3
2
1 •
awards ceremony
x
1 2 3 4 5 6 7 8 9 10 11
Figure L.1: Sample Input 1 and a path that minimizes the sun shining in your eyes.
Input
The first line of input contains five integers n, xc , yc , xa , and ya , where n (0 ≤ n ≤ 105 ) is the number of
shaded areas, (xc , yc ) is the location of the contest, and (xa , ya ) is the location of the awards ceremony
(−106 ≤ xc , yc , xa , ya ≤ 106 ). The sun shines in the direction (0, 1) from south towards north. You
look into the sun if you walk in direction (x, y) for any y < 0 and any x.
The next n lines describe the shaded areas, which are axis-aligned rectangles. Each of these lines
contains four integers x1 , y1 , x2 , and y2 (−106 ≤ x1 < x2 ≤ 106 ; −106 ≤ y1 < y2 ≤ 106 ).
The southwest corner of the rectangle is (x1 , y1 ) and its northeast corner is (x2 , y2 ). The rectangles
describing the shaded areas do not touch or intersect.
Output
Output the minimum distance you have to walk with the sun shining in your eyes. Your answer must
have an absolute or relative error of at most 10−7 .
Explanation of Sample 1: Figure L.1 shows an optimal path from the contest location to the awards
ceremony location with 5 segments. On the first segment you walk away from the sun. On the second
and fourth segments you walk towards the sun but in a shaded area. On the third and fifth segments you
walk towards the sun outside the shaded areas. The total length of these two segments is 3.