University of Waterloo Logo and CEMC Banner

2016 Canadian Computing Competition
Junior Problems

Problem J1: Tournament Selection

Problem Description

Each player in a tournament plays six games. There are no ties. The tournament director places the players in groups based on the results of games as follows:

Write a program to determine which group a player is placed in.

Input Specification

The input consists of six lines, each with one of two possible letters: W (to indicate a win) or L (to indicate a loss).

Output Specification

The output will be either 1, 2, 3 (to indicate which Group the player should be placed in) or -1 (to indicate the player has been eliminated).

Sample Input 1

W
L
W
W
L
W

Output for Sample Input 1

2

Sample Input 2

L
L
L
L
L
L

Output for Sample Input 2

-1

Problem J2: Magic Squares

Problem Description

Magic Squares are square arrays of numbers that have the interesting property that the numbers in each column, and in each row, all add up to the same total.

Given a \(4\times 4\) square of numbers, determine if it is magic square.

Input Specification

The input consists of four lines, each line having 4 space-separated integers.

Output Specification

Output either magic if the input is a magic square, or not magic if the input is not a magic square.

Sample Input 1

16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Output for Sample Input 1

magic

Explanation for Output for Sample Input 1

Notice that each row adds up to 34, and each column also adds up to 34.

Sample Input 2

5 10 1 3
10 4 2 3
1 2 8 5
3 3 5 0

Output for Sample Input 2

not magic

Explanation for Output for Sample Input 2

Notice that the top row adds up to 19, but the rightmost column adds up to 11.

Problem J3: Hidden Palindrome

Problem Description

A palindrome is a word which is the same when read forwards as it is when read backwards. For example, mom and anna are two palindromes.

A word which has just one letter, such as a, is also a palindrome.

Given a word, what is the longest palindrome that is contained in the word? That is, what is the longest palindrome that we can obtain, if we are allowed to delete characters from the beginning and/or the end of the string?

Input Specification

The input will consist of one line, containing a sequence of at least 1 and at most 40 lowercase letters.

Output Specification

Output the total number of letters of the longest palindrome contained in the input word.

Sample Input 1

banana

Output for Sample Input 1

5

Explanation for Output for Sample Input 1

The palindrome anana has 5 letters.

Sample Input 2

abracadabra

Output for Sample Input 2

3

Explanation for Output for Sample Input 2

The palindromes aca and ada have 3 letters, and there are no other palindromes in the input which are longer.

Sample Input 3

abba

Output for Sample Input 3

4

Problem J4: Arrival Time

Problem Description

Fiona commutes to work each day. If there is no rush-hour traffic, her commute time is 2 hours. However, there is often rush-hour traffic. Specifically, rush-hour traffic occurs from 07:00 (7am) until 10:00 (10am) in the morning and 15:00 (3pm) until 19:00 (7pm) in the afternoon. During rush-hour traffic, her speed is reduced by half.

She leaves either on the hour (at XX:00), 20 minutes past the hour (at XX:20), or 40 minutes past the hour (at XX:40).

Given Fiona’s departure time, at what time does she arrive at work?

Input Specification

The input will be one line, which contains an expression of the form HH:MM, where HH is one of the 24 starting hours (00, 01, \(\ldots\), 23) and MM is one of the three possible departure minute times (00, 20, 40).

Output Specification

Output the time of Fiona’s arrival, in the form HH:MM.

Sample Input 1

05:00

Output for Sample Input 1

07:00

Explanation for Output for Sample Input 1

Fiona does not encounter any rush-hour traffic, and leaving at 5am, she arrives at exactly 7am.

Sample Input 2

07:00

Output for Sample Input 2

10:30

Explanation for Output for Sample Input 2

Fiona drives for 3 hours in rush-hour traffic, but only travels as far as she normally would after driving for 1.5 hours. During the final 30 minutes (0.5 hours) she is driving in non-rush-hour traffic.

Sample Input 3

23:20

Output for Sample Input 3

01:20

Explanation for Output for Sample Input 3

Fiona leaves at 11:20pm, and with non-rush-hour traffic, it takes two hours to travel, so she arrives at 1:20am the next day.

Problem J5: Tandem Bicycle

Problem Description

Since time immemorial, the citizens of Dmojistan and Pegland have been at war. Now, they have finally signed a truce. They have decided to participate in a tandem bicycle ride to celebrate the truce. There are \(N\) citizens from each country. They must be assigned to pairs so that each pair contains one person from Dmojistan and one person from Pegland.

Each citizen has a cycling speed. In a pair, the fastest person will always operate the tandem bicycle while the slower person simply enjoys the ride. In other words, if the members of a pair have speeds \(a\) and \(b\), then the bike speed of the pair is \(\max(a,b)\). The total speed is the sum of the \(N\) individual bike speeds.

For this problem, in each test case, you will be asked to answer one of two questions:

Input Specification

The first line will contain the type of question you are to solve, which is either \(1\) or \(2\).

The second line contains \(N\) (\(1 \leq N \leq 100\)).

The third line contains \(N\) space-separated integers: the speeds of the citizens of Dmojistan.

The fourth line contains \(N\) space-separated integers: the speeds of the citizens of Pegland.

Each person’s speed will be an integer between \(1\) and \(1\ 000\ 000\).

For 8 of the 15 available marks, questions of type \(1\) will be asked. For 7 of the 15 available marks, questions of type \(2\) will be asked.

Output Specification

Output the maximum or minimum total speed that answers the question asked.

Sample Input 1

1
3
5 1 4
6 2 4

Output for Sample Input 1

12

Explanation for Output for Sample Input 1

There is a unique optimal solution:

Sample Input 2

2
3
5 1 4
6 2 4

Output for Sample Input 2

15

Explanation for Output for Sample Input 2

There are multiple possible optimal solutions. Here is one optimal solution:

Sample Input 3

2
5
202 177 189 589 102
17 78 1 496 540

Output for Sample Input 3

2016

Explanation for Output for Sample Input 3

There are multiple possible optimal solutions. Here is one optimal solution:

This sum yields \(202+540+189+589+496=2016\).