0% found this document useful (0 votes)
35 views28 pages

NCPC 2021 Problems

The document describes a problem about placing customs units at checkpoints along the Norway-Sweden border to catch smugglers. It provides details on the number of checkpoints, roads between checkpoints, time to pass each checkpoint, and number of available Norwegian and Swedish customs units. The goal is to determine a placement of units that will catch any smugglers taking the fastest route from the entrance to the exit checkpoint.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views28 pages

NCPC 2021 Problems

The document describes a problem about placing customs units at checkpoints along the Norway-Sweden border to catch smugglers. It provides details on the number of checkpoints, roads between checkpoints, time to pass each checkpoint, and number of available Norwegian and Swedish customs units. The goal is to determine a placement of units that will catch any smugglers taking the fastest route from the entrance to the exit checkpoint.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Nordic Collegiate Programming Contest

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

Do not open before the contest has started.


Advice, hints, and general information
• The problems are not sorted by difficulty.

• 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.

Sample Input 1 Sample Output 1


5 1 0 4 5 3 1
2 7 1 5 4

NCPC 2021 Problem A: Antenna Analysis 1


This page is intentionally left blank.
Problem B
Breaking Bars
Time limit: 2 seconds
Selma is visited by her two grandchildren Elsa and Asle who love chocolate. To be precise,
they are especially fond of the brand Nut Cream Puffed Chocolate that comes in bars made
up by 6 × 6 squares. The bars can be broken along the valleys between squares into smaller
rectangular bars of integer dimensions. Due to the fragile nature of this type of chocolate, the
bars often break into smaller rectangular bars even before you unpack them (but still only of
integer dimensions).
Thus Selma finds herself with a set of rectangular bars of various dimensions in her candy
stash. She knows how important it is to be fair to children, so not only does she want to give
Elsa and Asle the same amount of chocolate, but also identical collections of rectangular bars
(where an a × b bar is considered identical to a b × a bar). To do this, Selma can break her bars
into smaller pieces. A break is the operation of taking an a × b bar and breaking it along a valley
to produce two bars of dimensions c × b and (a − c) × b, for some integer c ∈ [1, a − 1], or two
bars of dimensions a × d and a × (b − d), for some integer d ∈ [1, b − 1]. See Figure B.1 for an
example.
Selma would like to give her two grandchildren identical collections of bars, each collection
consisting of at least t squares of chocolate. What is the minimum number of breaks she needs
to make to be able to do this?

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.

NCPC 2021 Problem B: Breaking Bars 3


Sample Input 1 Sample Output 1
4 15 2
1x2 2x2 3x3 3x5

Sample Input 2 Sample Output 2


6 7 0
1x2 2x3 1x4 3x2 4x1 6x6

Sample Input 3 Sample Output 3


5 3 1
1x1 1x1 1x1 1x1 1x4

NCPC 2021 Problem B: Breaking Bars 4


Problem C
Customs Controls
Time limit: 2 seconds
With lifted restrictions, the border trade between Norway and Sweden will surely be back to
its former glory. But the authorities are worried that this will also mean an increase of illegal
smuggling of goods. The customs authorities of Norway and Sweden must cooperate to prevent
this from becoming too big of a problem.
To pass through the customs, one must visit a series of checkpoints, the Nordic Customs
and Passport Control. There are n checkpoints in total, numbered from 1 to n, where 1 is
the entrance and n is the exit. There are m pairs of bidirectional roads that connect distinct
checkpoints. The ith checkpoint takes some amount of time ti to pass through, and this is the
bottleneck in crossing the border (the time it takes to walk the roads is negligible).
Each checkpoint can be watched by one customs unit, either a Norwegian one or a Swedish
one. There are k Norwegian customs units available, and n − k Swedish units. When a road has
both of its endpoints watched by customs units from the same country, any smugglers using that
road will be caught. Smugglers are of course always in a hurry, and will always attempt to go
from 1 to n in as short amount of time as possible.
Your task is to decide where to put the n customs units, so that any smugglers who take a
fastest possible route from 1 to n will be caught.

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”.

Sample Input 1 Sample Output 1


3 2 0 SSS
1 1 1
1 2
2 3

NCPC 2021 Problem C: Customs Controls 5


Sample Input 2 Sample Output 2
2 1 1 impossible
1 1
1 2

Sample Input 3 Sample Output 3


8 9 4 SNSNSSNN
3 3 1 2 2 3 2 1
1 2
1 3
1 4
2 5
3 6
4 7
5 8
6 8
7 8

NCPC 2021 Problem C: Customs Controls 6


