2021 Canadian Computing Olympiad
Day 1,
Problem 1: Swap Swap Sort
Time Limit: 3 seconds
Problem Description
There is an array of integers,
each with a value between and
. Your friend has a an algorithm
that can sort this array according to any ordering of the numbers from
to . The algorithm performs a sequence of
swap operations, that exchange two
adjacent elements of the array. The algorithm performs
exactly the minimum number of such swaps to sort the array.
The desired ordering of the numbers from to is given by a target
permutation. A target permutation is a sequence of each number
from to appearing exactly once, in the same
order that is desired by the corresponding ordering.
For example, the array [1 4 2 1 2]
sorted
by the target permutation
results in the array [4 1 1 2 2]
.
You are interested in the number of swaps performed by your friend’s
algorithm for different target permutations. To explore this, you start
with a target permutation of and perform operations on it. Each operation swaps
two adjacent elements of the target permutation. After
performing each operation, find the number of swaps your friend’s
algorithm would make if it was run with the current target permutation.
The operations cumulatively
change the target permutation, but do not affect the array.
The first line contains the three integers , , and .
The next line contains
integers
specifying the
array .
The next lines each contains a
single integer , representing the
operation of swapping the elements of the target permutation at indices
and .
For 3 of the 25 available marks, .
For an additional 3 of the 25 available marks, .
For an additional 5 of the 25 available marks, .
Output
Specification
For each of the operations,
output a line containing a single integer; the answer for the current
target permutation.
5 4 3
1 4 2 1 2
3
2
1
4
2
2
The three target permutations are , then , then . For the final target
permutation, your friend’s algorithm uses two swaps to sort the array
[1 4 2 1 2]
to
[4 1 1 2 2]
.
Day 1, Problem 2: Weird Numeral System
Time Limit: 1.5 seconds
Problem Description
Alice enjoys thinking about base- numeral systems (don’t we all?). As you
might know, in the standard base-
numeral system, an integer can be
represented as where:
For example, in standard base-,
you would write as
1 2 0
, since .
But standard base- systems are
too easy for Alice. Instead, she’s thinking about
weird-base- systems.
A weird-base- system is just
like the standard base- system,
except that instead of using the digits , you use for some value . For example, in a weird-base- system with , you could write as
1 -1 -1 0
, since .
Alice is wondering how to write integers, through , in a weird-base- system that uses the digits through . Please help her out!
The first line contains four space-separated integers, , , , and .
The second line contains
distinct integers, through
.
Finally, the -th of the next
lines contains .
For 8 of the 25 available marks, , .
Output
Specification
Output lines, the -th of which is a weird-base- representation of . If multiple representations are
possible, any will be accepted. The digits of the representation should
be separated by spaces. Note that
must be represented by a non-empty set of digits.
If there is no possible representation, output
IMPOSSIBLE
.
3 3 3 1
-1 0 1
15
8
-5
1 -1 -1 0
1 0 -1
-1 1 1
We have:
,
, and
.
10 1 3 2
0 2 -2
17
IMPOSSIBLE
Day 1, Problem 3: Through Another Maze Darkly
Time Limit: 8 seconds
Problem Description
There is a maze that is formed by connecting rooms via corridors. The rooms are numbered
to and each room has the shape of a
circle. The corridors have the following constraints:
One difficulty in navigating through this maze is that the lights are
all out, so you cannot see where you are. To help with navigation, each
room contains a laser pointer that initially points to a corridor.
Consider the following strategy:
Rotate the room’s laser pointer clockwise until it points to
another corridor.
Follow the room’s laser pointer and traverse the
corridor.
Repeat the previous two steps indefinitely.
You created queries to
investigate this strategy. For each query, you are given an integer
and you start at room 1.
Determine the final room after traversing exactly corridors. All laser pointers will
reset to their original orientation after each query.
The first line contains the integers and .
The next lines describe the
layout of the maze, with the of these lines describing
room . Specifically, it contains
an integer , the number of
corridors connecting to room ,
followed by integers, , indicating the rooms
that these corridors lead to, in
clockwise order. Lastly, room ’s
laser pointer initially points to the corridor leading to room .
The final lines describe a
query, with each line containing an integer .
For 4 of the 25 available marks, the corridor forms a connection
between room and room .
For an additional 4 of the 25 available marks, and .
For an additional 12 of the 25 available marks, .
Output
Specification
Output lines answering the
queries in order.
5 6
1 2
3 3 1 4
1 2
2 5 2
1 4
1
2
3
4
5
6
2
1
2
4
2
3
This is the initial state of the maze.
The strategy will visit these rooms in order:
Day 2, Problem 1: Travelling Merchant
Time Limit: 1 second
Problem
Description
A merchant would like to make a business of travelling between
cities, moving goods from one city to another in exchange for a profit.
There are cities and trading routes between them.
The -th trading route lets the
merchant travel from city to
city (in only that direction).
In order to take this route, the merchant must already have at least
units of money. After taking
this route, the total amount of money the merchant has will increase by
units, with a guarantee that
.
For each of the cities, we
would like to know the minimum amount of money required for a merchant
to start in that city and be able to keep travelling forever.
The first line contains the two integers and .
The -th of the next lines contains the four integers , , , and . Note that
there may be any number of routes between a pair of cities.
For 4 of the 25 available marks, .
For an additional 5 of the 25 available marks, for all .
Output
Specification
On a single line, output
space-separated integers, where the -th is the answer if the merchant were
to start at city . This answer is
either the minimum amount of money, or if no amount of money can be
sufficient.
5 5
3 1 4 0
2 1 3 0
1 3 1 1
3 2 3 1
4 2 0 2
2 3 3 1 -1
Starting from city 2 with 3 units of money, the merchant can cycle
between cities 2, 1, and 3.
Day 2, Problem 2: Bread First Search
Time Limit: 1 second
Problem Description
There are towns in a network
of undirected roads. Each road
connects one pair of towns. The government has decided to conduct a
breadth first search, which means finding an ordering of the towns such that if the ordering begins
with :
Each town except for is
adjacent to another town given earlier in the order.
The towns are given in non-decreasing order of distance to . Here, distance means the minimum
number of roads that need to be traversed to reach a town.
However, someone mistakenly did a bread first search. They
found the ordering
(this was obtained by sorting the towns in increasing order of bread
production).
To cover up this embarrassment, the government would like to build
new roads such that is
also a possible breadth first search ordering. The new roads can be
built between any two towns. What is the minimum possible number of
roads that need to be built?
The first line contains the two integers and .
The -th of the next lines contains the two integers and , representing the two endpoints of the -th road. It is guaranteed that and there is at most one
road between any two towns.
For 5 of the 25 available marks, .
For an additional 6 of the 25 marks available, .
Output Specification
On a single line, output the minimum number of roads that must be
constructed.
2 0
1
For to be a breadth first
search ordering, a road between towns and must be built.
6 3
1 3
2 6
4 5
2
By building a road between and
and a road between and , the sequence of distances becomes
which is in
non-decreasing order.
Day 2, Problem 3: Loop Town
Time Limit: 4 seconds
Problem Description
Some cities have complicated road networks that require advanced
graph theory to analyze. But not Loop Town! Loop Town has a single
circular road that loops around the town. It has residents that live in distinct houses located around the
road. The town also has offices,
and each resident works at a distinct office.
The road in Loop Town has length . The location of each building will be
represented by an integer between
and . Since the road is
circular, the positions and are adjacent. It is guaranteed that
the locations of all buildings
will be distinct.
Every morning, all residents
simultaneously exit their houses onto the road. They then need to walk
along the road to the entrance of the office where they work. When each
resident has reached the entrance of their office, they all enter
simultaneously.
However, a pandemic has now come to Loop Town, disrupting this usual
routine. To prevent the spread of disease, residents must now observe
social distancing while walking to work. Since the loop road is rather
narrow, this means that it is far more inconvenient for two people to
cross each other on their way to work (one person must temporarily step
off the path to let the other pass). What is the minimum total number of
crossings, assuming all the residents work together to achieve this?
The first line contains the two integers and .
The -th of the next lines contains the two integers and , where and represent the locations of the
-th resident’s house and office
respectively. It is guaranteed that all locations are distinct.
For 12 of the 25 available marks, .
For an additional 6 of the 25 available marks, .
Output
Specification
On a single line, output the minimum total number of crossings.
3 100
10 50
30 20
60 40
0
Since the road is circular, nobody needs to cross each other.
4 100
30 70
10 12
60 75
90 50
1