University of Waterloo Logo and CEMC Banner

2023 Canadian Computing Competition
Senior Problems

Problem J4/S1: Trianglane

Problem Description

Bocchi the Builder just finished constructing her latest project: a laneway consisting of two rows of white equilateral triangular tiles. However, at the last moment, disaster struck! She accidentally spilled black paint on some of the tiles. Now, some of the tiles are wet and the other tiles are dry. Bocchi must place warning tape around the perimeters of all wet areas. Can you help her determine how many metres of tape she needs?

The first triangular tile will point upwards. Each pair of adjacent tiles (that is, tiles that share a common side) will point in opposite directions. Each tile has a side length of 1 metre.

Input Specification

The first line of input will consist of one positive integer \(C\), representing the number of columns.

The next two lines will each consist of \(C\) integers separated by spaces. Each integer represents the colour of a tile along the laneway, with 1 indicating that the tile is black (wet) and 0 indicating that the tile is white (dry).

The following table shows how the available 15 marks are distributed:

Marks Description Bound
3 The laneway is not very long, black tiles are never adjacent and the second row is fully white. \(C \le 2\,000\)
3 The laneway is not very long, black tiles may be adjacent and the second row is fully white. \(C \le 2\,000\)
5 The laneway is not very long, black tiles may be adjacent and may appear in the second row. \(C \le 2\,000\)
4 The laneway may be very long, black tiles may be adjacent and may appear in the second row. \(C \le 200\,000\)

Output Specification

Output a single integer representing the length of tape Bocchi needs, in metres.

Sample Input 1

5
1 0 1 0 1
0 0 0 0 0 

Output for Sample Input 1

9

Explanation of Output for Sample Input 1

The tiles are painted as follows, creating three wet areas. Bocchi will need 9 metres of warning tape as shown in yellow.

Two rows of five equilateral triangle tiles. In the top row, the tiles are pointing up, down, up, down, up. In the bottom row, the tiles are pointing down, up, down, up, down. Each tile pointing upward in the top row shares a side with a tile pointing downward in the bottom row. Each tile pointing upward in the top row is outlined in yellow.

Sample Input 2

7
0 0 1 1 0 1 0
0 0 1 0 1 0 0

Output for Sample Input 2

11

Explanation of Output for Sample Input 2

The tiles are painted as follows, creating three wet areas. Bocchi will need 5 metres of warning tape to surround one area and 3 metres of warning tape to surround each of the other two areas as shown in yellow.

Two rows of seven tiles arranged as in the previous input. The third and fourth tiles in the top row (pointing up then down) and the third tile in the bottom row (pointing down) form a trapezoidal area outlined in yellow. Also, the sixth tile in the top row and the fifth tile in the bottom row are each outlined in yellow.

Problem S2: Symmetric Mountains

Problem Description

Rebecca is a tour guide and is trying to market the Rocky Mountains for her magazine. She recently took a beautiful picture consisting of \(N\) mountains where the \(i\)-th mountain from the left has a height \(h_i\). She will crop this picture for her magazine, by possibly removing some mountains from the left side of the picture and possibly removing some mountains from the right side of the picture. That is, a crop consists of consecutive mountains starting from the \(l\)-th to the \(r\)-th mountain where \(l \le r\). To please her magazine readers, Rebecca will try to find the most symmetric crop.

We will measure the asymmetric value of a crop as the sum of the absolute difference for every pair of mountains equidistant from the midpoint of the crop. To help understand that definition, note that the absolute value of a number \(v\), written as \(|v|\), is the non-negative value of \(v\): for example \(|-6| = 6\) and \(|14|=14\). The asymmetric value of a crop is the sum of all \(|h_{l + i} - h_{r - i}|\) for \(0 \le i \le \frac{r - l }{2}\). To put that formula in a different way, we pair up the mountains working from the outside in toward the centre, calculate the absolute difference in height of each of these pairs, and sum them up.

Because Rebecca does not know how wide the picture needs to be, for all possible crop lengths, find the asymmetric value of the most symmetric crop (the crop with the minimum asymmetric value).

Input Specification

The first line consists of an integer \(N\), representing the number of mountains in the picture.
The second line consists of \(N\) space-separated integers, where the \(i\)-th integer from the left represents \(h_i\).

The following table shows how the available 15 marks are distributed:

Marks Bounds on \(N\) Bounds on \(h_i\) Additional Constraints
5 \(1 \le N \le 300\) \(0 \le h_i \le 10^5\) None
5 \(1 \le N \le 5000\) \(0 \le h_i \le 10^5\) Height of mountains are in non-decreasing order from left to right.
5 \(1 \le N \le 5000\) \(0 \le h_i \le 10^5\) None

Output Specification

Output on one line \(N\) space-separated integers, where the \(i\)-th integer from the left is the asymmetric value of the most symmetric picture of crops of length \(i\).

Sample Input 1

7
3 1 4 1 5 9 2

Output for Sample Input 1

0 2 0 5 2 10 10

Explanation of Output for Sample Input 1

We will show why the fifth value from the left is 2. Let us try to compute all the asymmetric values of crops with length 5.

The height of the mountains in the first crop is \([3, 1, 4, 1, 5]\). The asymmetric value of this crop is \(|3 - 5| + |1 - 1| + |4 - 4| = 2\).

The height of the mountains in the second crop is \([1, 4, 1, 5, 9]\). The asymmetric value of this crop is \(|1 - 9| + |4 - 5| + |1 - 1| = 9\).

The height of the mountains in the last crop is \([4, 1, 5, 9, 2]\). The asymmetric value of this crop is \(|4 - 2| + |1 - 9| + |5 - 5| = 10\).

Hence, the most symmetric crop of length 5 is 2.

Sample Input 2

4
1 3 5 6

Output for Sample Input 2

0 1 3 7

Explanation of Output for Sample Input 2

This sample satisfies the second subtask. Note that the only crop of length 4 is \([1,3,5,6]\) which has asymmetric value of \(|1 - 6| + |3 - 5| = 7\).

Problem S3: Palindromic Poster

Problem Description

Ryo and Kita are designing a new poster for Kessoku Band. After some furious brainstorming, they came to the conclusion that the poster should come in the form of a 2-D grid of lowercase English letters (i.e. a to z), with \(N\) rows and \(M\) columns.

Furthermore, it is known that Ryo and Kita both have peculiar tastes in palindromes. Ryo will only be satisfied with the poster if exactly \(R\) of its rows are palindromes, and Kita will only be satisfied with the poster if exactly \(C\) of its columns are palindromes. Can you design a poster that will satisfy both Ryo and Kita, or determine that it is impossible to do so?

Note: A string is considered a palindrome if it is the same when read forwards and backwards. For example, kayak and bb are palindromes, whereas guitar and live are not.

Input Specification

The first and only line of input consists of \(4\) space-separated integers \(N\), \(M\), \(R\), and \(C\).

The following table shows how the available 15 marks are distributed:

Marks Bounds on \(N\) Bounds on \(M\) Bounds on \(R\) Bounds on \(C\)
2 marks \(2 \le N \le 2\,000\) \(2 \le M \le 2\,000\) \(R = 1\) \(C = 1\)
2 marks \(N = 2\) \(M = 2\) \(0 \le R \le N\) \(0 \le C \le M\)
4 marks \(N = 2\) \(2 \le M \le 2\,000\) \(0 \le R \le N\) \(0 \le C \le M\)
7 marks \(2 \le N \le 2\,000\) \(2 \le M \le 2\,000\) \(0 \le R \le N\) \(0 \le C \le M\)

Output Specification

If it is impossible to design a poster that will satisfy both Ryo and Kita, output IMPOSSIBLE on a single line.

Otherwise, your output should contain \(N\) lines, each consisting of \(M\) lowercase English letters, representing your poster design. If there are multiple possible designs, output any of them.

Sample Input 1

4 5 1 2

Output for Sample Input 1

union
radar
badge
anime

Explanation of Output for Sample Input 1

In the given design, only the second row (namely radar) and the second and third columns (namely naan and iddi) are palindromes. Since exactly \(R = 1\) of the rows and \(C = 2\) of the columns are palindromes, this is an acceptable design.

Sample Input 2

2 2 2 1

Output for Sample Input 2

IMPOSSIBLE

Explanation of Output for Sample Input 2

In this case, it can be proven that it is impossible to satisfy both Ryo and Kita.

Problem S4: Minimum Cost Roads

Problem Description

As the newly elected mayor of Kitchener, Alanna’s first job is to improve the city’s road plan.

Kitchener’s current road plan can be represented as a collection of \(N\) intersections with \(M\) roads, where the \(i\)-th road has length \(l_i\) meters, costs \(c_i\) dollars per year to maintain, and connects intersections \(u_i\) and \(v_i\). To create a plan, Alanna must select some subset of the \(M\) roads to keep and maintain, and that plan’s cost is the sum of maintenance costs of all roads in that subset.

To lower the city’s annual spending, Alanna would like to minimize the plan’s cost. However, the city also requires that she minimizes travel distances between intersections and will reject any plan that does not conform to those rules. Formally, for any pair of intersections \((i, j)\), if there exists a path from \(i\) to \(j\) taking \(l\) meters on the existing road plan, Alanna’s plan must also include a path between those intersections that is at most \(l\) meters.

Input Specification

The first line contains the integers \(N\) and \(M\).

Each of the next \(M\) lines contains the integers \(u_i\), \(v_i\), \(l_i\), and \(c_i\), meaning that there currently exists a road from intersection \(u_i\) to intersection \(v_i\) with length \(l_i\) and cost \(c_i\) \((1 \le u_i, v_i \le N, u_i \neq v_i)\).

The following table shows how the available 15 marks are distributed.

Marks Bounds on
\(N\) and \(M\)
Bounds on \(l_i\) Bounds on \(c_i\) Additional Constraints
\(3\) marks \(1 \le N, M \le 2\,000\) \(l_i = 0\) \(1 \le c_i \le 10^9\) None
\(6\) marks \(1 \le N, M \le 2\,000\) \(1 \le l_i \le 10^9\) \(1 \le c_i \le 10^9\) There is at most one road between any unordered pair of intersections.
\(6\) marks \(1 \le N, M \le 2\,000\) \(0 \le l_i \le 10^9\) \(1 \le c_i \le 10^9\) None

Output Specification

Output one integer, the minimum possible cost of a road plan that meets the requirements.

Sample Input

5 7
1 2 15 1
2 4 9 9
5 2 5 6
4 5 4 4
4 3 3 7
1 3 2 7
1 4 2 1

Output for Sample Input

25

Explanation of Output for Sample Input

Here is a diagram of the intersections along with a valid road plan with minimum cost.

A description of the diagram follows.

Each edge is labeled with a pair \((l, c)\) denoting that it has length \(l\) meters and cost \(c\) dollars. Additionally, the roads that are part of the plan are highlighted in blue, with a total cost of \(7+1+6+7+4=25\).

It can be shown that we cannot create a cheaper plan that also respects the city’s requirements.

Problem S5: The Filter

Problem Description

Alice, the mathematician, likes to study real numbers that are between 0 and 1. Her favourite tool is the filter.

A filter covers part of the number line. When a number reaches a filter, two events can happen. If a number is not covered by the filter, the number will pass through. If a number is covered, the number will be removed.

Alice has infinitely many filters. Her first 3 filters look like this:

A description of the diagram follows.

In general, the \(k\)-th filter can be defined as follows:

Alice has instructions for constructing the Cantor set. Start with the number line from 0 to 1. Apply all filters on the number line, and remove the numbers that are covered. The remaining numbers form the Cantor set.

Alice wants to research the Cantor set, and she came to you for help. Given an integer \(N\), Alice would like to know which fractions \(\frac{x}{N}\) are in the Cantor set.

Input Specification

The first line contains the integer \(N\).

The following table shows how the available 15 marks are distributed.

Marks Bounds on \(N\) Additional Constraints
3 marks \(3 \le N \le 3^{18}\) \(N\) is a power of \(3\)
4 marks \(2 \le N \le 10^5\) None
8 marks \(2 \le N \le 10^9\) None

Output Specification

Output all integers \(x\) where \(0 \le x \le N\) and \(\frac{x}{N}\) is in the Cantor set.

Output the answers in increasing order. The number of answers will not exceed \(10^6\).

Sample Input

12

Output for Sample Input

0
1
3
4
8
9
11
12

Explanation of Output for Sample Input

Here is a diagram of the fractions and the first 4 filters. In reality, there are infinitely many filters.

A description of the diagram follows.

\(\dfrac{5}{12}\), \(\dfrac{6}{12}\), and \(\dfrac{7}{12}\) are not in the Cantor set because they were covered by the \(1^\text{st}\) filter.

Furthermore, \(\dfrac{2}{12}\) and \(\dfrac{10}{12}\) are not in the Cantor set because they were covered by the \(2^\text{nd}\) filter.

It can be shown that the remaining fractions will pass through all filters.