Problem D
Deceptive Directions
Time limit: 2 seconds
You find yourself on a remote island, searching for a legendary lost treasure. However, despite
having gotten your hands on directions leading straight to the treasure, you have a problem. It
turns out you have a saboteur in your expedition, and that at some point they edited the precious
directions so they no longer lead to the treasure.
The island can be viewed as a rectangular grid, and the instructions are a sequence of
east/west/north/south steps to take in this grid, from a given starting position. These instructions
lead straight to the treasure (but may involve walking around obstacles) in the sense that there is
no shorter way of reaching the treasure. However, the saboteur has arbitrarily replaced each step
of the instructions by a step in one of the other three directions. In other words, any “west” step
has been replaced by “east”, “north” or “south”. This replacement has been done independently
for each step, so one “west” may have been replaced by “north” and another by “south”, and so
on.
Because of this sabotage, the instructions seem pretty useless. But maybe they can still be
used to narrow down the search. Write a program to find all possible locations of the treasure.

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 (‘!’).

Sample Input 1 Sample Output 1


5 5 #####
##### #...#
#...# #!S!#
#.S.# #.!.#
#...# #####
#####
N

NCPC 2021 Problem D: Deceptive Directions 7


Sample Input 2 Sample Output 2
7 5 #######
####### #!.#..#
#..#..# #..S..#
#..S..# #..#..#
#..#..# #######
#######
ESS

NCPC 2021 Problem D: Deceptive Directions 8


Problem E
Eavesdropper Evasion
Time limit: 4 seconds
Alice wants to send n messages to Bob over a communication channel. The ith message takes
ti time steps to send. At each integer time step, Alice can start sending any number of her
messages. Once started, a message must be transmitted in its entirety (it cannot be paused
and resumed later). Any number of messages can be sent in parallel over the channel without
affecting the transmission time of individual messages.
An attacker has the capability to disable the security protocols of the channel for an interval
of x continuous time steps, but only once (i.e., after doing this, they cannot wait a while and then
disable it for another x time steps). While the security is disabled, the attacker is able to listen
in, and any message that is sent in its entirety during those x time steps is considered exposed.
What is the minimum time needed for Alice to send all n messages to Bob so that at most
two messages are exposed, no matter when the attacker chooses to disable the security?

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.

Sample Input 1 Sample Output 1


6 10 16
2 3 4 5 6 7

Sample Input 2 Sample Output 2


7 6 11
9 3 2 3 8 3 3

NCPC 2021 Problem E: Eavesdropper Evasion 9


This page is intentionally left blank.
Problem F
Fortune From Folly
Time limit: 1 second
Your friend Ómar’s favourite video game is Striker-Count. But he has now grown tired of
actually playing the game and is more interested in the lootboxes found in the game. Inside
each lootbox there is an item of some level of rarity. Ómar is only interested in acquiring the
rarest items in the game. When he starts the game, he chooses two numbers n and k, such that
k ≤ n. He then opens lootboxes in the game until k of the last n lootboxes included an item of
the highest rarity.
This activity amuses Ómar, but does not interest you in the slightest. You are more interested
in the numbers: you know that each lootbox Ómar opens has probability p of containing an
item of highest rarity, independently for each lootbox. You want to find the expected number of
lootboxes Ómar will open before concluding his process.

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 .

Sample Input 1 Sample Output 1


3 2 0.0026 74445.39143490087

Sample Input 2 Sample Output 2


6 1 0.0026 384.61538461538464

NCPC 2021 Problem F: Fortune From Folly 11


This page is intentionally left blank.
Problem G
Grazed Grains
Time limit: 3 seconds
This year, there have been unusually many UFO sightings reported. Nobody knows if they are
caused by optical illusions, weather phenomena, or secret technology being tested by foreign
powers (terrestrial or not). UFO enthusiasts across the world rejoice, and speculations run wild.
But someone who is not impressed is the farmer Celeste. Her large grain field has repeatedly
been used as a landing site by the UFOs, destroying her crops. Celeste would like to bring
whoever is responsible to justice, but before she can do so she must assess the damage caused
by the unidentified flying offenders.
In total n circular UFOs have landed in Celeste’s field. The ith one left a crop circle which
destroyed all crops within radius ri of the point xi , yi . The field is very large, so you can assume
that it extends infinitely in all directions. Your task is to estimate the total area of crops destroyed
by the UFOs. Celeste only needs a rough estimate of the true answer, and your answer will be
counted as correct if its relative error is less than 10%.

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.

Sample Input 1 Sample Output 1


