Algorithms, CCC, Data Structures

CCC 2021 Senior Solutions

By 05st

Solutions (in C++) and explanations for problems S1-S5 on the 2021 Canadian Computing Competition.

S1

Problem Statement
Solution

Area of each fence piece is the area of the triangle on top + the area of the rectangle below. Sum up for the final answer.

Time Complexity: O(N)O(N)

S2

Problem Statement
Solution

Notice that repeating an instruction twice cancels it out. Use a set to collect all unique instructions. Count all of the instructions on rows (rr) vs. columns (cc). The answer is going to be r(nc)+c(mr)r(n-c) + c(m-r).

(n×mn \times m cells)

Time Complexity: O(KlogK)O(K\log K)

S3

Problem Statement
Solution

Let f(x)f(x) be the sum of the required times for all NN friends to gather at location xx. Notice f(x)f(x) is a convex function (I figured this out by simply observing the outputs). This means we can apply binary search to find the minimum of f(x)f(x). On each iteration, test 3 points to determine the bounds for the next iteration. Refer to my solution for specific details.

Time Complexity: O(N)O(N), something like O(Nlog(220))O(N\log(2^{20}))

S4

Problem Statement
Solution

First notice that it is always going to be optimal to take some portion of the subway only at the beginning. If you get off the subway and take it again at some other station, it would be the same as staying on the subway.

Do a breadth-first-search to find the shortest distances from the final station (NN) to every other station using only walking paths. Next, add the time for the subway to reach station xx to the walking time from xx to NN. The answer for each day is going to be the minimum among all stations.

To answer the queries efficiently, notice that exactly two stations will be affected each day. So, on the beginning of the day where station aa and station bb are swapped, subtract the time for the train to reach station aa from the total time (calculated earlier) for station aa. Do the same for bb, and then just add the train times again. This will allow the swaps to be done in O(logn)O(\log n) and minimum queries to be done in O(logn)O(\log n) if something like std::multiset is used.

Time Complexity: O(DlogN+N+W)O(D\log N + N + W)

S5

Problem Statement
Solution

Notice that it is optimal for AiA_i (in the answer array AA) to be the least common multiple (LCM) of all zz of constraints that pass over it (l,r,z)(l, r, z) lirl \leq i \leq r. Afterwards, we can check if all the constraints are satisfied.

Notice that 1z161 \leq z \leq 16. We can maintain 16 difference arrays to keep track of which ranges are a constraint for all zz.

Afterwards, we can build a segment tree in O(n)O(n) time using the information from the difference arrays, and simply do O(logn)O(\log n) range GCD queries with the segment tree to verify that all constraints are satisfied.

Comments