In the very recent term, I got an opportunity to become the teaching assistant for the CSC3100 Data Structures course. The below are compilation of solutions writeup made by me for all the assignments. These solutions are from my own insights and perspectives on the problems and are unofficial. PDF version can be found at the bottom of this page.
Assignment 1
Queue Disorder
Written and developed by: Xuanhao Pan
Editorial by: Yohandi
Description
In a queue, people are supposed to stand in ascending order of their heights. However, due to some confusion, the order of the queue gets disrupted. Your task is to determine the number of disorder pairs in the queue.
A disorder pair is defined as a pair of people
KaTeX can only parse string typed expression
(pi,pj) such that
KaTeX can only parse string typed expression
i<j and
KaTeX can only parse string typed expression
pi is taller than
KaTeX can only parse string typed expression
pj in the queue, which means their order is reversed.
For example, consider a queue with 5 people:
KaTeX can only parse string typed expression
[180,160,175,170,170]. In this case, there are 6 disorder pairs:
KaTeX can only parse string typed expression
(180,160),(180,175),(180,170),(180,170),(175,170) and
KaTeX can only parse string typed expression
(175,170). Please note that
KaTeX can only parse string typed expression
(170,170) is not considered as a disorder pair. Write a program to calculate the number of disorder pairs in a given queue with
KaTeX can only parse string typed expression
N people.
Constraints
KaTeX can only parse string typed expression
1≤N≤106
KaTeX can only parse string typed expression
1≤pi≤109,∀i∈{1,…,N}
Solution
This problem is a straightforward inversion count problem. We can use merge sort to maintain the number of inversions between pairs. Suppose a range
KaTeX can only parse string typed expression
[L,R) accounts for the number of inversions of all pairs in the range. This then recurses into sub-ranges
KaTeX can only parse string typed expression
[L,mid) and
KaTeX can only parse string typed expression
[mid,R). Any indices
KaTeX can only parse string typed expression
i and
KaTeX can only parse string typed expression
j such that
KaTeX can only parse string typed expression
i∈[L,mid) and
KaTeX can only parse string typed expression
j∈[mid,R) can be processed concurrently with the merge procedure for the two half arrays using the sorted property.
Alternatively, we can use a Binary Indexed Tree to count the number of elements within a specific index range. To count only those elements smaller than a certain number, updates can be made following the sequence of the sorted elements. Each element, however, should be mapped into a pair of itself and its index (descendingly) to handle elements with the same value.
Both solutions work in
KaTeX can only parse string typed expression
O(nlogn).
#include<bits/stdc++.h>using namespace std;class BIT {private:...
Star Sequence
Written and developed by: Tianci Hou
Editorial by: Yohandi
Description
Renko Usami observes space through a telescope when she notices a fantastic phenomenon – the number of stars in the fields follows a mathematical pattern.
Specifically, let’s denote the number of stars in the
KaTeX can only parse string typed expression
N-th field by
KaTeX can only parse string typed expression
fN, then
KaTeX can only parse string typed expression
fN satisfies the following expression, and
KaTeX can only parse string typed expression
a,b are given positive integers.
KaTeX can only parse string typed expression
fN=afN−1+bfN−2(N≥2)
Now Renko Usami is curious about how many stars in the
KaTeX can only parse string typed expression
n-th field, but the
KaTeX can only parse string typed expression
n-th field is too far away to be observed through her cheap astronomical telescope. Since there are so many stars, she only cares about the value of the number of stars modulo
KaTeX can only parse string typed expression
m. In other words, she want to know
KaTeX can only parse string typed expression
fnmodm.
Fortunately, Renko Usami is able to observe the two closest star fields to her, and the numbers of stars in fields are
KaTeX can only parse string typed expression
f0 and
KaTeX can only parse string typed expression
f1. Unfortunately, she is going to be late again for her appointment with Merry. Can you write a program for her to calculate
KaTeX can only parse string typed expression
fnmodm?
Constraints
KaTeX can only parse string typed expression
1≤n≤1018
KaTeX can only parse string typed expression
1≤a,b,f0,f1≤100
KaTeX can only parse string typed expression
1≤m≤2⋅109
Solution
To calculate
KaTeX can only parse string typed expression
fnmodm, we use matrix exponentiation to efficiently compute the required term in the sequence. We can construct a vector consisting of
KaTeX can only parse string typed expression
fN and
KaTeX can only parse string typed expression
fN−1 using the following matrix power multiplied by the initial vector, which consists of
n players that are located in different floors. The
KaTeX can only parse string typed expression
i-th player (for each
KaTeX can only parse string typed expression
i such that
KaTeX can only parse string typed expression
1≤i≤n, where
KaTeX can only parse string typed expression
i is an integer) is characterized by the following:
KaTeX can only parse string typed expression
pi, a unique starting floor level
KaTeX can only parse string typed expression
hi, a starting HP (Health Points) value
KaTeX can only parse string typed expression
di, a direction of movement, either U indicating that the
KaTeX can only parse string typed expression
i-th player is going upward or D indicating that the
KaTeX can only parse string typed expression
i-th player is going downward
All players move simultaneously at the same speed in their respective directions. When two players meet, they engage in a battle. The player with the lower health is eliminated instantaneously, while the other continue to move in the same direction at the same speed but with a reduced HP by
KaTeX can only parse string typed expression
1. In a case where two players have identical HP, they are both eliminated simultaneously after the battle.
Your task is to determine the final HP of the surviving players
Constraints
KaTeX can only parse string typed expression
1≤n≤106
KaTeX can only parse string typed expression
1≤pi≤108,∀i∈{1,…,n}
KaTeX can only parse string typed expression
1≤hi≤100,∀i∈{1,…,n}
KaTeX can only parse string typed expression
di∈{U, D
KaTeX can only parse string typed expression
},∀i∈{1,…,n}
Solution
Note that downward-moving players survive only if they win every battle with all players below them that are moving upward. Moreover, there won't be any fight between players that are moving in the same direction.
To solve this, we first sort the players by their starting floor. We then simulate from the lowest floor. If a player is moving up, we add them to a stack. Otherwise, this player battles against every player in the current state of the stack, starting from the one on top.
O(nlogn); however, the constant itself was made strict as stack, the main solution, works in
KaTeX can only parse string typed expression
O(n). The sorting algorithm dominates the runtime of the overall algorithm. The constraint of
KaTeX can only parse string typed expression
pi,∀i∈{1,…,n} should have been lower than or equal to
KaTeX can only parse string typed expression
106 instead of
KaTeX can only parse string typed expression
108 so that we can use the counting sort algorithm, which can be well-applied in
KaTeX can only parse string typed expression
O(n).
Buried Treasure
Written and developed by: Yohandi
Editorial by: Yohandi
Description
Jack Sparrow, the legendary pirate of the Seven Seas, sets sail to an inhabited island in search of a buried treasure. Guided by his map, he believes the treasure lies within a weird-looking trench. The trench stretches across a width of
KaTeX can only parse string typed expression
n. For any given point from
KaTeX can only parse string typed expression
i−1 to
KaTeX can only parse string typed expression
i on the trench’s
KaTeX can only parse string typed expression
x-coordinate, the depth is represented by
KaTeX can only parse string typed expression
di (for each
KaTeX can only parse string typed expression
i such that
KaTeX can only parse string typed expression
1≤i≤n, where
KaTeX can only parse string typed expression
i is an integer).
Jack is wondering about the size of the largest treasure that could possibly fit inside the trench. Wanting to maximize his haul, he turns to you, a trusted member of his crew, to make the calculations. By largest, Jack means the maximum area – the product of width and height – of the rectangular treasure chest that can be buried within the trench’s confines. For example, the following figure shows the largest possible treasure that can fit in a trench with
KaTeX can only parse string typed expression
n=8 and
KaTeX can only parse string typed expression
d=[6,2,5,4,5,1,4,4].
Figure 1
Could you give these calculations a look for our legendary pirate?
Constraints
KaTeX can only parse string typed expression
1≤T≤10
KaTeX can only parse string typed expression
1≤n≤106
KaTeX can only parse string typed expression
1≤di≤106,∀i∈{1,…,n}
The sum of
KaTeX can only parse string typed expression
n over all queries, denoted as
KaTeX can only parse string typed expression
N, does not exceed
KaTeX can only parse string typed expression
106 in each test case.
Solution
This problem is a classic "Maximum Rectangle Area in Histogram" problem with a rephrased and modified statement. However, the proof of optimal chest rotation angles is only between
KaTeX can only parse string typed expression
0∘,90∘,180∘,270∘ is still needed to correctly solve this problem.
The approach is to have a stack that maintains the indices of depths in ascending order so that we can identify the potential of extension (starting points) for rectangles. Each of those rectangles is computed as
KaTeX can only parse string typed expression
Ai=di×(ri−li+1), where
KaTeX can only parse string typed expression
ri is the rightmost index to which the rectangle containing the
KaTeX can only parse string typed expression
i-th index can extend (same goes for
KaTeX can only parse string typed expression
li but on the other direction). The main task is to find
KaTeX can only parse string typed expression
maxiAi.
#include<bits/stdc++.h>using namespace std;longlonglargest_chest(int n, vector<int>&v){longlong ret =0;...
Assignment 3
Node Distance
Written and developed by: Yige Jiang
Editorial by: Yohandi
Description
You are given a tree with
KaTeX can only parse string typed expression
n nodes, where each edge in the tree has a corresponding weight denoting the length of each edge. The nodes in the tree are colored either black or white. Your task is to calculate the sum of distances between every pair of black nodes in the tree. Let
KaTeX can only parse string typed expression
B={b1,b2,…} a set of black nodes, then the answer is formulated as: