University of Waterloo Logo and CEMC Banner

2021 Canadian Computing Competition

Junior Problems

Problem J1: Boiling Water

Problem Description

At sea level, atmospheric pressure is 100 kPa and water begins to boil at 100°C. As you go above sea level, atmospheric pressure decreases, and water boils at lower temperatures. As you go below sea level, atmospheric pressure increases, and water boils at higher temperatures. A formula relating atmospheric pressure to the temperature at which water begins to boil is \[P = 5 \times B - 400\] where \(P\) is atmospheric pressure measured in kPa, and \(B\) is the temperature at which water begins to boil measured in °C.

Given the temperature at which water begins to boil, determine atmospheric pressure. Also determine if you are below sea level, at sea level, or above sea level.

Note that the science of this problem is generally correct but the values of 100°C and 100 kPa are approximate and the formula is a simplification of the exact relationship between water’s boiling point and atmospheric pressure.

Input Specification

The input is one line containing an integer \(B\) where \(B \geq 80\) and \(B \leq 200\). This represents the temperature in °C at which water begins to boil.

Output Specification

The output is two lines. The first line must contain an integer which is atmospheric pressure measured in kPa. The second line must contain the integer -1, 0, or 1. This integer represents whether you are below sea level, at sea level, or above sea level, respectively.

Sample Input 1

99

Output for Sample Input 1

95
1

Explanation of Output for Sample Input 1

When \(B=99\), we can substitute into the formula and get \(P= 5 \times 99 - 400\) which equals \(95\). Since 95 kPa is less than 100 kPa, you are above sea level.

Sample Input 2

102

Output for Sample Input 2

100
 -1

Explanation of Output for Sample Input 2

When \(B=102\), we can substitute into the formula and get \(P=5 \times 102 - 400\) which equals \(110\). Since 110 kPa is greater than 100 kPa, you are below sea level.

Problem J2: Silent Auction

Problem Description

A charity is having a silent auction where people place bids on a prize without knowing anyone else’s bid. Each bid includes a person’s name and the amount of their bid. After the silent auction is over, the winner is the person who has placed the highest bid. If there is a tie, the person whose bid was placed first wins. Your job is to determine the winner of the silent auction.

Input Specification

The first line of input contains a positive integer \(N\), where \(1 \leq N \leq 100\), representing the number of bids collected at the silent auction. Each of the next \(N\) pairs of lines contains a person’s name on one line, and the amount of their bid, in dollars, on the next line. Each bid is a positive integer less than \(2000\). The order of the input is the order in which bids were placed.

Output Specification

Output the name of the person who has won the silent auction.

Sample Input 1

3
Ahmed
300
Suzanne
500
Ivona
450

Output for Sample Input 1

Suzanne

Explanation of Output for Sample Input 1

The highest bid placed was 500 and it was placed by Suzanne. Suzanne wins the silent auction.

Sample Input 2

2
Ijeoma
20
Goor
20

Output for Sample Input 2

Ijeoma

Explanation of Output for Sample Input 2

The highest bid placed was 20 and it was placed by both Ijeoma and Goor. Since Ijeoma’s bid was placed first, Ijeoma wins the silent auction.

Problem J3: Secret Instructions

Problem Description

Professor Santos has decided to hide a secret formula for a new type of biofuel. She has, however, left a sequence of coded instructions for her assistant.

Each instruction is a sequence of five digits which represents a direction to turn and the number of steps to take.

The first two digits represent the direction to turn:

The remaining three digits represent the number of steps to take which will always be at least 100.

Your job is to decode the instructions so the assistant can use them to find the secret formula.

Input Specification

There will be at least two lines of input. Each line except the last line will contain exactly five digits representing an instruction. The first line will not begin with 00. The last line will contain 99999 and no other line will contain 99999.

Output Specification

There must be one line of output for each line of input except the last line of input. These output lines correspond to the input lines (in order). Each output line gives the decoding of the corresponding instruction: either right or left, followed by a single space, followed by the number of steps to be taken in that direction.

Sample Input

57234
00907
34100
99999

Output for Sample Input

right 234
right 907
left 100

Explanation of Output for Sample Input

The first instruction is 57234 which is decoded as right 234 because \(5 + 7 = 12\) which is even and 57 is followed by 234.

The second instruction is 00907 which is decoded with the same direction as the previous instruction (right) but with 907 steps.

The third instruction is 34100 which is decoded as left 100 because \(3 + 4 = 7\) which is odd and 34 is followed by 100.

The last line contains 99999 which tells us these are the only three instructions.

Problem J4: Arranging Books

Problem Description

Valentina wants books on a shelf to be arranged in a particular way. Every time she sees a shelf of books, she rearranges the books so that all the large books appear on the left, followed by all the medium-sized books, and then all the small books on the right. She does this by repeatedly choosing any two books and exchanging their locations. Exchanging the locations of two books is called a swap.

Help Valentina determine the fewest number of swaps needed to arrange a shelf of books as she wishes.

Input Specification

The input will consist of exactly one line containing at least one character.

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

7 marks at most \(1000\) characters each character will be L or S
2 marks at most \(500\,000\) characters each character will be L or S
4 marks at most \(1000\) characters each character will be L, M, or S
2 marks at most \(500\,000\) characters each character will be L, M, or S

Output Specification

Output a single integer which is equal to the minimum possible number of swaps needed to arrange the books so that all occurrences of L come first, followed by all occurrences of M, and then all occurrences of S.

Sample Input 1

LMMMS

Output for Sample Input 1

0

Explanation of Output for Sample Input 1

The books are already arranged according to Valentina’s wishes.

Sample Input 2

LLSLM

Output for Sample Input 2

2

Explanation of Output for Sample Input 2

By swapping the S and M, we end up with LLMLS. Then the M can be swapped with the L to its right. This is one way to use two swaps to arrange the books as Valentina wants them to be. It is not possible to use fewer swaps because both S and M need to be moved but using one swap to exchange them does not leave the books arranged as Valentina wants them to be.

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 \(M\)-by-\(N\) 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 \(M\). The second line of input will be a positive integer \(N\). The third line of input will be a positive integer \(K\). The remaining input will be \(K\) 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 \(1\) to \(M\). Columns are numbered left to right from \(1\) to \(N\).

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

1 mark \(M = 1\) \(N=1\) \(K \leq 100\) only one cell, and up to 100 choices by the artist
4 marks \(M = 1\) \(N \leq 100\) \(K \leq 100\) only one row, and up to 100 choices by the artist
5 marks \(M \leq 100\) \(N \leq 100\) \(K \leq 100\) up to 100 rows, up to 100 columns, and up to 100 choices by the artist
5 marks \(MN \leq 5\,000\,000\) \(K \leq 1\,000\,000\) 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