Professional Documents
Culture Documents
Problem A
- Observation: For a partially constructed
subsequence, what is the *maximum* prefix
of the pattern you can match?
- Next character of the subsequence
determines new length of prefix match
- Modified KMP / Automata: F(pre1, c) = pre2
- DP to compute distinct subsequences.
- O(26*|S|*|P|)
Problem B
The problem asks for: given an undirected graph, can you find a Shortest
path tree (SPT) rooted at 0 that is also a MST
Execute dijkstra to find a SPT, however, in case of ties, always use the
shortest edge entering any vertex. Lets call this tree T.
It can be shown that if a SPT (T) exists having same weight as an MST,
then you can replace each parent-child edge of T by corresponding edge
of T, and edge weight never increases in the process.
Therefore, IF SPT = MST is possible, then T is one such SPT. check if T is
a MST, you are done.
Problem C
Observation: We can only consider values A
[i]%M.
Precompute and store for every i, sum of all
values <= i.
For each i, we need to find no. of
candidates Y, such that (A[i]+Y)%M <= X.
Can be split easily into 2 cases & solved.
O(N)
Problem D
- Dolls dont nest if there are two dolls of the
same size.
- Check input array for duplicate elements.
- O(NLogN) or O(N)
Problem E
- Observation: For every factor of i > sqrt(i)
theres another factor < sqrt(i)
- Sieve of |B - A + 1|, where S[i] = #factors of
(A + i)
- Iterate till sqrt (B) for the smaller factor.
- Update factors count.
Problem F
-
Problem G
- Observation: Hugpile order == Heap
- ith element can be in position P iff position
has at most i-1 ancestors and at most n-i
descendants
- run a binary search for lowest possible value
of P
- run a binary search for highest possible
value of P
Problem H
- Fix n colors in set 1, and put remaining (kn) colors in set 2.
- Observation: The size of set 2 is always
constant = (k-n).
- Try forming m cycles (for all m) and
problem reduces to n-m.
- O(n^2) dp. Reduce to O(n), using math.
Problem I
- Simulation.
- Find positions of both players after each
minute and check if they are matching.
- Need to take care of cycles.
- Complexity: O(S).
Problem J
- Break each circle into arcs such that any arc
is not intersected by any other circle.
- For each circle, for each arc, check if that
arc is enclosed by some other circle that is
drawn later. If not add length of that arc to
answer.
- O(N^3) solution runs in time.
- O(N^2) also possible.