Hello everyone, I hope you can enjoy this special round for Zepto Lab. Here are the solutions of this round.
A. King of Thieves
This task is easy for many of you. We can just iterate over all possible
Unable to parse markup [type=CF_TEX]
andUnable to parse markup [type=CF_TEX]
, then we can computeUnable to parse markup [type=CF_TEX]
, and check whether this subsequence satisfies the condition mentioned in the task.B. Om Nom and Dark Park
We use greedy and recursion to solve this task. For each tree rooted at
Unable to parse markup [type=CF_TEX]
, we adjust its two subtrees at first, using recursion. Then we increase one edge fromUnable to parse markup [type=CF_TEX]
's child toUnable to parse markup [type=CF_TEX]
.C. Om Nom and Candies
If there is a kind of candy which weighs greater than
Unable to parse markup [type=CF_TEX]
, then we can iterate over the number of it to buy, which is less thanUnable to parse markup [type=CF_TEX]
.Otherwise, without loss of generality we suppose
Unable to parse markup [type=CF_TEX]
. If the number of the blue candies that Om Nom eats is more thanUnable to parse markup [type=CF_TEX]
, he could eatUnable to parse markup [type=CF_TEX]
red candies instead ofUnable to parse markup [type=CF_TEX]
blue candies, becauseUnable to parse markup [type=CF_TEX]
. It means the number of the blue candies will be less thanUnable to parse markup [type=CF_TEX]
, and we can iterate over this number.D. Om Nom and Necklace
This task is to determine whether a string is in the form of
Unable to parse markup [type=CF_TEX]
for each prefixes of a given stringUnable to parse markup [type=CF_TEX]
For a prefix P, let's split it into some blocks, just like
Unable to parse markup [type=CF_TEX]
, whichUnable to parse markup [type=CF_TEX]
is a prefix ofUnable to parse markup [type=CF_TEX]
. Obviously, if we use KMP algorithm, we can do it in linear time, and the length ofUnable to parse markup [type=CF_TEX]
will be minimal. There are only two cases :Unable to parse markup [type=CF_TEX]
.Unable to parse markup [type=CF_TEX]
. WhenUnable to parse markup [type=CF_TEX]
. Assume thatUnable to parse markup [type=CF_TEX]
appearsUnable to parse markup [type=CF_TEX]
times. Consider "ABABAB....ABABA", the lastUnable to parse markup [type=CF_TEX]
must be a suffix ofUnable to parse markup [type=CF_TEX]
, and it must be likeUnable to parse markup [type=CF_TEX]
, soUnable to parse markup [type=CF_TEX]
will be likeUnable to parse markup [type=CF_TEX]
, and so willUnable to parse markup [type=CF_TEX]
. By greedy algorithm, the length ofUnable to parse markup [type=CF_TEX]
will be minimal, so it will beUnable to parse markup [type=CF_TEX]
, whereUnable to parse markup [type=CF_TEX]
appearsUnable to parse markup [type=CF_TEX]
times. AndUnable to parse markup [type=CF_TEX]
will beUnable to parse markup [type=CF_TEX]
, whereUnable to parse markup [type=CF_TEX]
appearsUnable to parse markup [type=CF_TEX]
times. So we just need to check whetherUnable to parse markup [type=CF_TEX]
.Unable to parse markup [type=CF_TEX]
. WhenUnable to parse markup [type=CF_TEX]
, the strategy is similar toUnable to parse markup [type=CF_TEX]
. A will be like "SS...ST", and its length will be minimal. At last we just need to check whetherUnable to parse markup [type=CF_TEX]
.
The total time complexity is
Unable to parse markup [type=CF_TEX]
.E. Transmitting Levels
Our task is to compute at least how many number of blocks are needed to partition a circular sequence into blocks whose sum is less than
Unable to parse markup [type=CF_TEX]
.By monotonicity, it is easy to get the length of maximal blocks which starts from 1 to
Unable to parse markup [type=CF_TEX]
inUnable to parse markup [type=CF_TEX]
. Assume the block with minimal length isUnable to parse markup [type=CF_TEX]
and its length isUnable to parse markup [type=CF_TEX]
, it is obvious that whatever the blocks are, there must be a block that it starts inUnable to parse markup [type=CF_TEX]
. So, we can iterate over all theUnable to parse markup [type=CF_TEX]
numbers ofUnable to parse markup [type=CF_TEX]
, making it the start of a block, and calculate the number of blocks.Notice that all the lengths of blocks is (non-strictly) greater than
Unable to parse markup [type=CF_TEX]
, therefore the number of blocks we need is at mostUnable to parse markup [type=CF_TEX]
. We need to iterateUnable to parse markup [type=CF_TEX]
times, but each time we can get the answer inUnable to parse markup [type=CF_TEX]
, so finally we can check whether the answer is legal inUnable to parse markup [type=CF_TEX]
.F. Pudding Monsters
Actually this problem is to compute how many segments in a permutation forms a permutation of successive integers.
We use divide and conquer to solve this problem.
If we want to compute the answer for an interval
Unable to parse markup [type=CF_TEX]
, we divide this interval into two smaller onesUnable to parse markup [type=CF_TEX]
whereUnable to parse markup [type=CF_TEX]
. We only care about the segments which crossesUnable to parse markup [type=CF_TEX]
. We call the first intervalUnable to parse markup [type=CF_TEX]
and the latter oneUnable to parse markup [type=CF_TEX]
.Considering the positiions of maximum numbers and minimum numbers in these valid segments, There are four possible cases:
- the maximum number is in
Unable to parse markup [type=CF_TEX]
, the the minimum is also inUnable to parse markup [type=CF_TEX]
; - the maximum number is in
Unable to parse markup [type=CF_TEX]
, the the minimum is also inUnable to parse markup [type=CF_TEX]
; - the maximum number is in
Unable to parse markup [type=CF_TEX]
, the the minimum is inUnable to parse markup [type=CF_TEX]
; - the maximum number is in
Unable to parse markup [type=CF_TEX]
, the the minimum is inUnable to parse markup [type=CF_TEX]
;
Let
Unable to parse markup [type=CF_TEX]
be the given sequence and we defineUnable to parse markup [type=CF_TEX]
. Similarly we can defineUnable to parse markup [type=CF_TEX]
.For simplicity we only cares about case 1 and case 4.
In Case 1, we iterate over the start position of the segment, so we know the maximum and minimum number so we can compute the length of the segment and check the corresponding segment using
Unable to parse markup [type=CF_TEX]
andUnable to parse markup [type=CF_TEX]
.In Case 4, we iterate over the start position again, denoted as
Unable to parse markup [type=CF_TEX]
. Suppose the right end isUnable to parse markup [type=CF_TEX]
, then we know thatUnable to parse markup [type=CF_TEX]
so we can limitUnable to parse markup [type=CF_TEX]
into some range. Another constraint forUnable to parse markup [type=CF_TEX]
is thatUnable to parse markup [type=CF_TEX]
, i.e.Unable to parse markup [type=CF_TEX]
. Note that whenUnable to parse markup [type=CF_TEX]
varies, the valid range forUnable to parse markup [type=CF_TEX]
also varies, but the range is monotone, so we can maintain how many times a number appears in linear time.It's easy to show that this algorithm runs for
Unable to parse markup [type=CF_TEX]
, by Master Theorem.There are some other people using segment trees. You can see a nice implement here
G. Spiders Evil Plan
In this task, we are given a tree and many queries. In each query, we are supposed to calculate the maximum total length of
Unable to parse markup [type=CF_TEX]
paths with the constraint thatUnable to parse markup [type=CF_TEX]
must be covered.Consider
Unable to parse markup [type=CF_TEX]
is the union of the paths (it contains nodes and edges).For each query
Unable to parse markup [type=CF_TEX]
, ifUnable to parse markup [type=CF_TEX]
, then there is always a method thatUnable to parse markup [type=CF_TEX]
is connected.
Further, we could get the following theorem:
For an unrooted tree, if it has
Unable to parse markup [type=CF_TEX]
leaves, thenUnable to parse markup [type=CF_TEX]
paths can cover this tree completely.
Proof for this theorem is that, if some edge
Unable to parse markup [type=CF_TEX]
is not covered, we can interchange two paths, i.e. we change two pathsUnable to parse markup [type=CF_TEX]
andUnable to parse markup [type=CF_TEX]
toUnable to parse markup [type=CF_TEX]
andUnable to parse markup [type=CF_TEX]
, forUnable to parse markup [type=CF_TEX]
in the subtree ofUnable to parse markup [type=CF_TEX]
andUnable to parse markup [type=CF_TEX]
in the subtree ofUnable to parse markup [type=CF_TEX]
.
So a query
Unable to parse markup [type=CF_TEX]
could be described as :Find
Unable to parse markup [type=CF_TEX]
leaves in the tree, with nodeUnable to parse markup [type=CF_TEX]
inUnable to parse markup [type=CF_TEX]
, and maximize the total of weight of the edges inUnable to parse markup [type=CF_TEX]
.For a query
Unable to parse markup [type=CF_TEX]
, we can makeUnable to parse markup [type=CF_TEX]
the root. Then this task is how to choose the leaves. Note that we could select leaves one by one, every time we select the leaf which makes answer larger without selecting the others, as follow :
But if for every query we need to change the root, the time complexity cannot be accepted. Assuming the longest path in the tree is
Unable to parse markup [type=CF_TEX]
, we could find that whatever the query is,Unable to parse markup [type=CF_TEX]
will contain eitherUnable to parse markup [type=CF_TEX]
orUnable to parse markup [type=CF_TEX]
certainly.
So, we just need to make
Unable to parse markup [type=CF_TEX]
andUnable to parse markup [type=CF_TEX]
the root in turn, and get the maximum answers. However, there is another problem :Unable to parse markup [type=CF_TEX]
may not be inUnable to parse markup [type=CF_TEX]
. Like this :
But it doesn't matter. We just need to link
Unable to parse markup [type=CF_TEX]
with the selected, and erase some leaf. Of course after erasing, the answer should be maximum.
Thanks, for all of your excellent performance!







