MCO 2015 Problems & Solutions

Post on 11-Jan-2016

12 views 0 download

description

MCO P And S

transcript

3rd MCO 2015Problems & Solutions

BadmintonProposed by Mohd Suhaimi Ramly

Badminton

● Task: (i) keep track of badminton scores and (ii) determine the winner of the match.

● Goal: test (simple) logic programming skills.● At the end, need to determine winner.● Keep track of games won?● Winner of last point is winner of the match!

Badminton - Pseudocodepoints = ABAAABBB...

score_A = score_B = 0 // set scores to 0-0

for each point ← points:

if point = A then score_A++

if point = B then score_B++

if score_A = 21 or score_B = 21: // game is won

print score_A + “-” + score_B

score_A = score_B = 0 // reset scores to 0-0

print point // winner of last point is winner of the match

HoneyProposed by Lim Yun Kai

Honey

● Task: Collect maximum amount of honey from N place using pot of size M in at most K steps.

● Solution: Greedy.

Honey - Solution● Greedily take honey from hive with largest amount of honey.

● Can be achieve using a priority queue.

● The naive way involved actual simulating of K steps.

● Complexity: O(N log N + K log N), fails on subtask 3.

● A better way is to collect as many “full pot” as possible while taking the input.

● Then sort the rest, and take from largest to smallest.

● Complexity: O(N log N),

Honey - Pseudocode (C++ style)vector<int> honey(n);

fullpot = 0;

for i = 0 -> n-1:

cin >> honey[i];

fullpot += honey[i]/M;

honey[i] %= M;

if fullpot >= K: output K*M; end;

else: K -= fullpot; ans = fullpot*M;

sort(honey, largest to smallest);

for i = 0 -> min(K, N)-1:

ans += honey[i];

output ans; end;

BitcoinProposed by Quah Fu Yong

Bitcoin

● Task: Given a grid of known size and a number of points, compute the maximum squared distance between the points in the grid

● Test precomputation skills

Bitcoin - Key Observation

1 2 3 4 5 6

1

2

3

4

5

● Think : Compare the distance of the green box with all the box in all the colored boxes in row 1

● Distance with the blue squares are definitely not the maximum distance as there are still better boxes besides them

● Hence, comparisons with the middle (i.e. non-sidemost) points are redundant

● We can do precompute the leftmost & rightmost points or every row

Bitcoin - AlgorithmHigh level description:

- left = [], right = []- find the leftmost and rightmost points for every row,

store relevant information in left and right- Evaluate the maximum distance between all the points in

the array- Implementing precomputation naively, however, fails

Subtask 3

Bitcoin - Algorithm- It is not necessary to compare the distance between all

the leftmost points themselves (similar for right ones)- The only comparisons we need to make are:

- distance between all left and right [ (M-1) * M ]- distance between every row’s left and right [ M ]

- As there are negative coordinates, C++ implementation needs to take array indexing problems into account

- Problem can be solved by offsetting the index (i.e. : instead of arr[i] = x, do arr[i+M] = x) This works so long the offset value is large enough

SecretProposed by Quah Fu Yong

Secret

● Task: Given two strings, find if it is possible to ‘rotate’ one of the string to obtain the other string

● Test dynamic programming skills

Secret - Common Substringmutated_first = first + first // concat it

if first.len == second.len && hasSubString(mutated_first, second):

print “YES”

else:

print “NO”

def hasSubString(first, second):

// run any reasonble string searching algorithm

// Since first and second are array / vectors, cannot run strstr directly without manipulation

// Simple string searching algorithm : KMP algorithm

Secret - Standard Library Soln- Represent mutated_first and second the string as a

series of char (Using base encoding)- check if second is a substring of mutated_first using

strstr(const char *, const char*) [ from <string.h> library ] (http://www.cplusplus.com/reference/cstring/strstr/)

- Requires knowledge of base encoding or padding, and standard library

TrainsProposed by Ng Jia Jen

Trains

● Task: To find a path that requires the least amount of cost from a certain point A to point B

● Graph problem● Solved by Dijkstra's shortest path algorithm

Trains - Naive Solution- Try every permutation of paths that connects both

points

- Among all permutations, pick the one with the least cost

- However will not suffice (obviously) due to too many permutations that a potential path can take from Point A to Point B

Trains - Complete Solution1. Start from a starting point2. Add ‘reachable’ points from currently traversed points

along with the total cost of traversing those points to a list

3. From that list of potential points, pick the point with the lowest cost and traverse there

4. Repeat Step 2 and 3 until ending point is reached

Trains - Graphical DemoPotential Points

Cost Coordinates

99 1,3

Begin at point (1,3) and end at point (4,5)

Trains - Graphical DemoPotential Points

Cost Coordinates

Traverse point (1,3)

Trains - Graphical DemoPotential Points

Cost Coordinates

119 1,2

119 1,4

119 2,3

Add all potential points to a list

Trains - Graphical DemoPotential Points

Cost Coordinates

Traverse lowest potential point.In this case, all 3 points have the lowest cost. Traverse all...

Trains - Graphical DemoPotential Points

Cost Coordinates

129 1,1

139 2,2

139 2,2

129 2,4

129 2,4

129 1,5

Keep on adding potential points. Note that the points with ‘-1’ are not considered

Trains - Graphical DemoPotential Points

Cost Coordinates

129 1,1

139 2,2

139 2,2

129 2,4

129 2,4

129 1,5

Also notice that some points (ex. (2,2)) overlap which demonstrates that some points can be reached from

2 or more different points

From (1,2):

From (2,3):

Trains - Graphical DemoPotential Points

Cost Coordinates

129 1,1

139 2,2

139 2,2

129 2,4

129 2,4

129 1,5

Pick lowest cost

Trains - Graphical DemoPotential Points

Cost Coordinates

139 2,2

139 2,2

Traverse lowest cost

Trains - Graphical DemoPotential Points

Cost Coordinates

139 2,2

139 2,2

139 2,5

139 2,5

159 2,1

Add potential points

Trains - Graphical DemoPotential Points

Cost Coordinates

159 2,1

169 2,1

Traverse and add potential points

Trains - Graphical DemoPotential Points

Cost Coordinates

159 2,1

169 2,1

Notice that (2,1) can be reached from either (1,1) with a cost of 159 or (2,2) with a cost of 169.

From (1,1):

From (2,2):

Trains - Graphical DemoPotential Points

Cost Coordinates

The lowest cost (159) is chosen.

Trains - Complete SolutionAlgorithm goes on until the ending point is reached…..

Trains - Complexity (Subtask 2)Let number of nodes = VFor every node we visit, we need to insert its potential points ( O(1) ) and also we would have to find the minimum cost potential point in the list.Assuming worst case, the list would have a maximum size of (4*V), where 4 is for the 4 adjacent nodes that are beside a certain node.To find the minimum point, a linear search can be utilised for a complexity of O(4*V) = O(V).Since for every node we would need to search the list for the minimum point and add the potential points, complexity would be O(V) * O(V) + O(1) = O(V²).Since V = N² as the map is a square grid, overall complexity = O(N⁴).

Trains - Complexity (Subtask 3)This can be speeded up by utilising a priority queue to speed up the linear search from O(V) to O(log V).However, insertion of new nodes would also take O(log V) instead of O(1) as in linear search.Nevertheless, it is still faster as for every node, we insert a new potential nodes( O(log V) ) and select the minimum cost node ( still O(log V) ) with complexity of O(log V) + O(log V) = O(log V) for a certain node.Since there are V nodes, complexity = O(V * log V).V = N², hence overall complexity = O(N² * log N²)