Thank you for participating in this round! Problems I are authored by orzdevinwang and the rest by me.
Sorry for the weak pretest in problem B.
hints and code are WIP.
What is the parity of $$$s$$$ after an operation?
After the first operation, $$$s$$$ is always odd. You can only earn points when $$$a_i$$$ is odd after this operation. However, during the first operation, you can only earn a point if $$$a_i$$$ is even.
Let $$$c_0$$$ and $$$c_1$$$ be the number of even and odd numbers, respectively.
If there is at least one even number, you can place it first, making the answer $$$c_1 + 1$$$. Otherwise, since no points can be earned during the first operation, the answer is $$$c_1 - 1$$$.
Time complexity: $$$O(n)$$$.
Let $$$a$$$ and $$$b$$$ be the lengths of the two bases, and let $$$c$$$ be the length of the two legs. A necessary and sufficient condition for forming an isosceles trapezoid with a positive area is $$$|a - b| < 2c$$$, as the longest edge must be shorter than the sum of the other three edges.
To determine whether an isosceles trapezoid can be formed, consider the following cases:
- If there are two distinct pairs of identical numbers that do not overlap, these pairs can always form an isosceles trapezoid.
- If there are no pairs of identical numbers, it is impossible to form an isosceles trapezoid.
- If there is exactly one pair of identical numbers, denoted by $$$c$$$, we will use them as the legs. Remove this pair and check whether there exist two other numbers whose difference is less than $$$2c$$$. This can be efficiently done by sorting the remaining numbers and checking adjacent pairs. \end{itemize}
The time complexity of this approach is $$$O(n \log n)$$$.
For the $$$i$$$-th person, there are at most two possible cases:
- If they are honest, there are exactly $$$a_i$$$ liars to their left.
- If they are a liar, since two liars cannot stand next to each other, the $$$(i-1)$$$-th person must be honest. In this case, there are $$$a_{i-1} + 1$$$ liars to their left. \end{itemize}
Let $$$dp_i$$$ represent the number of possible game configurations for the first $$$i$$$ people, where the $$$i$$$-th person is honest. The transitions are as follows:
- If the $$$(i-1)$$$-th person is honest, check if $$$a_i = a_{i-1}$$$. If true, add $$$dp_{i-1}$$$ to $$$dp_i$$$.
- If the $$$(i-1)$$$-th person is a liar, check if $$$a_i = a_{i-2} + 1$$$. If true, add $$$dp_{i-2}$$$ to $$$dp_i$$$. \end{itemize}
The final answer is given by $$$dp_n + dp_{n-1}$$$.
Time complexity: $$$O(n)$$$.
It can be hard to determine how to merge small numbers into a larger number directly. Therefore, we approach the problem in reverse.
Instead of merging numbers, we consider transforming the number $$$b$$$ back into $$$a$$$. For each number $$$x$$$, it can only be formed by merging $$$\lceil \frac{x}{2} \rceil$$$ and $$$\lfloor \frac{x}{2} \rfloor$$$. Thus, the reversed operation is as follows:
\begin{itemize} \item Select an integer $$$x$$$ from $$$b$$$ and split it into $$$\lceil \frac{x}{2} \rceil$$$ and $$$\lfloor \frac{x}{2} \rfloor$$$. \end{itemize}
We can perform this splitting operation on the numbers in $$$b$$$ exactly $$$n - m$$$ times. If the largest number in $$$b$$$ also appears in $$$a$$$, we can remove it from both $$$a$$$ and $$$b$$$ simultaneously. Otherwise, the largest number in $$$b$$$ must be split into two smaller numbers.
To efficiently manage the numbers in $$$a$$$ and $$$b$$$, we can use a priority queue or a multiset.
The time complexity of this approach is $$$O((n + m) \log (n + m))$$$.
For a fixed $$$p$$$, let $$$f(S)$$$ represent the number obtained after applying the operations in set $$$S$$$ to $$$p$$$. Define $$$g(i)$$$ as the minimum value of $$$f(S)$$$ for all subsets $$$S$$$ with $$$|S| = i$$$, i.e., $$$g(i) = \min_{|S| = i} f(S)$$$.
Lemma: The function $$$g$$$ is convex, that is for all $$$i$$$ ($$$1 \leq i < m$$$), the inequality $$$2g(i) \leq g(i-1) + g(i+1)$$$ holds.
Proof: Let $$$f(S)$$$ be the value corresponding to the minimum of $$$g(i-1)$$$, and let $$$f(T)$$$ correspond to the minimum of $$$g(i+1)$$$. Suppose the $$$w$$$-th bit is the highest bit where $$$f(S)$$$ and $$$f(T)$$$ differ.
We can always find an operation $$$y \in T \setminus S$$$ that turns the $$$w$$$-th bit in $$$g(i-1)$$$ into $$$0$$$. In this case, we have:
Moreover, since the highest bit where $$$f(S \cup {y})$$$ and $$$g(i+1)$$$ differ is no greater than $$$w-1$$$, we have:
Combining these inequalities gives:
proving the convexity of $g$.
For each number $$$a_i$$$, we can use brute force to compute the minimum value obtained after applying $$$j$$$ operations, denoted as $$$num(i, j)$$$. From the lemma, we know that the difference $$$num(i, k) - num(i, k - 1)$$$ is non-increasing.
Thus, when performing operations on this number, the reductions in value are:
You need to perform $$$k$$$ operations in total. To minimize the result, you can choose the $$$k$$$ largest reductions from all possible operations. This can be done by sorting all operations by their cost or by using std::nth_element.
Time complexity: $O(n2^m + nm)$。