跳到正文

题解

unofficial English tutorial of KDOI round 10

Moeebius约 16 分钟
原文
文章信息与版本

文章信息

发布
2024年10月14日
收录
2025年12月12日
更新
2026年10月21日
篇幅
约 16 分钟
获赞
5

版本

  1. v1当前

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, kk, won’t exceed 5, since 10k>k⋅10510^k > k\cdot 10^5 for all k>5k>5.

Consider dynamic programming. Let fi,jf_{i,j} be the minimum cost to delete all the last ii digits, while jj digits are deleted using operation 2.

Let cic_i be the ii-th digit of nn, counting from the least significant digit. Now we have the following transition:

  • fi,j←fi−1,j+vcif_{i,j} \gets f_{i-1,j} + v_{c_i} (operation 1)
  • fi,j←fi−1,j−1+ci⋅10j−1f_{i,j} \gets f_{i-1,j-1} + c_i \cdot 10^{j-1} (operation 2)

Thus, we can solve this problem in O(klog⁡n)O(k\log n) time, where k=5k=5 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 ai′a'_i be the temperature of vertex ii after we use up operation 1.

The answer is yes (i.e. Huoyu) if and only if ∀j∈soni,aj′≤ai′\forall j \in \text{son}_i, a'_j \le a'_i, and a1′≤0a'_1 \le 0.

To proof it, consider the root. Since we can only increase the temperature using operation 2, aroot′≤0a'_{root} \le 0 should hold. Then we add −aroot-a_{root} to the root, and for every son jj, there should be aj′≤aroot′a'_j \le a'_{root}, or aj′a'_j will remain positive.

Now the temperature of root is 00, so we can simply remove it and check the remain forest. The above observation can then be proved by induction. □\square

Let xix_i be the number of operation 1 done on vertex ii. (If ii isn’t a leaf, we can simply let xi=∑j∈sonixjx_i = \sum\limits_{j\in \text{son}_i} x_j.)

We need to check whether we can managed to satisfy the following inequalities:

∀j∈sonj,ai−xi≥aj−xjx1≥a1\forall j \in \text{son}_j, a_i - x_i \ge a_j - x_j \\ x_1 \ge a_1

However, it seems impossible to maintain the information, since aia_i and xix_i can be extremely large. So we need observation 3:

Observation 3

for all vertex ii, there exists an interval [li,ri][l_i, r_i] (possibly empty), such that xi=px_i = p is valid (with all the constraints inside subtree ii)   ⟺  p∈[li,ri]\iff p \in [l_i,r_i].

The proof also needs induction.

Firstly, lil_i is easy to compute: li=max⁡{∑j∈sonilj,ai}l_i = \max\{\sum\limits_{j \in \text{son}_i}l_j, a_i\}. Suppose we know rir_i, 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 xi=p (p>li)x_i=p\ (p>l_i) is valid. Now we need to proof xi=p−1x_i=p-1 is also valid.

There must exist a j∈sonij \in \text{son}_i whose xj>lix_j > l_i, or p>lip > l_i is violated. Then we decrease xjx_j by 11.

For jj, ai−xi≥aj−xja_i - x_i \ge a_j - x_j still holds, since we decrease 11 at both sides of the inequality; and for kk other than jj, the left hand side increases by 11 while the right hand side remains the same.

Thus, xi=p−1x_i=p-1 is still valid. □\square

One corner case is that ∣soni∣=1|\text{son}_i|=1, then we simply need to inherit [l,r][l,r] from its son.

Given observation 3, a fan of Um_nik should immediately come up with binary search.

In detail, we can first compute lil_i, then use binary search to compute rir_i. We only need to check whether xi=px_i = p is valid.

Enumerate jj through every son vertex of ii. Since ai−xi≥aj−xja_i - x_i \ge a_j - x_j we have xj≤ai−aj+xjx_j \le a_i-a_j+x_j. So xj∈[li,min⁡{ri,ai−aj+xj}]x_j \in [l_i, \min\{r_i,a_i-a_j+x_j\}]. If this interval is empty then pp is obviously invalid.

Otherwise, we can compute bound of xix_i by summing up all these intervals, and pp is valid iff it falls into such bound.

Thus, we can solve the problem in O(nlog⁡V)O(n \log V) 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. Σ=2\Sigma=2.

This is a deterministic approach, which needs some case work.

Consider when the answer is no (i.e. Shuiniao).

  • If ss only contains one kind of character; Or
  • If ss only contains two kinds of character, and the second kind only occurs at the middle of string (i.e n is odd and only s(n+1)/2s_{(n+1)/2} 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 kk. After examining anti2.ans one should notice almost every subsequence has length 22. 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 ss 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 ss. WLOG let a be such character.

stage 1

We enumerate from 11 to nn. If si≠as_i \neq \mathtt a we merge every remaining a before ii into the subsequence which ii 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 lil_i be the number of as in the subsequence which ii belongs to, on the left of ii. Specially, if ii is not matched with an a in stage 0, let li=0l_i=0.

Let dd be the length of the remaining a sequence.

If there exists an ii where si≠a∧li≠ds_i \neq \mathtt{a} \land l_i \neq d we can simply append all the remaining a after ii.

Otherwise, if there exists more than 11 ii satisfying si≠as_i \neq \mathtt a:

Let p,qp,q be the first two such ii.

  • If d>1d>1 we can append the first remaining a after pp, and the rest after qq.
  • Otherwise we can move the first a with pp to the belonging subsequence of qq. Now qq will have two as before it, and nothing behind it, and pp will be alone. Then, we can directly append the remaining a(only 11) after pp.

Otherwise, there will be no solution, as the string must satisfy one of the condition given above.

Thus, we solved this problem in O(nlog⁡n)O(n \log n) 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 rr, troupe in room pp exits if and only if l≤qpl \le q_p, where qpq_p is a constant which only depends on rr and pp. (Specially, let q1=rq_1=r.)

Consider maintaining qpq_p as we move rr from 11 to kk.

Suppose we move to ii from i−1i-1. Only qaiq_{a_i} is affected, which becomes max⁡v∈outai{qv}\max\limits_{v \in \text{out}_{a_i}}\{q_v\}.

So the problem is translated into a form of 2D counting, because we only need to count ∑i[qi≥l]\sum_{i} [q_i \ge l], which can be done in O((n+q)log⁡n)O((n+q) \log n) time by storing all the queries in advance and using fenwick tree.

However maintaining qpq_p is of wrong complexity, since the out degree of a vertex can be large.

Use the balancing trick. For all vertices xx with out degree greater than BB, we can maintain qxq_x when updating qaiq_{a_i}; for other vertices, we can simply enumerate through the edges. Choose B=nB=\sqrt n, and we can complete this in O(nn)O(n \sqrt n) time.

原文
存档评论1 条抓取于