You are on page 1of 11

Solution Sketches

ACM ICPC 2014 (Amritapuri / Coimbatore)

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
-

Realize that substring being common is same as at least one character


being common.
Construct array A[1<<16], A[mask] = number of substrings which have
exactly those chars in mask. Eg A[5] = # of substrings having only A and
C characters.
Construct array B[1<<16] from A. B[mask] = Sum{A[i], i & mask = 0}
Your answer will be A[i] * ((N c 2) + N - B[i]). 3^16 algorithm to find B is
easy. That will TLE.
write DP as: B[i] = DP[16][i]
DP[0][i] = A[~i]
DP[l][i] = DP[l-1][i] + (i&(1<<(l-1)) ? DP[l-1][i^1] : 0);
Intuitively, DP[l][i] = replace any 1 in positions 1...l of binary-rep(i) by a *
and take the sum by allowing * = 0 and * = 1.
Eg. DP[3][13] = DP[3][1101] = DP[0][1*0*] = DP[0][1101] + DP[0][1100] +

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.

You might also like