2018 German Collegiate Programming Contest GCPC 18 en
2018 German Collegiate Programming Contest GCPC 18 en
Contest 2018
June 16th
Problems
A Attack on Alpha-Zet
B Battle Royale
C Coolest Ski Route
D Down the Pyramid
E Expired License
F Fighting Monsters
G GPS
H Hyper Illuminati
I It’s Time for a Montage
J Jigsaw Puzzle
K Kitchen Cable Chaos
L Logic Puzzle
M Mountaineers
This page is intentionally left (almost) blank.
Problem A: Attack on Alpha-Zet
Space pirate Captain Krys has recently acquired a map of the
artificial and highly secure planet Alpha-Zet which he has been
planning to raid for ages. It turns out the whole planet is built
on a 2D plane with modules that serve as one room each. There
is exactly one module at every pair of integer coordinates and
modules are exactly 1 × 1 units big. Every module is bidirection-
ally connected to at least one adjacent module. Also, for any two
modules there exists exactly one path between them. All in all
the modules create a rectangular maze without any loops.
On the map Captain Krys has marked several modules he wants Figure A.1: Illustration of
to visit in exactly the marked order. What he intends to do there Sample Input 2
is none of your business, but he promises you a fortune if you
determine the number of modules he has to walk through along
the route (since there are no loops he will always take the direct route from one marked module
to the next). The first marked module indicates where he starts his journey, the last where he
wants to finish.
Input
The input consists of:
• one line with two integers h and w (2 ≤ h, w ≤ 1 000) describing the height and the width
of the maze.
• h + 1 lines follow, describing the maze in ASCII, each line containing 2 · w + 1 characters.
The description always follows these rules:
– In every row, columns with odd index (starting at index 1) contain either vertical
walls or spaces and columns with even index contain either horizontal walls or
spaces.
– The first row describes the northern wall of the maze (which always consists only of
horizontal walls). Every subsequent row describes a row of modules.
– A module is located at every even column index. Its western and eastern walls are
located at the directly neighboring odd column indices respectively, its northern wall
is located at the same column index but one row above and its southern wall can be
found at its own position. If a wall is missing, the corresponding position contains a
space instead.
• After the description of the maze, an integer m (2 ≤ m ≤ 104 ) is given.
• Each of the following m lines describes a marked module with two integer coordinates
x and y (1 ≤ x ≤ h; 1 ≤ y ≤ w). The first pair of coordinates is the start point of the
journey, the last pair the end point. Modules may appear multiple times but never twice or
more in a row. (1, 1) is the top left module and (h, w) is the bottom right module.
It is guaranteed that the maze itself is enclosed. Furthermore it is guaranteed that exactly one
path exists between any two modules.
Output
Output one integer, the number of modules Captain Krys has to travel through if he follows the
route in the exact order given in the input.
Input
The input consists of:
• one line with two integers xc , yc specifying your current location;
• one line with two integers xd , yd specifying your destination;
• one line with three integers xb , yb , rb specifying the center and radius of the blue circle;
• one line with three integers xr , yr , rr specifying the center and radius of the red circle.
All coordinates have an absolute value of at most 1 000, and 1 ≤ rb , rr ≤ 1 000. The red circle
is strictly inside the blue circle. Your current location and destination are strictly inside the blue
circle and strictly outside of the red circle, and the direct path between them is blocked by the
red circle.
Output
Output the length of the shortest path that does not leave the blue or enter the red circle. The
output must be accurate up to a relative or absolute error (whichever is lower) of 10−7 .
Input
The input consists of:
• one line with two integers n (2 ≤ n ≤ 1000) and m (1 ≤ m ≤ 5000), where n is the
number of (1-indexed) connecting points between slopes and m is the number of slopes.
• m lines, each with three integers s, t, c (1 ≤ s, t ≤ n, 1 ≤ c ≤ 100) representing a slope
from point s to point t with condition measure c.
Points without incoming slopes are mountain tops with beautiful scenery, points without outgoing
slopes are valleys. The helicopter can land on every connecting point, so the friends can start
and end their tour at any point they want. All slopes go downhill, so regardless of where they
start, they cannot reach the same point again after taking any of the slopes.
Output
Output a single number n that is the maximum sum of condition measures along a path that the
friends could take.
Sample visualization
8
3 5
1 2 3
However, I am not interested in completing the pyramid – instead, I would much rather go
underground. Thus, for a sequence of n non-negative integers, I will write down a sequence of
n + 1 non-negative integers below it such that each number in the original sequence is the sum
of the two numbers I put below it. However, there may be several possible sequences or perhaps
even none at all satisfying this condition. So, could you please tell me how many sequences
there are for me to choose from?
Input
The input consists of:
• one line with the integer n (1 ≤ n ≤ 106 ), the length of the base sequence.
• one line with n integers a1 , . . . , an (0 ≤ ai ≤ 108 for each i), forming the base sequence.
Output
Output a single integer, the number of non-negative integer sequences that would have the input
sequence as the next level in a number pyramid.
Input
The input consists of:
• one line with an integer n (1 ≤ n ≤ 105 ), the number of applications Paul has to file;
• n lines, each with two real numbers a and b (0 < a, b < 100), where a × b is the desired
aspect ratio of one application.
All real numbers are given with at most 5 decimal places after the decimal point.
Output
For each application, if it is possible to represent the desired aspect ratio by two prime numbers
p and q, output one line with p and q. Otherwise, output impossible. If multiple solutions
exist, output the one minimizing p + q.
A B
4 → 7
4 ← 3
1 → 3
1 ← 2
-1 2
Images by OpenClipart-Vectors on Pixabay and from PhantomOpenEmoji.
One of Emma’s most beloved cards in the game is a spell called Fight! which states:
Pick two monsters. They fight each other to the death. If the surviving monster has
a power of exactly 1 left, return this card to your hand.
Of course, Emma would like to play as efficiently as possible by picking two monsters such that
Fight! is returned to her hand. However, there are often a lot of monsters on the board, which
makes it very time consuming to figure out whether this can be done or not. Can you help her
find two monsters she can pick so that she gets the card back?
Input
The input consists of:
• one line with an integer n (2 ≤ n ≤ 105 ), the number of monsters;
• one line with n integers m1 , . . . , mn (1 ≤ mi ≤ 106 ), giving the power of each monster.
Output
If there is no pair of monsters that Emma can pick, output impossible. Otherwise, output
two distinct integers i, j (1 ≤ i, j ≤ n), where i is the index of the monster that starts the fight
and j is the index of the other monster. If multiple solutions exist, any of them will be accepted.
Input
The input consists of:
• one line with an integer s (1 ≤ s ≤ 104 ), the number of satellites;
• one line with two real numbers `o , `a , the longitude and latitude of your position;
• s lines, each with four real numbers φ, ψ, r, x describing one of the satellites.
For the longitude `o ∈ [−180, 180], positive values represent east and negative values west.
For the latitude `a ∈ [−90, 90], positive values represent north and negative values south.
The format for the satellite descriptions is as follows:
• φ ∈ [−180, 180] – the reference longitude at which the orbit intersects with the Earth’s
equator (each orbit intersects the equator (at least) twice). Positive longitude represents
east, negative longitude represents west.
• ψ ∈ [0, 360] – the angle between orbit and the Earth’s eastbound equator when passing
through the equator at longitude φ. Values between 0 and 180 indicate northbound
movement while values between 180 and 360 indicate southbound movement. If the orbit
is coplanar to the equator, the angle ψ will be 0 or 180.
• r ∈ [7 000, 106 ] – the radius of the satellite’s orbit in kilometers.
• x ∈ [0, 1) – the fraction of the orbit the satellite had covered at the time when its signal
was sent, starting from its intersection with the equator at φ.
1
In practice the clocks are not completely synchronous – because of general relativity. In this problem we
assume that these effects do not exist.
2
Another simplification.
Note: All real numbers in the input are given with at most 3 decimal places after the decimal
point. It is guaranteed that each satellite has a distance of at least 1 meter from the tangent plane
at your position (this means that you can safely use floating point arithmetic in your solution).
Output
For every satellite, output no signal if a signal sent from that satellite does not reach you.
If the satellite’s signal does reach you, output the time that the signal needs to travel from the
satellite to your position, in seconds. The time must be accurate up to a relative or absolute error
(whichever is lower) of 10−7 .
Figure H.1: A 3-dimensional hyper pyramid with 3 steps consisting of 14 blocks in total.
Input
The input consists of:
• one line with a single integer m (1 ≤ m ≤ 1016 ) . This integer is the meaningful number
the leaders have chosen.
Output
If a hyper-step pyramid matching all the requirements exists, output a single line with two
integers n and s, the dimension of the pyramid and its number of steps. If none exists, output
impossible. If multiple solutions exist, any will be accepted.
Input
The input consists of:
• one line with an integer n (1 ≤ n ≤ 1 000), giving the number of rival pairs.
• one line with n integers h1 , . . . , hn (1 ≤ hi ≤ 1 000 for each i), the i-th value giving the
Power Level of the i-th most important hero.
• one line with n integers v1 , . . . , vn (1 ≤ vi ≤ 1 000 for each i), the i-th value giving the
Power Level of the i-th most important villain.
Output
Output a single integer, the minimum number of days the heroes need to spend training in order
for their side to win.
0 0 0
0
1 6
4 3
5 0
7
1 5
2
0
3 6 4
2 0
0 0 0
More formally:
• There are n square-shaped pieces, numbered from 1 to n, which need to be arranged side
by side to form a single rectangle. All n pieces have to be used.
• The edges of the pieces are either straight or irregular. Straight edges must be placed on
the boundary of the assembled rectangle and irregular edges must be placed on the inside.
• The irregular edges are all shaped differently, so that each edge shape occurs exactly two
times, on two different puzzle pieces. Two pieces can only be placed next to each other if
the shapes of the corresponding edges match.
• You may rotate the pieces, but you may not flip them over.
Input
The input consists of:
• one line with one integer n (1 ≤ n ≤ 3 · 105 ), the number of pieces;
• n lines, each with four integers, the i-th line gives the connections (edge shapes) of the
i-th piece in counter-clockwise order.
A connection of type 0 stands for a straight edge. The other connection types are numbered with
consecutive positive integers starting from 1 and each of them occurs exactly two times, on two
different lines.
Output
If the pieces cannot be assembled as described above, output impossible. Otherwise, output
the solved puzzle in the following format:
• one line with two integers h, w (h, w ≥ 1, h · w = n), the height and width of the grid;
• h lines, each with w integers, the numbers of the pieces.
Any rotation of the correct solution will by accepted.
1 2
Figure K.1: Four cables of different lengths connect the controller with the sandwich maker,
with different overlaps. Connection 1 has the maximal overlap, connection 2 the minimal overlap
and all other connections are in between. The quality of this setup is 0.
Considering all possible arrangements of cables, find the one with the best quality.
Input
The input consists of:
• one line with two integers n, g (1 ≤ n ≤ 60, 11 ≤ g ≤ 1 000), the number of cables and
the distance to be covered in centimeters, measured between the casings of the controller
and the sandwich maker;
• n lines, each with an integer d (11 ≤ d ≤ 1 000), giving the lengths of the cables
(including the stripped ends).
Each of the n cables can be used at most once.
Output
Output one number, the best achievable quality. The quality must be accurate up to a relative
or absolute error (whichever is lower) of 10−7 . If no arrangement fits your needs, output
impossible.
1 1 2 1 1 1 1 2 1 1
1 2 3 2 1 1 2 3 2 1
1 2 3 2 1 1 2 3 2 1
0 1 1 1 0 0 1 1 1 0
You are given a two-dimensional grid of cells, initially all white, and you have to color some of
the cells in black. You are also given a grid of clue numbers, which extends beyond the borders
of the puzzle grid by one cell in each direction. The number in a cell indicates (exactly) how
many cells in the 3 × 3 block centered at this cell need to be colored in black. You may not color
any cells outside of the original grid.
Input
The input consists of:
• one line with two integers h, w (1 ≤ h, w ≤ 100), the height and width of the puzzle;
• h + 2 lines, each with w + 2 integers c1 , . . . , cw+2 (0 ≤ ci ≤ 9), the clue numbers.
Output
If the given clue numbers are inconsistent, output impossible. Otherwise, output h lines
with w characters each, the solution to the puzzle. Use X for black cells and . for white cells. If
there are multiple solutions, any of them will be accepted.
Input
The input consists of:
• one line with three integers m, n and q (1 ≤ m, n ≤ 500, 1 ≤ q ≤ 105 ), where m is the
number of rows, n is the number of columns, and q is the number of mountaineers;
• m lines, each with n integers h1 , . . . , hn (1 ≤ hi ≤ 106 ), the height values in the map;
• q lines, each with four integers x1 , y1 , x2 , y2 (1 ≤ x1 , x2 ≤ m, 1 ≤ y1 , y2 ≤ n), describing
a mountaineer who wants to trek from (x1 , y1 ) to (x2 , y2 ).
The top left cell of the grid has coordinates (1, 1) and the bottom right cell has coordinates
(m, n).
Output
Output q integers, the minimal height for each mountaineer, in the same order as in the input.