Solutions (in C++) and explanations for problems S1-S5 on the 2021 Canadian Computing Competition.
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:
Notice that repeating an instruction twice cancels it out. Use a set to collect all unique instructions. Count all of the instructions on rows () vs. columns (). The answer is going to be .
( cells)
Time Complexity:
Let be the sum of the required times for all friends to gather at location . Notice 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 . On each iteration, test 3 points to determine the bounds for the next iteration. Refer to my solution for specific details.
Time Complexity: , something like
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 () to every other station using only walking paths. Next, add the time for the subway to reach station to the walking time from to . 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 and station are swapped, subtract the time for the train to reach station from the total time (calculated earlier) for station . Do the same for , and then just add the train times again. This will allow the swaps to be done in and minimum queries to be done in if something like std::multiset
is used.
Time Complexity:
Notice that it is optimal for (in the answer array ) to be the least common multiple (LCM) of all of constraints that pass over it . Afterwards, we can check if all the constraints are satisfied.
Notice that . We can maintain 16 difference arrays to keep track of which ranges are a constraint for all .
Afterwards, we can build a segment tree in time using the information from the difference arrays, and simply do range GCD queries with the segment tree to verify that all constraints are satisfied.