1 3.1415926535
0 0 1

Sample Input 2 Sample Output 2


4 25.991148
0 0 2
1 0 1
2 2 2
10 10 1

NCPC 2021 Problem G: Grazed Grains 13


This page is intentionally left blank.
Problem H
Hiring Help
Time limit: 4 seconds
A certain large unnamed software development company has n developers. The productivity
of each coder working for the company has been rigorously measured in terms of two key
performance indicators: the number of lines of code they write per hour, and the number of bugs
they fix per hour.
When a project needs to be done, the manager in charge of the project is allocated some
budget of t man-hours of programmer time. The manager can then staff different coders on the
project, up to a total of t hours. For instance if there are three programmers, the manager can
allocate any non-negative real numbers t1 , t2 , and t3 hours of their respective work hours, as
long as t1 + t2 + t3 ≤ t. If the three programmers write l1 , l2 , and l3 lines of code per hour, a
total amount of t1 · l1 + t2 · l2 + t3 · l3 lines of code will then be written for the project. Similarly
if they fix b1 , b2 , and b3 bugs per hour, a total of t1 · b1 + t2 · b2 + t3 · b3 bugs will be fixed.
Due to the uncertain economy, the company has a hiring freeze, meaning that no new coders
are hired to the company. However, under certain conditions, a manager is allowed to bring
in outside help by outsourcing a project to an external consultant rather than doing it in-house.
But this is only allowed if it is not possible to do the project equally efficiently in-house. In
particular, if the consultant writes ` lines of code and fixes b bugs in t hours, and there exists
some allocation of our existing coders which would write at least ` lines of code and fix at least
b bugs in at most t hours, then a manager is not allowed to hire this consultant (regardless of
whether those existing coders would actually have time to work on the project or whether they
are already too busy with other projects).
While no new coders are hired, employees do sometimes decide to leave the company. Given
a chronological list of events – requests to use a consultant, and employees quitting – find out
which of the requests will be approved.

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.

NCPC 2021 Problem H: Hiring Help 15


Sample Input 1 Sample Output 1
4 no
200 100 no
100 200 yes
100 100 no
200 200
5
c 10 2000 2000
c 5 750 750
q 4
c 3 600 600
c 10 1500 1500

Sample Input 2 Sample Output 2


8 no
400 300 no
300 200 no
300 400 no
200 300 yes
500 500 no
100 500 no
100 100 no
500 100
12
c 4 1611 1601
c 3 602 601
c 2 399 795
c 1 395 206
q 7
q 6
q 5
q 4
c 4 1611 1601
c 3 602 601
c 2 399 795
c 1 395 206

NCPC 2021 Problem H: Hiring Help 16


Problem I
Intact Intervals
Time limit: 5 seconds
Gustav is an astronaut on the Nordic Celestial Planetary Craft (NCPC), a large space station in
orbit around Mars. Today, one of Gustav’s tasks is to look over the satefy routines on board.
The space station consists of n modules arranged in a circle, so that module i is connected
to module i + 1 for i = 1 . . . n − 1, and module n is connected to module 1. Each module
i has a non-negative integer type ai , representing the kind of equipment that can be found
there. Different modules can have the same type. In case of emergency, the equipment must be
rearranged so that each module i instead gets type bi , for some list b1 , b2 , · · · , bn . Here, the list b
is a rearrangement of the list a.
Gustav has noticed that if some module connections are severed, causing the space station
to split into separate parts, it may become impossible to perform this rearrangement of the
equipment. He decides to estimate how likely it is that the safety routines can be followed, by
calculating in how many ways the space station can be separated into two or more parts such
that it is still possible to rearrange the equipment according to the emergency procedures.
In other words, your task is to count in how many ways the circular list a can be partitioned
into at least two non-empty contiguous intervals, in such a way that the circular list b can be
obtained by rearranging elements within each interval. Since this number can be quite big, you
should find its remainder modulo 109 + 7.
For example, consider Sample Input 1 below. Here the list a could be split into [1|223|4],
indicating that the connection between modules 1 and 2, and the connection between modules 4
and 5, are severed. Note that the connection between module 5 and 1 remains in this split. The
second possible way in which a could be split is [12|2|34].
In Sample Input 2 below, the only possible way to split the list a into at least two non-empty
parts is to separate the two modules. But then it is impossible to rearrange the parts to create the
list b. Hence, the answer is 0.

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.

Sample Input 1 Sample Output 1


5 2
1 2 2 3 4
4 3 2 2 1

NCPC 2021 Problem I: Intact Intervals 17


