2021 Canadian Computing Competition
Senior Problems
Problem S1: Crazy Fencing
Problem Description
You need to paint a wooden fence between your house and your neighbour’s
house. You want to determine the area of the fence, in order to
determine how much paint you will use.
However, the fence is made out of non-uniform pieces of wood, and your
neighbour believes that they have an artistic flair. In particular, the
pieces of wood may be of various widths. The bottom of each piece of
wood will be horizontal, both sides will be vertical, but its top may be
cut on an angle. Two such pieces of wood are shown below:
Thankfully, the fence has been constructed so that adjacent pieces of
wood have the same height on the sides where they touch, which makes the
fence more visually appealing.
Input Specification
The first line of the input will be a positive integer , where .
The second line of input will contain space-separated integers describing the left and
right heights of each piece of wood. Specifically, the left height of
the th piece of wood is
and the right height of the
th piece of wood is
.
The third line of input will contain space-separated integers describing the width of the th piece of wood.
Output Specification
Output the total area of the fence.
Sample Input 1
3
2 3 6 2
4 1 1
Output for Sample Input 1
18.5
Explanation of Output for Sample Input 1
The fence looks like the following:
When looking from left to right, the individual areas of the pieces
of wood are ,
, and , for a total area of
18.5.
Sample Input 2
4
6 4 9 7 3
5 2 4 1
Output for Sample Input 2
75
Explanation of Output for Sample Input 2
The fence looks like the following:
When looking from left to right, the individual areas of the pieces
of wood are 25, 13, 32, and 5, for a total area of 75.
Problem J5/S2: Modern Art
Problem Description
A new and upcoming artist has a unique way to create checkered patterns.
The idea is to use an -by- canvas which is initially entirely
black. Then the artist repeatedly chooses a row or column and runs their
magic brush along the row or column. The brush changes the colour of
each cell in the row or column from black to gold or gold to black.
Given the artist’s choices, your job is to determine how much gold
appears in the pattern determined by these choices.
Input Specification
The first line of input will be a positive integer . The second line of input will be a
positive integer . The third line
of input will be a positive integer . The remaining input will be lines giving the choices made by the
artist. Each of these lines will either be R
followed by a single space and then an integer which is a row number, or
C
followed by a single space and then an
integer which is a column number. Rows are numbered top down from to . Columns are numbered left to right
from to .
The following table shows how the available 15 marks are
distributed.
1 mark |
|
|
|
only one cell, and
up to 100 choices by the artist |
4 marks |
|
|
|
only one row, and
up to 100 choices by the artist |
5 marks |
|
|
|
up to 100 rows, up to 100 columns, and
up to 100 choices by the artist |
5 marks |
|
|
up to 5000000 cells, and
up to 1000000 choices by the artist |
Output Specification
Output one non-negative integer which is equal to the number of cells
that are gold in the pattern determined by the artist’s choices.
Sample Input 1
3
3
2
R 1
C 1
Output for Sample Input 1
4
Explanation of Output for Sample Input 1
After running the brush along the first row, the canvas looks like
this:
GGG
BBB
BBB
Then after running the brush along the first column, four cells are
gold in the final pattern determined by the artist’s choices:
BGG
GBB
GBB
Sample Input 2
4
5
7
R 3
C 1
C 2
R 2
R 2
C 1
R 4
Output for Sample Input 2
10
Explanation of Output for Sample Input 2
Ten cells are gold in the final pattern determined by the artist’s choices:
BGBBB
BGBBB
GBGGG
GBGGG
Problem S3: Lunch Concert
Problem Description
It’s lunchtime at your school! Your friends are all standing on a long
field, as they usually do. The field can be represented as a number
line, with the th friend initially
at position metres along it.
The th friend is able to walk in
either direction along the field at a rate of one metre per seconds, and their hearing is good
enough to be able to hear music up to and including metres away from their position.
Multiple students may occupy the same positions on the field, both
initially and after walking.
You’re going to hold a little concert at some position metres along the field (where is any integer of your choice), and
text all of your friends about it. Once you do, each of them will walk
along the field for the minimum amount of time such that they end up
being able to hear your concert (in other words, such that each friend
ends up within units of ).
You’d like to choose such that
you minimize the sum of all of
your friends’ walking times. What is this minimum sum (in seconds)?
Please note that the result might not fit within a 32-bit integer.
Input Specification
The first line of input contains .
The next lines contain three
space-separated integers, ,
, and .
The following table shows how the available 15 marks are
distributed.
Output Specification
Output one integer which is the minimum possible sum of walking times
(in seconds) for all of your
friends to be able to hear your concert.
Sample Input 1
1
0 1000 0
Output for Sample Input 1
0
Explanation of Output for Sample Input 1
If you choose , your single
friend won’t need to walk at all to hear it.
Sample Input 2
2
10 4 3
20 4 2
Output for Sample Input 2
20
Explanation of Output for Sample Input 2
One possible optimal choice of is
, which would require your first
friend to walk to position
(taking seconds) and
your second friend to walk to position (taking seconds), for a total of seconds.
Sample Input 3
3
6 8 3
1 4 1
14 5 2
Output for Sample Input 3
43
Problem S4: Daily Commute
Problem Description
Toronto has subway stations,
numbered from to . You start at station , and every day, you need to reach
station to get to school.
There are one-way
walkways running amongst the stations, the th of which allows you to
walk from station
to a different station
in 1 minute. There may be
multiple walkways connecting any given pair of stations.
The subway line follows a certain route through the stations, starting at station and visiting each station once.
Initially, this route consists of stations , in that order. , and is a permutation of
the integers . Only one
subway train runs along this route per day, departing from station 1 at
6am in the morning and taking 1 minute to reach each subsequent station.
This means that, minutes after
6am, the train will be at station (or at station if ).
Over a period of days,
however, the subway line’s route will keep changing. At the start of the
th day, the th station and th station in the route will be
swapped. Note that, after each such change, the route will still begin
at station and will visit all
stations once each. Changes will
carry over to subsequent days – the route will not automatically reset
itself back to .
On each of these days, you’d
like to determine how quickly you can get to school so you can begin
learning things. On the th day, starting at 6am in
the morning (after the th update to the subway
line’s route), you’ll begin your daily trip to station . Each minute, you may either ride the
subway to its next stop (if you’re currently at the same station as the
train and it hasn’t already completed its route), take a walkway from
your current station to another one, or wait at your current station.
Note that your trip begins at the same time as the train’s route,
meaning that you may choose to immediately ride it if you’d like to, and
that you may choose to leave and then get back on the train during your
trip.
Input Specification
The first line contains three space-separated integers, , , and .
The next lines each contain two
space-separated integers, and
.
The next line contains the
space-separated integers, , which form the initial permutation of stations.
The next lines each contain two
space-separated integers, and
.
The following table shows how the available 15 marks are
distributed.
2 marks |
|
|
|
2 marks |
|
|
|
3 marks |
|
|
|
8 marks |
|
|
|
Output Specification
The output is lines, with one integer per line. The th line is the minimum number
of minutes required to reach station on the th day ().
Sample Input
4 3 3
1 2
3 4
4 1
1 4 3 2
3 4
4 2
3 2
Output for Sample Input
1
2
3
Explanation of Output for Sample Input
At the start of the first day, the subway line’s route will be updated
to visit stations , in
that order. On that day, you should simply take the subway to station
, taking 1 minute.
On the second day, the route will become , and you should take the
subway to station (taking 1
minute) and then walk to station
(taking 1 more minute).
On the third day, the route will become . One choice of optimal trip
involves walking to station 2 (taking 1 minute), then boarding the train
there and taking it through station 3 and finally to station 4 (taking
another 2 minutes).
Problem S5: Math Homework
Problem Description
Your math teacher has given you an assignment involving coming up with a
sequence of integers , such that for each
.
The sequence must also satisfy
requirements, with the th one stating that the GCD
(Greatest Common Divisor) of the contiguous subsequence must be equal to
. Note that the GCD of a
sequence of integers is the largest integer such that all the numbers in the
sequence are divisible by .
Find any valid sequence consistent with all of these
requirements, or determine that no such sequence exists.
Input Specification
The first line contains two space-separated integers, and .
The next lines each contain three
space-separated integers, ,
, and .
The following table shows how the available 15 marks are
distributed.
3 marks |
|
|
for each |
4 marks |
|
|
for each |
8 marks |
|
|
for each |
Output Specification
If no such sequence exists, output the string
Impossible
on one line. Otherwise, on one
line, output space-separated
integers, forming the sequence . If there are multiple
possible valid sequences, any valid sequence will be
accepted.
Sample Input 1
2 2
1 2 2
2 2 6
Output for Sample Input 1
4 6
Explanation of Output for Sample Input 1
If and , the GCD of is and the GCD of is , as required. Please note that
other outputs would also be accepted.
Sample Input 2
2 2
1 2 2
2 2 5
Output for Sample Input 2
Impossible
Explanation of Output for Sample Input
2
There exists no sequence
such that the GCD of is
and the GCD of is .