Problemset RPC08
Problemset RPC08
Input
The input consists of:
• One line with a string s (1 ≤ |s| ≤ 60), a German word. The word only consists of letters ‘a’ to
‘z’ and ‘A’ to ‘Z’. All letters are given in lowercase, except for the first one which is either upper- or
lowercase.
Output
Output “yes” if the word is sorted alphabetically, and “no” otherwise.
Example
Input Output
bekloppt yes
Ente no
Rommee yes
In honour of the Olympic ethos, an archer is faced with a revered task: to light a series of bonfires leading
to the Olympic Torch. The archer must establish a network of bonfires, across a grid of hills with heights
between 0 and 9, such that they may light the Olympic Torch in the south-east corner of the grid. Bound
by ancient tradition, the archer may only shoot in straight lines that follow the cardinal directions of
north-south or east-west. You must find the smallest number of additional bonfires needed to create an
unbroken chain of bonfires between the starting point and the Olympic Torch.
The rules for establishing visibility between bonfires are:
• Bonfires can be lit when they are within a direct line of sight of another bonfire that is already
burning (even if archers may shoot in a parabola, assume that if they cannot see the bonfire, they
cannot hit it).
• A mountain is considered to block the line of sight between two bonfires if its peak lies strictly above
the straight line between the two peaks of the bonfires in question. When determining if there is a
line of sight, assume that each grid cell’s terrain height is a peak of zero width in the middle of the
cell.
• An archer can only shoot north, west, east or south in a single shot. Shooting in a combination of
these directions is not allowed due to the Olympic traditions.
For instance, consider an example with terrain heights in a line like “124”:
• A bonfire placed on terrain height 1 can see a bonfire placed on terrain height 4 because the terrain
with height 2 is not tall enough to obstruct the view.
• However, if the terrain heights were “134”, a bonfire on height 1 would not see a bonfire on height
4 due to the peak of height 3 blocking the line of sight.
• In cases such as “222” or “123”, bonfires at the first and last point can see each other since the
intermediate heights are not obstructing the direct view.
What is the minimum number of additional bonfires you need to place in order to enable signalling from
the bonfire at the north-west to the Olympic Torch at the south-east? Assume that the initial bonfire
and the Olympic Torch have already been placed.
Input
The input consists of:
• One line with two integers r and c (2 ≤ r, c ≤ 100), the number of rows and the number of columns
in the terrain, respectively.
• Then r lines follow, each with c digits. The digits signify the height h (0 ≤ h ≤ 9) of each mountain.
Output
Output the minimum number of additional bonfires needed in order to enable signalling from the bonfire
at the north-west corner to the Olympic Torch at the south-east corner.
Example
Input Output
5 6 3
112121
198776
412221
766611
422231
5 5 5
25688
59998
69996
89995
88653
2 18 6
011222222322222110
011222222322222110
On Halloween, Quirrell suddenly enters the Great Hall and claims to have seen a troll in the dungeons.
The evacuation of the students into their dormitories is started immediately. At the same time, a group
of teachers prepares to search the dungeons for the troll.
The dungeons were constructed over a long period of time and are a system of connected corridors. They
originally consisted of just one long corridor with an entrance and an exit. Over time, new corridors were
added. Each new corridor had its entrance placed on the right-hand wall of an already existing corridor
and ended in the same corridor where it started, but further towards the exit. Furthermore, a newly built
corridor did only intersect with the corridor containing its entrance and exit. To ensure that nobody gets
lost, each corridor contains markers pointing to the corridor’s exit.
The teachers have a map of the dungeons and want to use the fact that there is only one exit to their
advantage. They lock the exit and start all together at the entrance to the system. To ensure that all
of them actually reach the exit, they agree that each of them only moves towards the exit of the current
corridor or enters another corridor through its entrance. Of course, they want to avoid that the troll can
leave through the entrance or even attack them from behind. Hence, they want to move in such a way
that there is never a path between the entrance of the dungeons and the troll which is not blocked by
some teacher. Fortunately, each teacher is capable enough to deal with the troll, that is, it is sufficient if
it can be guaranteed that at least one teacher encounters the troll. How many teachers have to go to the
dungeons?
The system of corridors in the dungeons can also be constructed by composition of systems of corridors.
For each such system there are two positions that are defined as the entrance and the exit of the corridor,
respectively.
• (A+B) denotes that we append the system B to A, that is, the entrance of A is the new entrance of
the system, the exit of B is the new exit and we merge the exit of A with the entrance of B.
• (A*B) denotes that A and B are parallel to each other. This means that the entries of A and B are
merged and form the new entrance and the exits of A and B are merged as well and form the new
exit.
Input
The input consist of a single string of length at most 106 describing the dungeons as explained above.
Output
Print a single integer, the minimum number of teachers needed to examine the entire dungeons.
Example
Input
(()+(()*(()+())))
Output
2
Input
(((()+())*())*((()+(((()+())*())*()))+()))
Output
5
Hermione was really proud of her one million word text she wrote for
her assignment. She was, until she realized that the text must be
handed in on a single piece of paper with limited dimensions. Obvi-
ously, she could have just shortened her text, but Hermione decided to
go another route. She decided to just copy her text to a new piece of
paper, writing a little bit smaller. . . To make this easier, she decided
to first change the line breaks in her text such that the sum of the
height and width of the piece of paper is minimized. Given Hermione’s Hermione’s text. Image by Chris Martin, CC BY-SA
Wikimedia
text with n words and assuming that each character takes up one unit
height and one unit width, what is the minimal height plus width that can be achieved by inserting line
breaks? Note that two words which are on the same line need to be separated by a single space.
Input
The input consists of:
• One line with n space separated words wi (1 ≤ |wi | ≤ 106 ), consisting only of lowercase Latin letters.
It is guaranteed that the total length of the text, i.e. the sum of the lengths of the n words, is not greater
than 106 .
Output
Output a single integer, the sum of the height and width of the smallest piece of paper the text could fit
on.
Example
Input Output
4 11
i am lord voldemort
10 14
i solemnly swear that i am up to no good
Explanation
These are visualizations of the optimal result.
First testcase: 2 + 9
i ␣ am ␣ lord
voldemort
Second testcase: 4 + 10
i ␣ solemnly
swear ␣ that
i ␣ am ␣ up ␣ to
no ␣ good
Problem E. Euroexpress
Source file name: Euroexpress.c, Euroexpress.cpp, Euroexpress.java, Euroexpress.py
Input: Standard
Output: Standard
You want to travel to the Olympic Games this year and already
decided to take the Eurostar to Paris to be more environmentally
friendly. Your next decision is to pick a suitcase for your journey.
Upon reading the terms and conditions, you noticed that there is no
clear size limit for the suitcase. Instead, they provide various two-
dimensional constraints, and your suitcase is compliant if it fits in a
box where each side matches one of the aforementioned constraints.
4 dm
4 dm
m
3d
3 dm
Illustration of Example 2. A suitcase with dimensions 3dm × 8dm × 3dm fits in a box where each side
has either dimension 3dm × 8dm or 4dm × 4dm, i.e. complies either with constraint 3 or 1 of the input.
Since you need to buy a new suitcase anyway, you wonder how much
volume could a suitcase have and still be compliant?
Input
The input consists of:
• n lines, each containing two integers a and b (1 ≤ a, b ≤ 106 ), the dimensions of the constraint in
dm.
Output
Output a single integer, the maximum volume of a suitcase that you can carry with you in dm3 .
Example
Input Output
3 125
2 31
5 5
13 3
4 72
4 4
2 15
8 3
20 1
Frank is an avid football fan and of course attends the final game live
in the Olympic football stadium. It is a huge building, and Frank is
impressed by the maze of staircases connecting the n floors.
Frank notices that from each floor there are exactly two staircases
going up, one to the floor directly above and one to the floor two
levels up (skipping the floor directly above). On each staircase, a
volunteer is distributing little figurines to the spectators, giving one
figurine to everyone climbing up this staircase.
There may be many different routes between any two floors, where
a route consists of a sequence of staircases. Note that, because of
the mass of spectators heading to their seats, Frank can only take M.C. Escher, Relativity
staircases going up. Frank is wondering how many figurines he can
collect when walking up again and again but taking a different route
each time. He considers two routes to be different if they differ in at least one staircase.
Given several queries, each consisting of a pair of two floors, find the maximum number of figurines Frank
can collect when climbing up all the different routes between those floors.
Input
The input consists of:
• One line containing two integers n and q (1 ≤ n ≤ 106 , 1 ≤ q ≤ 5 · 104 ), the number of floors and
the number of queries, respectively.
• q lines, each containing two integers s and t (1 ≤ s ≤ t ≤ n), the numbers of two floors.
Output
For each query, output the total number of figurines Frank can collect when climbing up all different
routes between the two floors. Since this number can be very large, output it modulo 109 + 7.
Example
Input Output
5 4 0
1 1 1
2 3 3
1 3 15
1 5
1000000 1 884826434
1 1000000
Having had a very interesting day watching this year’s Road Cycling
Finals, you and your friends want to enjoy the evening in the lobby
of your hotel. Unfortunately, nobody brought any games to pass the
time. You hope that the hotel provides some games and indeed that’s
the case. In a rack besides many others, you find a copy of the game
Rummikub and you decide with your friends to give it a try.
This game is played with tiles where each tile has a number written
in one of four colours. In the hotel’s version, there are no jokers and
every two tiles either differ in their colour or numerical value. Each The unique solution to Example Input 4.
player has a set of hidden tiles and there are tiles in a common area.
The goal of each player is to place all their tiles into the common area.
In the common area, tiles have to be combined to form groups or runs. A group is composed of at least
3 tiles that all have the same numerical value, but different colours. A run is a combination of at least 3
tiles of the same colour, that have consecutive numerical values. Note that in your version of the game,
1 does not follow 13. So the tiles 12, 13 and 1 in red would not form a valid run.
In each turn players can place their hidden tiles into the common area and combine them with existing
groups and runs. To make it more interesting, they can even rearrange the common area freely – without
removing any tiles of course. The only restriction is that after the move, the common area should be
organized into groups and runs again.
It is your turn now. You ask yourself whether you can place all your hidden tiles into the common area
this turn. Write a computer program to help you.
Input
The input consists of:
• One line with an integer n (1 ≤ n ≤ 52), the number of tiles at your disposal.
• n lines, each containing a string c and an integer v (c ∈ {“Red”, “Yellow”, “Blue”, “Black”},
1 ≤ v ≤ 13), indicating that you have the tile with value v and colour c at your disposal.
No combination of colour and value appears more than once in the input.
Output
Output “possible” if you can place all your tiles into the common area in this round and “impossible”
otherwise.
Example
Input Output
3 possible
Red 1
Black 1
Yellow 1
8 possible
Red 3
Blue 3
Red 1
Red 2
Black 3
Red 4
Red 5
Yellow 3
7 impossible
Red 3
Blue 3
Red 1
Red 2
Black 3
Red 4
Red 5
21 possible
Red 1
Red 2
Red 3
Red 4
Red 8
Blue 3
Blue 4
Blue 5
Blue 7
Yellow 3
Yellow 4
Yellow 5
Yellow 6
Yellow 7
Yellow 8
Black 3
Black 4
Black 5
Black 6
Black 7
Black 8
Explanation
The unique solution to Example Input 4 is depicted in the image of the story.
Hannah is responsible for organizing the time slots at which the Ath-
letics competitions at the Olympic Games are held. She has finally
found one that adheres to the constraints imposed by the Olympic
committee and the other sports. Regrettably, someone else is respon-
sible for assigning athletes to the time slots. After talking to a few
athletes, she realizes that a bad assignment of athletes to time slots
could be unfortunate for some of them.
Therefore, she wants to reduce the maximum number of time slots an 100m hurdles at the 2020 Summer Olympics,
By Bob Ramsak on Wikimedia Commons
athlete could possibly be assigned to by at least one. She wants to do
this by removing some of the time slots as this is the easiest way to
adjust her plan. Of course, she wants to remove as few time slots as possible. Note that an athlete can
only be assigned to several time slots if there is a break of at least one hour in between consecutive ones.
Input
The input consists of:
• n lines, each containing two integers a and b (0 ≤ a < b ≤ 109 ), the start and end time of a time
slot in hours.
Output
Output the minimum number of time slots which have to be removed such that the maximum number of
time slots an athlete could be assigned to decreases by at least one.
Example
Input Output
5 1
0 2
1 4
3 6
5 8
7 9
6 2
0 2
1 3
4 6
5 7
8 10
9 11
5 2
0 2
3 6
1 2
1 4
5 6
Last night, Rubeus Hagrid once again visited his giant half-brother
Grawp in the forbidden forest. Unfortunately, when Grawp saw Ha-
grid’s pink umbrella, he used it to chase barbaric bowtruckles in the
trees. In this chaotic chase, Grawp unfortunately broke Hagrid’s um-
brella into pieces. Back in his homey hood, Ruben Hagrid wants to
assemble a new umbrella. In the past, he only stored the broken pieces
of his wand in it. However, he recently realized that members of the
non-magic community mostly use umbrellas to protect them against
Photo by Pascal Robert, Pixabay
the rain. In order to blend in and hide his true identity whenever he
goes shopping to London, Hagrid wants his new umbrella to look exactly like any other umbrella.
The umbrella should consist of a straight wooden handle with 8 metal ribs of length x meters connected
symmetrically to its top. Hagrid wants to attach the remaining non-stretchable, rainproof, pink fabric he
found in his hood along the full length of the metal ribs, i.e. from the top of the umbrella where all ribs
meet down to the very end of each metal rib. If the umbrella is opened all the way, the fabric between
two metal ribs forms a isosceles triangle. Depending on the amount of fabric that is used, the umbrella
can be opened more or less wide.
Hagrid has up to a square meters of non-stretchable, rainproof, pink fabric available for the umbrella.
Since he is quite a corpulent man, he wonders what the maximum area is that the umbrella can keep dry
if the rain falls perpendicular to the ground and there is absolutely no wind?
Input
The input consists of:
– a (0 < a ≤ 10) is the amount (in square meters) of non-stretchable, rainproof fabric that is
available for the production of a single umbrella.
– x (0 < x ≤ 5) is the length (in meters) of a metal stick.
All real values are given with at most three decimal places.
Output
Print a single real value, the maximum area (in square meters) that can be kept dry. Your solution is
considered correct if the relative and absolute error is less than 10−4 .
Example
Input Output
10.000 0.500 0.7071067812
10.000 5.000 1.2101397319
On the night before the big event, your fellow distance runner Jesse is running a few final laps on the
practice track near the Olympic village. You want to join them; however, it is so foggy that you can not
even see one metre ahead.
You know the running route of Jesse, which they will repeat in loops. Using an optimal strategy, how
long will you need on average to reach Jesse?
Both Jesse and you move in steps on a 2D grid, one grid cell at a time. You both move either up, down,
left, or right a full cell. It takes one second to move one cell. Jesse will always be moving along their fixed
route in a loop, and this route will visit no cell multiple times, except after they start looping their route.
When you start your attempt, Jesse could be on any step of their route, uniformly at random. You may
wait in a cell for 1 second instead of moving. If you and Jesse are in neighbouring cells and move towards
each other, you will meet after 0.5 seconds.
Illustration of Example Input 2, where the optimal expected time is 3.25 seconds.
Input
The input consists of:
• One line with three integers x, y, and n (0 ≤ x ≤ 109 , 0 ≤ y ≤ 109 , 2 ≤ n ≤ 105 ), describing your
initial position (x, y) and the length n of Jesse’s looping route.
• n lines, the ith of which contains two integers xi and yi (0 ≤ xi ≤ 109 , 0 ≤ yi ≤ 109 ), describing
the ith position of Jesse’s route.
All neighbouring steps of Jesse are guaranteed to be of distance exactly 1, so Jesse will always be moving
one step up, down, left, or right. This guarantee also holds for the last and first position, allowing Jesse to
loop their route. Additionally, no two positions in this route are the same. See Figure 4 for an example.
Output
Output the expected time in seconds to reach Jesse assuming an optimal strategy. Your answer should
have an absolute or relative error of at most 10−6 .
Example
Input Output
2 1 2 5.25
4 4
4 5
1 0 6 3.25
0 2
1 2
2 2
2 3
1 3
0 3
Input
The input consists of:
• One line with a string s (1 ≤ |s| ≤ 1000), consisting solely of the characters ‘a’ to ‘z’ and ‘A’ to
‘Z’ as well as spaces. The line neither starts nor ends with a space (but the input ends with a line
break, which does not need to be paid).
Output
Output the minimum cost in cents needed to print the text.
Example
Input Output
Hello 5
AAAAAAAAAA 2
Buy Llamas 11
A AaA 5
The RUNNERS set new world records 34
Input
The input consists of:
• One line containing a single string s describing the sequence of steps Neville wants to apply the spell
to. Each character in s is either 0 or 1, where a 1 indicates that the respective step is a vanishing
step. s contains at most 106 characters and no leading or trailing zeroes.
Output
Output the shortest sufficient form of the spell.
Example
Input Output
1 long
10001110101011110100011010101111 long
100000000000000000000000000000001 long long
Input
The input consists of:
• One line with two integers n and w (1 ≤ n ≤ 2 · 105 , 1 ≤ w ≤ n), where n is the number of people
in the team and w is the maximum number of people in one row.
• One line with n integers h1 , . . . , hn (0 ≤ hi ≤ 109 for each i), where hi is the height of person i.
Output
If it is possible to arrange all people for the photo so that everyone is visible, then output “yes”. Otherwise,
output “no”.
Example
Input Output
5 3 yes
180 160 200 190 150
3 1 no
150 150 140