T1 bargain
Operation 1 denotes “Choose a digit and delete it”, and operation 2 denotes deleteing all the remaining digits in one go.
Hint 1: When operation 2 is better than operation 1?
Hint 2: Enumerate from right to left.
It’s easy to notice that we won’t leave too much digits for operation 2. Furthermore, the number of left digits, , won’t exceed 5, since for all .
Consider dynamic programming. Let be the minimum cost to delete all the last digits, while digits are deleted using operation 2.
Let be the -th digit of , counting from the least significant digit. Now we have the following transition:
- (operation 1)
- (operation 2)
Thus, we can solve this problem in time, where in this case.
T2 water
The hardest problem in this round!
Operation 1 denotes subtraction, and operation 2 denotes addition.
Hint 1: Adjust the order of the operations.
Hint 2: Can you determine whether a sequence of operation 1 is valid?
Hint 3: Use binary search.
We need some observations to make this problem easier.
Observation 1
One can always use all operation 1 before any operation 2.
It’s straightforward, so I omit the proof.
However, it leads to the first key observation:
Observation 2
Let be the temperature of vertex after we use up operation 1.
The answer is yes (i.e.
Huoyu) if and only if , and .
To proof it, consider the root. Since we can only increase the temperature using operation 2, should hold. Then we add to the root, and for every son , there should be , or will remain positive.
Now the temperature of root is , so we can simply remove it and check the remain forest. The above observation can then be proved by induction.
Let be the number of operation 1 done on vertex . (If isn’t a leaf, we can simply let .)
We need to check whether we can managed to satisfy the following inequalities:
However, it seems impossible to maintain the information, since and can be extremely large. So we need observation 3:
Observation 3
for all vertex , there exists an interval (possibly empty), such that is valid (with all the constraints inside subtree ) .
The proof also needs induction.
Firstly, is easy to compute: . Suppose we know , and every subtree satisfy the above lemma.
It’s obvious that we only need to consider the case where there aren’t any empty intervals.
Suppose is valid. Now we need to proof is also valid.
There must exist a whose , or is violated. Then we decrease by .
For , still holds, since we decrease at both sides of the inequality; and for other than , the left hand side increases by while the right hand side remains the same.
Thus, is still valid.
One corner case is that , then we simply need to inherit from its son.
Given observation 3, a fan of Um_nik should immediately come up with binary search.
In detail, we can first compute , then use binary search to compute . We only need to check whether is valid.
Enumerate through every son vertex of . Since we have . So . If this interval is empty then is obviously invalid.
Otherwise, we can compute bound of by summing up all these intervals, and is valid iff it falls into such bound.
Thus, we can solve the problem in time.
Please take care of the bound while binary searching, and maybe __int128 is neccessary in some cases.
T3 anti
Hint 1: solve the 2/5 scored problem, i.e. come up with a solution without maximizing k.
Hint 2: solve Special Property B, i.e. .
This is a deterministic approach, which needs some case work.
Consider when the answer is no (i.e. Shuiniao).
- If only contains one kind of character; Or
- If only contains two kinds of character, and the second kind only occurs at the middle of string (i.e n is odd and only is different from the rest).
It’s obvious that such condition is sufficient.
We can also show that such condition are necessary, by giving a construction of the answer sequence.
After we exclude the situation with no solution, one should consider how to maximize . After examining anti2.ans one should notice almost every subsequence has length . It is because 2 different characters can form a valid subsequence, which seems optimal.
stage 0
So, if we only need to calculate k, one can easily come up with a greedy solution: Every time, we pick 2 diffrent characters with the maximum and the second maximum times of occurrence. Then we pack the first occurrence of both characters into a subsequence, and remove such 2 occurrence.
It’s not hard to proof that k is maximized. If becomes empty then we can directly output the answer. Otherwise we need to merge the remaining letters into existed subsequences.
Obviously, only 1 kind of character will remain in . WLOG let a be such character.
stage 1
We enumerate from to . If we merge every remaining a before into the subsequence which belongs to.
It can be shown that, after this stage, only a trailing sequence of a(possibly empty) will remain. Every a before any character other than a will be removed.
stage 2
Let be the number of as in the subsequence which belongs to, on the left of . Specially, if is not matched with an a in stage 0, let .
Let be the length of the remaining a sequence.
If there exists an where we can simply append all the remaining a after .
Otherwise, if there exists more than satisfying :
Let be the first two such .
- If we can append the first remaining
aafter , and the rest after . - Otherwise we can move the first
awith to the belonging subsequence of . Now will have twoas before it, and nothing behind it, and will be alone. Then, we can directly append the remaininga(only ) after .
Otherwise, there will be no solution, as the string must satisfy one of the condition given above.
Thus, we solved this problem in time. Note that we need to sort the subsequence. Maybe careful implementation can reach linear complexity.
T4 show
Hint 1: solve Special Property D.
Hint 2: consider sqrt balancing.
It is not hard for one to notice, given , troupe in room exits if and only if , where is a constant which only depends on and . (Specially, let .)
Consider maintaining as we move from to .
Suppose we move to from . Only is affected, which becomes .
So the problem is translated into a form of 2D counting, because we only need to count , which can be done in time by storing all the queries in advance and using fenwick tree.
However maintaining is of wrong complexity, since the out degree of a vertex can be large.
Use the balancing trick. For all vertices with out degree greater than , we can maintain when updating ; for other vertices, we can simply enumerate through the edges. Choose , and we can complete this in time.