NCPC 2021 Problems
NCPC 2021 Problems
NCPC 2021
October 9, 2021
Problems
A Antenna Analysis
B Breaking Bars
C Customs Controls
D Deceptive Directions
E Eavesdropper Evasion
F Fortune From Folly
G Grazed Grains
H Hiring Help
I Intact Intervals
J Joint Jog Jam
K Knot Knowledge
L Locust Locus
M Marvelous Marathon
• Your solution programs must read input from standard input (e.g. System.in in Java or
cin in C++) and write output to standard output (e.g. System.out in Java or cout
in C++). For further details and examples, please refer to the documentation in the help
pages for your favorite language on Kattis.
• For information about which compiler flags and versions are used, please refer to the
documentation in the help pages for your favorite language on Kattis.
• Your submissions will be run multiple times, on several different inputs. If your submission
is incorrect, the error message you get will be the error exhibited on the first input on which
you failed. E.g., if your instance is prone to crash but also incorrect, your submission
may be judged as either “Wrong Answer” or “Run Time Error”, depending on which is
discovered first. The inputs for a problem will always be tested in the same order.
• If you think some problem is ambiguous or underspecified, you may ask the judges for a
clarification request through the Kattis system. The most likely response is “No comment,
read problem statement”, indicating that the answer can be deduced by carefully reading
the problem statement or by checking the sample test cases given in the problem, or that
the answer to the question is simply irrelevant to solving the problem.
• In general we are lenient with small formatting errors in the output, in particular whitespace
errors within reason, and upper/lower case errors are often (but not always) ignored. But
not printing any spaces at all (e.g. missing the space in the string “1 2” so that it becomes
“12”) is typically not accepted. The safest way to get accepted is to follow the output
format exactly.
• For problems with floating point output, we only require that your output is correct up to
some error tolerance. For example, if the problem requires the output to be within either
absolute or relative error of 10−4 , this means that
– If the correct answer is 0.05, any answer between 0.0499 and .0501 will be accepted.
– If the correct answer is 500, any answer between 499.95 and 500.05 will be accepted.
Any reasonable format for floating point numbers is acceptable. For instance, “17.000000”,
“0.17e2”, and “17” are all acceptable ways of formatting the number 17. For the definition
of reasonable, please use your common sense.
Problem A
Antenna Analysis
Time limit: 4 seconds
Åke has heard that there may be some suspicious 5G radiation in his city. To test this, he uses
the antenna on his roof to measure the 5G level each day. However, he does not know how he
should analyze the data.
We are given the measurements for n consecutive days as a list of numbers x1 , . . . , xn (where
xi denotes the measurement for day i) and a constant c that measures how much Åke expects the
radiation to vary from day to day. We want to find, for each day i, the most significant difference
between the measurement on day i and any earlier day, after the expected variations are taken
into account. More precisely, the goal is to find the maximum value of
|xi − xj | − c · |i − j|
where j ≤ i. I.e., we want to find a large difference in 5G level that has happened recently.
Input
The first line of input contains the two integers n and c (1 ≤ n ≤ 4 · 105 , 1 ≤ c ≤ 106 ), the
number of measurements and expected day-to-day variation. The second input line contains the
n integers x1 , x2 , . . . , xn (1 ≤ xi ≤ 106 for i = 1, 2, . . . , n), giving the measurements of the n
days.
Output
Output n integers y1 , . . . , yn , where yi is the most significant difference on day i.
Figure B.1: Explanation of Sample Input 1. First make a vertical break as shown on the 3 × 5
bar (orange), then make a horizontal break on the newly created 3 × 2 bar (blue). This way Elsa
and Asle can each get one 1 × 2, one 2 × 2, and one 3 × 3 bar, in total 15 squares each.
Input
The first line of input contains two integers n and t (1 ≤ n ≤ 50, 1 ≤ t ≤ 900), where n is the
number of bars Selma has, and t is the least number of squares she wants each grandchild to
receive. Then follows a line containing n bar descriptions. A bar description is on the format
“axb” for two integers 1 ≤ a, b ≤ 6.
You may assume that the total amount of chocolate squares among the n bars is at least 2t.
Output
Output the minimum number of breaks needed to obtain two identical collections of bars, each
having a total of at least t squares.
Input
The first line of input contains three integers n, m, and k (2 ≤ n ≤ 105 , 1 ≤ m ≤ 2 · 105 ,
0 ≤ k ≤ n), the number of checkpoints, roads, and Norwegian customs units. The second
line of input contains n positive integers t1 , . . . , tn (1 ≤ ti ≤ 104 ), the time it takes to pass
through each checkpoint. Then follow m lines of input each containing two integers u and v
(1 ≤ u, v ≤ n), meaning that there is a road between checkpoints u and v.
It is guaranteed that it is possible to go from any checkpoint to any other checkpoint using
the roads. There is also at most one road between each pair of checkpoints, and no road connects
a checkpoint to itself.
Output
If there is a way to place the customs units so that every smuggler is caught, output a string of
length n, where the ith character indicates which type of customs unit to put at the ith checkpoint
(an ‘N’ for a Norwegian customs unit, and an ‘S’ for a Swedish customs unit). Otherwise, if
there if there is no way to catch every smuggler, output “impossible”.
Input
The first line of input consists of two integers w and h (3 ≤ w, h ≤ 1000), the width and height
of the map. Then follow h lines, each containing w characters, describing the map. Each such
character is either a ‘.’ symbolizing a walkable space, ‘#’ symbolizing an obstacle such as a
body of water, dense forest, or a mountain, or ‘S’ symbolizing the starting point of the directions.
Finally, there is a line containing a string I (1 ≤ |I| ≤ 105 ) consisting only of the characters
‘NWSE’, giving the faulty instruction sequence.
The map has exactly one ‘S’ and its boundary consists only of obstacle cells. The faulty
instruction sequence is such that there is at least one possible location of the treasure.
Output
Output the map in the same format as the input (without the first line specifying the dimensions),
with all possible locations of the treasure indicated by exclamation marks (‘!’).
2 2
5 5
7 7
6 6
4 4
3 3
0 5 10 15 0 5 10 15
Figure E.1: Left: Illustration of a solution to Sample Input 1. Right: sending the message of
length 4 a time step earlier would not be a solution, because the three messages of length 6, 4,
and 3 would then be exposed to an eavesdropper listening in from time step 5 to time step 15.
Input
The first line of input contains the two integers n and x (1 ≤ n ≤ 20 000, 1 ≤ x ≤ 10 000), the
number of messages Alice wants to send and the number of time steps someone may listen in.
This is followed by a line containing n integers t1 , . . . , tn (1 ≤ ti ≤ 10 000), the number of time
steps it takes to transmit each message.
Output
Output the minimum number of time steps to complete transmission of all n messages so that at
most two of them can be exposed.
Input
The only line of the input contains the two integers n and k (1 ≤ k ≤ n ≤ 6), and the real
number p (0 < p ≤ 1 and p has at most four decimals after the decimal point), with meanings as
described above.
Output
Output the expected number of lootboxes Ómar must open, with a relative error of at most 10−6 .
It is guaranteed that the input is such that this expected number does not exceed 109 .
Input
The first line of input contains one integer n (1 ≤ n ≤ 10), the number of UFOs. Then follow n
lines, the ith of which contains the three integers xi , yi , and ri (0 ≤ xi , yi ≤ 10, 1 ≤ ri ≤ 10),
describing the crop circle left by the ith UFO.
Output
Output the total area covered by the crop circles in the input, with a relative error of at most
1/10.
Input
The first line of input consists of a single integer n (0 ≤ n ≤ 2 · 105 ), the number of coders
(initially) at the company. The employees are numbered from 1 to n (names are too personal).
Then follow n lines, the ith of which contains two integers `i and fi (1 ≤ `i , fi ≤ 108 ), the
number of lines of code and the number of bugs fixed per hour by coder i.
Next follows a line with a single integer e (1 ≤ e ≤ 105 ), the number of events. This is
followed by e lines, describing the events in chronological order. An event is a line in one of the
following two forms:
• “c t ` f ”, for three integers t, ` and f (1 ≤ t ≤ 100, 1 ≤ `, f ≤ 108 ): a request to take
in a consultant for a project of t hours, where the consultant would write ` lines of code
and fix f bugs in those t hours.
• “q i”, for an integer i (1 ≤ i ≤ n): coder i quit the company.
You may assume that no coder quits more than once.
Output
For each request to take in a consultant, output “yes” if the request is approved, and “no” if it
is not approved.
Input
The first line of input contains a single integer n (2 ≤ n ≤ 106 ), the number of modules. The
second line contains the n integers a1 , . . . an (0 ≤ ai ≤ 109 ). The third and final line contains
the n integers b1 , . . . , bn (0 ≤ bi ≤ 109 ).
The list b is guaranteed to be a rearrangement of the list a.
Output
Print one integer, the number of safe separations modulo 109 + 7.
Input
The input consists of a single line containing eight integers describing four points:
in that order. Each point is given by two integers x and y (0 ≤ x, y ≤ 104 ), the coordinates of
the point.
Output
Output the maximum distance between Kari and Ola during their run, with an absolute or relative
error of at most 10−6 .
Sample Input 1 Sample Output 1
0 0 0 0 1 1 2 2 1.4142135624
Input
The first line of input consists of an integer n (2 ≤ n ≤ 50), the number of knots Sonja needs to
learn. This is followed by a line containing n distinct integers x1 , . . . , xn (1 ≤ xi ≤ 1 000), the
knots that Sonja needs to learn. Finally, the last line contains n − 1 distinct integers y1 , . . . , yn−1
(1 ≤ yi ≤ 1 000), the knots that Sonja has learned so far. You may assume that each knot Sonja
has learned is one of the n knots she was supposed to learn.
Output
Output the number of the remaining knot that Sonja needs to learn.
Input
The first line of input contains a single integer k (1 ≤ k ≤ 99), the number of pairs of periodical
cicadas. Then follow k lines, each containing three integers y, c1 and c2 (1800 ≤ y ≤ 2021,
1 ≤ c1 , c2 ≤ 99), the year this pair was last observed and cycle lengths for the first and second
species, respectively. You may assume that none of the k pairs reappears earlier than 2022.
Output
Output the first year a pair reappears.
9 9 9 4 4 4 4 6 6 6 6
7 7 7 7 7 7 7 7 7 5 8 8
Figure M.1: Illustration of Sample Input 1. The numbers in the cells indicate the beauty value
for each meter of the road (with omitted values being 0). The highlighted cells and arrows mark
the optimal race, involving two U-turns.
Input
The first line of input contains the three integers m, x and n (1 ≤ m ≤ 109 , 1 ≤ x ≤ 2m,
0 ≤ n ≤ 200), the length of the road, the length of the race and the number of segments.
This is followed by n lines describing the segments. Each such line contains three integer
a, b, v (0 ≤ a, b ≤ m, 1 ≤ v ≤ 109 , and a 6= b), describing a segment with endpoints a and b
having beauty value v. If a < b, this is the segments of cells in the top row of the grid in the
range [a, b), and if a > b, this is the segments of cells in the bottom row of the grid in the range
[b, a).
The parts of the road that are not covered by any segments have beauty value 0. Each cell
in the grid is covered at most once (that is, there are no overlapping segments in the same
direction).
Output
Output the maximum possible total beauty the race can have.