University of Waterloo Logo and CEMC Banner

2013 Canadian Computing Competition
Junior Problems

Problem J1: Next in line

Problem Description

You know a family with three children. Their ages form an arithmetic sequence: the difference in ages between the middle child and youngest child is the same as the difference in ages between the oldest child and the middle child. For example, their ages could be 5, 10 and 15, since both adjacent pairs have a difference of 5 years.

Given the ages of the youngest and middle children, what is the age of the oldest child?

Input Specification

The input consists of two integers, each on a separate line. The first line is the age \(Y\) of the youngest child (\(0 \leq Y \leq 50\)). The second line is the age \(M\) of the middle child (\(Y \leq M \leq 50\)).

Output Specification

The output will be the age of the oldest child.

Sample Input 1

12
15

Output for Sample Input 1

18

Sample Input 2

10
10

Output for Sample Input 4
10

Problem J2: Rotating letters

Problem Description

An artist wants to construct a sign whose letters will rotate freely in the breeze. In order to do this, she must only use letters that are not changed by rotation of 180 degrees: I, O, S, H, Z, X, and N.

Write a program that reads a word and determines whether the word can be used on the sign.

Input Specification

The input will consist of one word, all in uppercase letters, with no spaces. The maximum length of the word will be 30 letters, and the word will have at least one letter in it.

Output Specification

Output YES if the input word can be used on the sign; otherwise, output NO.

Sample Input 1

SHINS

Output for Sample Input 1

YES

Sample Input 2

NOISE

Output for Sample Input 2

NO

Problem J3: From 1987 to 2013

Problem Description

You might be surprised to know that 2013 is the first year since 1987 with distinct digits. The years 2014, 2015, 2016, 2017, 2018, 2019 each have distinct digits. 2012 does not have distinct digits, since the digit 2 is repeated.

Given a year, what is the next year with distinct digits?

Input Specification

The input consists of one integer \(Y\) (\(0 \leq Y \leq 10000\)), representing the starting year.

Output Specification

The output will be the single integer \(D\), which is the next year after \(Y\) with distinct digits.

Sample Input 1

1987

Output for Sample Input 1

2013

Sample Input 2

999

Output for Sample Input 2

1023

Problem J4: Time on task

Problem Description

You have been asked by a parental unit to do your chores.

Each chore takes a certain amount of time, but you may not have enough time to do all of your chores, since you can only complete one chore at a time. You can do the chores in any order that you wish.

What is the largest amount of chores you can complete in the given amount of time?

Input Specification

The first line of input consists of an integer \(T\) (\(0 \leq T \leq 100000\)), which is the total number of minutes you have available to complete your chores.

The second line of input consists of an integer \(C\) (\(0 \leq C \leq 100\)), which is the total number of chores that you may choose from. The next \(C\) lines contain the (positive integer) number of minutes required to do each of these chores. You can assume that each chore will take at most \(100000\) minutes.

Output Specification

The output will be the maximum number of chores that can be completed in time \(T\).

Sample Input 1

6
3
3
6
3

Output for Sample Input 1

2

Explanation of Output for Sample Input 1

Chores must be completed in at most 6 minutes. There are 3 chores available. The first chore takes 3 minutes. The second chore takes 6 minutes. The third chore takes 3 minutes. The answer is 2 since only 2 of these chores can be completed in 6 minutes of time. Specifically, the first and last chore can be completed in the allowable time. It is not possible to complete all 3 chores in 6 minutes.

Sample Input 2

6
5
5
4
3
2
1

Output for Sample Input 2

3

Explanation of Output for Sample Input 2

Tasks 3, 4, and 5 can be completed in 6 minutes. It is not possible to complete more than 3 tasks in 6 minutes.

Problem J5: Chances of winning

Problem Description

You want to determine the chances that your favourite team will be the champion of a small tournament.

There are exactly four teams. At the end of the tournament, a total of six games will have been played with each team playing every other team exactly once. For each game, either one team wins (and the other loses), or the game ends in a tie. If the game does not end in a tie, the winning team is awarded three points and the losing team is awarded zero points. If the game ends in a tie, each team is awarded one point.

Your favourite team will only be the champion if it ends the tournament with strictly more total points than every other team (i.e., a tie for first place is not good enough for your favourite team).

The tournament is not over yet but you know the scores of every game that has already been played. You want to consider all possible ways points could be awarded in the remaining games that have not yet been played and determine in how many of these cases your favourite team will be the tournament champion.

Input Specification

The first line of input will contain an integer \(T\) which is your favourite team (\(1 \leq T \leq 4\)).

The second line will contain an integer \(G\), the number of games already played (\(0 \leq G \leq 5\)).

The next \(G\) lines will give the results of games that have already been played. Each of these lines will consist of four integers \(A\), \(B\), \(S_A\), \(S_B\) separated by single spaces where \(1\leq A < B \leq 4\), and \(S_A, S_B \geq 0\) . This corresponds to a game between team \(A\) (which had score \(S_A\)) and team \(B\) (which had score \(S_B\)) where team \(A\) won if \(S_A>S_B\), team \(B\) won if \(S_A<S_B\) and the game ended in a tie if \(S_A=S_B\). The pairs \(A\) and \(B\) on the input lines are distinct, since no pair of teams plays twice.

Output Specification

The output will consist of a single integer which is the number of times that team \(T\) is the champion over all possible awarding of points in the remaining games in the tournament.

Sample Input 1

3
3
1 3 7 5
3 4 0 8
2 4 2 2

Output for Sample Input 1

0

Explanation of Output for Sample Input 1

Team 3 has lost two of its three games, and team 4 has tied one and won one, which gives 4 points to team 4. Even if team 3 wins its final game, it cannot have more points than team 4, and thus, will not be champion.

Sample Input 2

3
4
1 3 5 7
3 4 8 0
2 4 2 2
1 2 5 5

Output for Sample Input 2

9

Explanation of Output for Sample Input 2

After these games, we know the following:

Team Points
1 1
2 2
3 6
4 1

There are two remaining games (team 3 plays team 2; team 1 plays team 4). Teams 1, 2 or 4 cannot achieve 6 points, since even if they win their final games, their final point totals will be 4, 5 and 4 respectively. Thus, out of the 9 possible outcomes (2 matches with 3 different possible results per match), team 3 will be the champion in all 9 outcomes.