Sample Input 2 Sample Output 2
2 0
1 2
2 1

NCPC 2021 Problem I: Intact Intervals 18


Problem J
Joint Jog Jam
Time limit: 1 second
Like so many good stories, this one begins with a claim that Kari is a faster runner than Ola,
who of course challenges Kari to a run-down. Dubbed (rather ironically) Non-Competitive Pace
Challenge, they want to see who can run the furthest in a certain amount of time t. Clearly they
both choose to run in straight lines with constant speed.
Kari wrote an app to make sure that Ola does not cheat, but the app requires that their phones
constantly communicate over Bluetooth.
After their run, Kari needs to ensure that they were never too far apart from each other at
any time during the run. Write a program that computes the maximum distance between Kari
and Ola at any point during the run.

Input
The input consists of a single line containing eight integers describing four points:

• the starting position of Kari,

• the starting position of Ola,

• the ending position of Kari, and

• the ending position of Ola,

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

Sample Input 2 Sample Output 2


0 0 0 1 0 2 2 1 2.2360679775

Sample Input 3 Sample Output 3


5 0 10 0 5 0 10 0 5

NCPC 2021 Problem J: Joint Jog Jam 19


This page is intentionally left blank.
Problem K
Knot Knowledge
Time limit: 1 second
Sonja the scout is taking a test to see if she knows all the knots a scout is supposed to know. The
Scout’s Big Book of Knots has descriptions of 1 000 different knots, conveniently numbered
from 1 to 1 000. For the test, Sonja needs to learn a specific set of n of these knots. After some
intense studying, she has learned all except one of them, but she has forgotten which knot she
does not yet know.
Given the list of knots Sonja needs to learn, and the ones she has learned so far, find the
remaining knot to learn.

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.

Sample Input 1 Sample Output 1


4 1
1 2 4 3
4 2 3

Sample Input 2 Sample Output 2


4 10
10 101 999 1
1 999 101

NCPC 2021 Problem K: Knot Knowledge 21


This page is intentionally left blank.
Problem L
Locust Locus
Time limit: 1 second
There are two different species of periodical cicadas that only appear from hibernation every 13
and 17 years, respectively. Your old grandpa tells you that he saw them simultaneously back in
’92. You start pondering how many years you have to wait until you see them again. You collect
information about other pairs of periodical cicadas and when they were last observed to find out
when the next simultaneous appearance is.
Given several different pairs of cicadas and their last simultaneous appearance, find the next
year that one of the pairs reappears.

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.

Sample Input 1 Sample Output 1


3 2036
1992 13 17
1992 14 18
2001 5 7

Sample Input 2 Sample Output 2


2 2026
2020 2 3
2019 3 4

NCPC 2021 Problem L: Locust Locus 23


This page is intentionally left blank.
Problem M
Marvelous Marathon
Time limit: 5 seconds
A marathon race is being planned in the beautiful countryside. The course will be somewhere
along a long, bidirectional, road. The organizers want to determine exactly where along this
road the race should be in order to maximize the experience for the runners, so that they get
to enjoy as much beautiful scenery as possible and are distracted from their tired limbs. The
scenery varies in beauty depending on where on the road you are, and also in which direction
you are running. Because of this, the organizers are fine with having the runners make up to two
U-turns, as long as no part of the road is used more than once in each direction.
We model the road of length m meters as a rectangular grid of size 2 × m, where each cell
has a non-negative “beauty” value associated with it. The columns represent each meter of the
road ordered from start to to end. The top row in a column represents the beauty for this part
of the road when running in the direction towards the end of the road, and the bottom row in a
column represents the beauty when running towards the start of the road. A race of length x is
then some set of exactly x of the cells in the grid. Those x cells must form a path in the grid
where no cell is visited more than once, we only move to the right or down from cells in the top
row, and we only move to the left or up from cells in the bottom row. See Figure M.1 for an
example race. The “total beauty” of a race is the sum of the beauty values of the included cells.
The road is long, so rather than providing a list of all of the 2m beauty values, each side of
the road is divided into a small number of segments, where the cells within a segment have some
constant beauty value (and cells with beauty 0 are simply omitted).

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.

NCPC 2021 Problem M: Marvelous Marathon 25


Sample Input 1 Sample Output 1
19 14 6 89
14 5 7
11 15 6
3 7 4
16 15 5
19 17 8
0 3 9

Sample Input 2 Sample Output 2


100000 42195 2 35548500000000
30000 60000 500000000
40000 10000 1000000000

NCPC 2021 Problem M: Marvelous Marathon 26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy