Thanks for participating in Codeforces Round 1122 (Div. 3)!
How many participants can possibly have solved all three problems?
At most $$$\min(a_1,a_2,a_3)$$$ participants could have solved all three problems. This is achievable by making the same $$$\min(a_1,a_2,a_3)$$$ participants solve every problem, then distributing the remaining solves arbitrarily.
Thus, the answer is $$$n-\min(a_1,a_2,a_3)$$$.
The complexity is $$$O(1)$$$ per test case.
Consider the cases $$$a\geq b$$$ and $$$a \lt b$$$ separately.
If Alice ever takes stones, she should take all of them.
If $$$a\geq b$$$, Alice takes all $$$c$$$ stones immediately. There is nothing left for Bob to use to reduce her lead, so the score is $$$a+c-b$$$.
Now suppose $$$a \lt b$$$. Alice can take $$$0$$$ stones, and Bob will also take $$$0$$$ because he is already ahead and taking more stones would only increase the score. This gives $$$b-a$$$. Otherwise, Alice should take all $$$c$$$ stones at once. Taking only part of them gives a difference between the two cases of taking none and taking all, so it cannot be better than both. Taking all gives $$$|a+c-b|$$$.
If $$$a+c \lt b$$$, this is smaller than $$$b-a$$$. Otherwise, it equals $$$a+c-b$$$. Therefore, the answer is $$$\max(|a-b|,a+c-b)$$$.
The complexity is $$$O(1)$$$ per test case.
If a prefix contains both $$$0$$$ and $$$1$$$, what are its bitwise AND and bitwise OR?
A sorted binary string is determined by one split point.
If a prefix contains both $$$0$$$ and $$$1$$$, its AND is $$$0$$$ and its OR is $$$1$$$, so its last character can be changed to either value.
If $$$s_1=1$$$, the first character can never change, so the final string must be all ones. Every zero can be changed using OR, giving the number of zeros as the answer.
Now suppose $$$s_1=0$$$. Fix the point after which the ones start. For a split after $$$i$$$, we need to change every one in $$$[1,i]$$$ and every zero in $$$[i+1,n]$$$. We can first change the required zeros on the right using OR, then change the required ones on the left using AND. A split inside the initial block of zeros is never better than moving it to the end of that block.
So, scan every split while maintaining the number of ones on the left and zeros on the right, and take the minimum.
The complexity is $$$O(n)$$$ per test case.
Look at how the height and position of each section change after an operation.
A section moving one position to the right gains $$$1$$$ height, while a section moving one position to the left loses $$$1$$$ height.
During an operation, every section passed over moves one position to the right and gains $$$1$$$ height. The section being moved goes $$$j-i$$$ positions to the left and loses exactly $$$j-i$$$ height. Thus, for every section, $$$\text{height}-\text{position}$$$ never changes. The section initially at position $$$i$$$ therefore always keeps the value $$$a_i-i$$$.
We can also reorder the sections however we want by building the desired order from left to right and repeatedly moving the required section into the next position.
Suppose $$$k$$$ consecutive positions starting at $$$p$$$ are flat at height $$$h$$$. Their invariant values are $$$h-p,h-p-1,\ldots,h-p-k+1$$$, which are $$$k$$$ distinct consecutive integers. The reverse is also true: if $$$k$$$ sections have consecutive values $$$a_i-i$$$, placing them in decreasing order makes their heights equal.
Therefore, the answer is the longest consecutive run among the distinct values $$$a_i-i$$$. Put them in a set and scan in increasing order.
The complexity is $$$O(n\log n)$$$ per test case.
Use DP. Let $$$dp_i$$$ be the minimum number of operations needed to turn one $$$i$$$ into elements which are all at most $$$k$$$.
Let $$$dp_i$$$ be the minimum number of operations needed to turn one $$$i$$$ into elements which are all at most $$$k$$$. Clearly, $$$dp_i=0$$$ for $$$i\leq k$$$.
For $$$i \gt k$$$, suppose we choose a prime divisor $$$p$$$ of $$$i$$$. One operation gives us $$$p$$$ copies of $$$i/p$$$, and these copies can then be handled independently. Thus, $$$dp_i=\min_{p\mid i,\ p\text{ prime}}(1+p\cdot dp_{i/p})$$$.
Since $$$i/p \lt i$$$, compute the DP in increasing order. We can precompute the distinct prime divisors of every number with a sieve. Finally, the original elements are independent, so the answer is $$$\sum dp_{a_i}$$$.
The complexity is $$$O(n\log\log n)$$$ per test case after preprocessing.
Can you solve the problem for all $$$f(1),f(2),\ldots,f(n)$$$?
This was actually the original version of the problem!
Work backwards from one copy of the value you want to create.
Process the values from large to small, and let $$$q$$$ be the number of copies of the current value that you need.
If you need $$$q$$$ copies of $$$x$$$ but only have $$$c \lt q$$$, the missing $$$q-c$$$ copies of $$$x$$$ each force another copy of every smaller value.
We check whether a value $$$M$$$ can appear, then binary search the largest valid $$$M$$$. The largest initial value is already present, so only larger values need to be checked.
Work backwards from one copy of $$$M$$$. At first we need one copy of every value below it, so set $$$q=1$$$ and process the values downwards. Suppose we currently need $$$q$$$ copies of $$$x \gt 0$$$, and there are $$$c$$$ copies of $$$x$$$ in the initial multiset. If $$$c\geq q$$$, we use $$$q$$$ of them and have $$$c-q$$$ extras. If $$$c \lt q$$$, the missing $$$q-c$$$ copies of $$$x$$$ each require another copy of every smaller value. Thus, $$$q$$$ becomes $$$q+(q-c)=2q-c$$$.
So the transition is $$$q\leftarrow q+\max(0,q-c)$$$. Extra positive elements cannot save us at another positive level, but they can all be turned into zeros by taking them alone. We count these extras, and when we reach $$$0$$$, the check succeeds exactly when the initial zeros plus all extras are at least $$$q$$$.
Missing values are easy to skip. If there are $$$d$$$ missing values in a row, then $$$q$$$ doubles $$$d$$$ times, so multiply it by $$$2^d$$$ at once. We can also cap $$$q$$$ once it becomes larger than the total number of elements to avoid overflow.
If $$$M$$$ is possible, then every smaller value is also possible, since all of them have to exist while building $$$M$$$. Therefore, binary search works. After sorting the pairs, one check is $$$O(n)$$$.
The complexity is $$$O(n\log n+n\log C)$$$ per test case, where $$$C$$$ is the binary search range.
Can you get a much smaller upper bound for the binary search?
Let $$$mx=\max x_i$$$.
You only need to search up to about $$$mx+50$$$. Once we go much further than $$$mx$$$, there are many consecutive missing values, and every missing value doubles $$$q$$$, so the required number of elements grows too quickly.
Thus, we can binary search only on $$$[mx,mx+50]$$$, giving $$$O(n\log 50)$$$ checks after sorting.
The exact proof and bound are left to the reader :)
Process the tree from the leaves upward.
A node can always be brought back to its original value after changing it.
For every node $$$i$$$, its reachable values have the form $$$(a_i+g_ix)\bmod b_i$$$ for some integer $$$x$$$. Find $$$g_i$$$.
For every node $$$u$$$, let $$$g_u$$$ be such that its reachable values are exactly $$$(a_u+g_ux)\bmod b_u$$$. We compute $$$g_u$$$ from the leaves upward.
Suppose $$$v$$$ is a child of $$$u$$$. If $$$g_v=b_v$$$, then $$$v$$$ is fixed at $$$a_v$$$. Otherwise, its value can change from $$$a_v$$$ by multiples of $$$g_v$$$. Let $$$S_u$$$ be the sum of the initial values of all direct children of $$$u$$$. We can add $$$S_u$$$ to $$$u$$$ by leaving all children at their initial values, and every non-fixed child $$$v$$$ lets us vary this amount by multiples of $$$g_v$$$. Therefore, $$$g_u=\gcd(b_u,S_u,g_v\text{ for all non-fixed children }v)$$$.
For a leaf, this gives $$$g_u=b_u$$$, as expected. Once we know $$$g_u$$$, the largest reachable value is $$$a_u+\left\lfloor\frac{b_u-1-a_u}{g_u}\right\rfloor g_u$$$.
These maxima can all be achieved together. We can first set a node to its maximum, then continue working strictly inside its children's subtrees without changing that node. So after computing all $$$g_u$$$ bottom-up, we simply sum the maximum reachable value of every node.
The complexity is $$$O(n\log 10^9)$$$ per test case.
We only need to keep one occurrence of every value $$$1,2,\ldots,n$$$. What can we do with every other occurrence?
Let $$$t_i$$$ be the arrival position of the occurrence of $$$i$$$ which stays in the final deque. When can $$$i$$$ be inserted without a malfunction?
An element can be inserted normally exactly when $$$t_i$$$ is a prefix minimum or a suffix minimum of $$$t_1,t_2,\ldots,t_n$$$.
Fix the position of the global minimum. The prefix minima and suffix minima can be optimized separately.
If some value from $$$1$$$ to $$$n$$$ never appears, the answer is $$$-1$$$. Otherwise, choose one occurrence of each value to keep. Every unused occurrence can simply be pushed to an end and immediately removed from that same end.
Let $$$t_i$$$ be the position of the chosen occurrence of $$$i$$$. We can push $$$i$$$ to the front normally exactly when every chosen value which arrived earlier is larger than $$$i$$$, or $$$t_i \lt \min(t_1,\ldots,t_{i-1})$$$. Similarly, we can push it to the back exactly when $$$t_i \lt \min(t_{i+1},\ldots,t_n)$$$. Thus, a value avoids a malfunction exactly when $$$t_i$$$ is a prefix minimum or a suffix minimum. The global minimum is counted in both, so if there are $$$L$$$ prefix minima and $$$R$$$ suffix minima, we keep $$$L+R-1$$$ values for free.
Now optimize the prefix minima. Process the values $$$1,2,\ldots,n$$$. Let $$$dp[p]$$$ be the maximum number of prefix minima so far when the smallest chosen arrival position is $$$p$$$. For an occurrence at position $$$t$$$, making it the new minimum gives $$$new[t]=1+\max_{p \gt t}dp[p]$$$. All of these transitions must be calculated before adding the states for the current value.
An old state with minimum $$$p$$$ can stay unchanged only if the current value has some occurrence after $$$p$$$. If $$$last_i$$$ is its last occurrence, every state with $$$p \gt last_i$$$ must therefore be deleted. For value $$$1$$$, every occurrence simply starts with value $$$1$$$.
After all values are processed, let $$$L[p]$$$ be the best number of prefix minima whose global minimum is at position $$$p$$$. Run the same DP with the values in reverse order to get $$$R[p]$$$ for suffix minima. For a fixed $$$p$$$, the two sides can be combined independently, sharing only the global minimum itself. Therefore, the maximum number of values which avoid a malfunction is $$$\max_p(L[p]+R[p]-1)$$$, and the answer is $$$n-\max_p(L[p]+R[p]-1)$$$.
Store the DP in a segment tree. For an occurrence $$$t$$$, the transition needs the maximum on $$$(t,m]$$$. After calculating all transitions for the current value, clear the suffix after $$$last_i$$$ and insert the new states.
A lazy segment tree supports the suffix clear and range maximum queries in $$$O(\log m)$$$, so the total complexity is $$$O(m\log m)$$$.
We can also use a normal segment tree together with a set of active positions. Whenever a state with position greater than $$$last_i$$$ becomes invalid, remove the largest such position from the set and set its segment tree value to $$$0$$$.
Each position is inserted once and deleted at most once in one pass, so there are only $$$O(m)$$$ deletions in total. The total complexity is again $$$O(m\log m)$$$.








Update the announcement :3
all the code sections are empty. are you waiting until after the hacking, or is this a mistake?
Sigma round, loved so much. But not C >:(
For H, instead of a segtree, we can use a BIT for easier implementation: https://codeforces.me/contest/2266/submission/391534225 Finding maximum over (t,m] is the same as finding maximum on (0,m-t]
nice!
had a ton of fun solving F :D, my bound for the binary search was [0,n+60] which makes it a bit easier to implement than having it bound by mx, and it's pretty easy to prove. was scared I didn't handle the overflow well but I guess it was just 1 if lol
I can’t remember the last time a Div 3 contest rekt me this hard.
After looking at the editorial, I actually like this contest.
Also, my post contest discussion stream here. ABCEDFG is the order.
D and E had me STRESSED. Glad I succeeded in solving them, and props to you WorldWarV for this contest! The problems I managed to solve had very elegant solutions, which I appreciate.
I solved C using dp
Submission
I solved E after the contest. I didn't use DP I brute-forced the solution for each number in the array by factorizing it, and then found the formula for how many operations it will cost if I want to remove factor x from ai