Author: TheScrasse
Preparation: TheScrasse
There are two types of subarrays.
If there exists $$$a_i \geq a_{i+1}$$$, how many times can the subarray appear?
- If there exists an $$$i$$$ such that $$$a_i \geq a_{i+1}$$$, we have $$$a_i = k$$$ for some $$$k$$$, $$$a_{i+1} = 1$$$. The subarray $$$[k, 1]$$$ appears only once on the blackboard (when James writes $$$1, \ldots, k$$$ and then $$$1, \ldots, k+1$$$). Since $$$a_1, a_2, \ldots, a_m$$$ contains $$$[k, 1]$$$, it must appear at most once as well, but the statement guarantees that it appears at least once, so the answer is $$$1$$$.
- Otherwise, the array $$$a_1, a_2, \ldots, a_m$$$ can be rewritten as $$$l, l+1, \ldots, r$$$, and it appears when James writes $$$1, \ldots, r$$$, then when James writes $$$1, \ldots, r+1$$$, ..., then when James writes $$$1, \ldots, n$$$ (so $$$n-r+1$$$ times in total).
Complexity: $$$O(n)$$$
Author: TheScrasse
Preparation: TheScrasse
Suppose you have already calculated where person $$$i-1$$$ goes. Can you calculate fast where person $$$i$$$ goes?
The first $$$i-2$$$ cells visited by people $$$i-1$$$ and $$$i$$$ coincide.
Person $$$i$$$ performs the same commands as person $$$i-1$$$, with the addition of the $$$i$$$-th command.
Now we wonder whether performing the same $$$i-1$$$ commands means that the first $$$i-1$$$ cells visited are the same. This is actually false in general, because after person $$$i-1$$$ performs his commands, he colors a new cell black, and this can change the outcome of the $$$(i-1)$$$-th operation. However, the outcome of the first $$$i-2$$$ commands remains the same, because the new black cell is too far from the positions visited in the first $$$i-2$$$ commands.
So the position visited by person $$$i$$$ after $$$i-2$$$ commands is the same position visited by person $$$i-1$$$ after $$$i-2$$$ commands, and it's enough to simulate the last two commands naively.
Complexity: $$$O(n \log n)$$$ or $$$O(n)$$$
Author: TheScrasse
Preparation: TheScrasse
For a fixed $$$k$$$, find a greedy strategy.
You can calculate the answer with prefix sums.
Consider the following strategy:
- Iterate over events in increasing order.
- In the first $$$k$$$ events, people enter the museum. In the last $$$k$$$ events, people exit. For the other events, if the current number of people is $$$k$$$ a person exits, otherwise a person enters.
This strategy is optimal, because it maximizes the number of people present at any time in the museum.
- After the $$$i$$$-th event with $$$i \leq k$$$, at most $$$i$$$ people can be inside the museum, and in fact we have exactly $$$i$$$.
- A similar argument works for the last $$$k$$$ events (you have to use that at the end the museum is empty).
- For the other events, after the $$$i$$$-th event we know that the parity of the number of people is equal to the parity of $$$i$$$. In fact, the number of people is either $$$k$$$ or $$$k-1$$$, which is the maximum possible number of people $$$\leq k$$$ with the correct parity.
Now we have to calculate the following terms:
- $$$1 \cdot a_1 + 2 \cdot a_2 + \ldots + k \cdot a_k$$$: can be done with prefix sums of $$$i \cdot a_i$$$.
- $$$k \cdot a_{2n-k+1} + (k-1) \cdot a_{2n-k+2} + \ldots + 1 \cdot a_{2n}$$$: can be done with prefix sums of $$$(2n-i+1) \cdot a_i$$$:
- $$$k \cdot (a_{k+2} + a_{k+4} + \ldots + a_{2n-k})$$$, $$$k \cdot (a_{k+2} + a_{k+4} + \ldots + a_{2n-k-1})$$$ and similar: can be done with prefix sums of $$$a_{2i}$$$, and prefix sums of $$$a_{2i+1}$$$.
Alternatively, we can calculate the answer for some $$$k$$$ fast using the answer for $$$k-2$$$ and changing a few terms.
Complexity: $$$O(n)$$$
Author: TheScrasse
Preparation: Dominater069
Let's ignore the first condition for now.
Some cells are necessarily black.
Then, some cells are necessarily white.
Now what happens with $$$k = 2$$$? You can conclude that some cells in column $$$2$$$ are necessarily white.
Repeat with $$$k = 3$$$, etc.
Once you find all the grids which satisfy the second and the third conditions, iterate over rows in decreasing order.
- $$$(1, 1)$$$ is black, because of the second condition with $$$k = 1$$$.
- Then, $$$(i, 1)$$$ is white for $$$i \geq 2$$$, because of the third condition with $$$k = n$$$.
- Then, either $$$(1, 2)$$$ or $$$(2, 2)$$$ is black, because of the second condition with $$$k = 2$$$.
- Then, $$$(i, 2)$$$ is white for $$$i \geq 3$$$, because of the third condition with $$$k = n-1$$$.
- $$$\ldots$$$
In general, only $$$(i, j)$$$ with $$$i \leq j$$$ can be black. If you repeat the same process starting from $$$(1, n)$$$, you can conclude that only $$$(i, j)$$$ with $$$i \leq n-j+1$$$ can be black.
So, for each column $$$j$$$, there is exactly one black cell $$$(i, j)$$$, with $$$i \leq \min(j, n-j+1)$$$.
Now you can iterate over rows in decreasing order. For each row, you have some number $$$c_i$$$ of available columns, and $$$a_i$$$ columns you must fill, and this can be done in $$$\binom{c_i}{a_i}$$$ ways. You can calculate $$$c_i$$$ from $$$c_{i+1}$$$ by calculating the number of new available columns, and subtracting the $$$a_{i+1}$$$ columns you must fill.
Complexity: $$$O(n \log n)$$$ or $$$O(n)$$$
Author: TheScrasse
Preparation: TheScrasse
Try to come up with a condition for when Alice can choose some subset of items.
Write a dynamic programming $$$O(n^2)$$$ solution based on the condition.
Optimize the solution with data structures like segment trees.
We can assume $$$a_i = i$$$ for all $$$i$$$. Now, if Alice wants to take some subset $$$S$$$, we have the following condition:
- For all $$$x \notin S$$$, $$$y \in S$$$, if $$$x \lt y$$$, then $$$pos_x \lt pos_y$$$, where $$$pos = b^{-1}$$$
Proof: If $$$pos_x \gt pos_y$$$, the item $$$y$$$ cannot be taken before $$$x$$$ because $$$x$$$ comes first in Alice's priority order. And item $$$x$$$ cannot be taken before $$$y$$$ because $$$y$$$ comes first in Bob's priority order.
Now, if the condition is satisfied, infact all subsets are valid. A simple algorithm works, just make Alice take all items in $$$S$$$ in increasing order, and when Alice wants to take some item but it is not the first remaining item according to her priority order, Bob takes items till it becomes first in her priority order.
Now, we can write our dynamic programming solution: $$$dp_{(i, j)} =$$$ maximum sum of items taken by Alice within the first $$$i$$$ items such that $$$max(pos_x)$$$ among Bob's items is $$$j$$$.
Transitions are:
- We are allowed to let Alice take item $$$i + 1$$$ if and only if $$$pos_i \gt j$$$.
- Bob can take item $$$i + 1$$$ without restrictions, and $$$j$$$ gets updated to $$$\max(j, pos_{i + 1})$$$
We then optimize this with storing a segment tree over the states $$$j$$$. The first type of transition is a range add query, the second type of transition can be split into $$$j \lt pos_{i + 1}$$$ and $$$j \gt pos_{i + 1}$$$, in the first case we do nothing, and in the second case we need a range max query.
All of the above operations can be handled with a lazy segment tree. The time complexity is $$$O(n \cdot log(n))$$$.
Author: Dominater069
Preparation: Dominater069
Find a characterization for when a position array $$$p$$$ is possible. It helps to think in terms of $$$f_x =$$$ the number of people at coordinate $$$x$$$ instead.
Essentially, an attraction at $$$x$$$ combines the people standing at $$$x - 1, x$$$ and $$$x + 1$$$. Each update unites $$$3$$$ components into one, except for updates at the edge.
Read the hints. We are trying to find a characterization for valid sequences $$$p$$$.
We can prove the following necessary conditions easily by considering what happens when an attraction is placed in different positions,
- There exists $$$1 \le L \le R \le n$$$ such that $$$f_i \ge 1$$$ if and only if $$$L \le i \le R$$$.
- $$$f_i$$$ is odd for all $$$L \lt i \lt R$$$.
Further, these conditions are sufficient, and a simple construction is to do $$$\frac{f_i}{2}$$$ attractions at the position of the centre of the group of people finally at $$$i$$$ (The edges are handled easily). Also, you may have to do some translations at the end, which can be done by attracting at $$$1$$$ or $$$n$$$.
Try to fix $$$L$$$ and $$$R - L + 1$$$, and then calculate the sum under these restrictions.
The middle $$$R - L - 1$$$ variables have to be odd, and the outer $$$2$$$ variables can have any parity. Fix the parity of the outer elements.
Use expected value symmetry arguments to calculate the sum fast.
Assume $$$L = 1$$$ and $$$R = K$$$ ($$$K \gt 1$$$) for now. We have the equation $$$f_1 + f_2 + f_3 + \ldots + f_k = n$$$, and $$$f_i \ge 1$$$ and $$$f_i$$$ odd for $$$i \ne 1$$$ and $$$i \ne k$$$, where $$$f_i =$$$ the number of people finally at $$$i$$$. We want $$$\sum f_i \cdot a_i$$$ over all ways.
Fix parity of $$$f_1, f_k$$$ say $$$x$$$ and $$$y$$$ (let $$$1 \le x, y \le 2$$$), and then let
- $$$f_i = 2 \cdot g_i + 1$$$ for all $$$i \ne 1, i \ne k$$$
- $$$f_1 = 2 \cdot g_1 + x$$$
- $$$f_k = 2 \cdot g_k + y$$$
Let $$$S = n - x - y - (k - 2)$$$. Now, the equation becomes $$$\sum g_i = \frac{S}{2}$$$, and we want $$$\sum 2 \cdot g_i \cdot a_i$$$ over all ways ($$$+$$$ a few more terms arising from the constants $$$1$$$, $$$x$$$ and $$$y$$$).
If $$$S$$$ is odd, no solution. Assume $$$S$$$ even.
Since all $$$g_i$$$ variables are symmetric, we can just say the expected value of $$$g_i$$$ is $$$\frac{S}{2 \cdot k}$$$, and then calculate $$$\sum ways \cdot EV(g_i) \cdot a_i$$$, where $$$ways$$$ is the number of solutions (simple stars and bars).
To solve the full problem, note that it essentially requires you to optimize finding sum of subarrays of size of length $$$k$$$, and this can be done (cleanly) with prefix sums on prefix sums. Also, you need to handle $$$L = R$$$ separately. The final solution is $$$O(n)$$$.
2150E1 - Hidden Single (Version 1)
Author: TheScrasse
Preparation: TheScrasse, Dominater069
Assume all elements appear twice, and look for a contradiction.
Split the array into two.
Determine whether each value appear on the left, on the right, or in both.
Continue recursively.
Write a recursive function solve(tl, tr, vals, waste), which means that
- you are looking for the special value in the interval
[tl, tr]; - the vector
valscontains the values which only appear in[tl, tr]; - the vector
wastecontains the value which appear both in[tl, tr]and outside it.
At the beginning, call solve(1, 2n - 1, {1, 2, ..., n}, {}). In every call,
- let
tm = (tl + tr) / 2, and split the interval[tl, tr]into two intervals[tl, tm]and[tm+1, tr]; - for each value in
wastedetermine the sub-interval containing it (spending $$$1$$$ query); - for each value in
valsdetermine the sub-intervals containing it (spending at most $$$2$$$ queries).
Now pretend every element appears twice, and calculate the length of [tl, tm] and [tm+1, tr] according to the values they contain. One of the lengths turns out to be wrong, because the interval contains a single element which was counted twice, so you can recurse there.
You visit at most $$$\lceil \log_2 n \rceil$$$ intervals. The total length of the visited intervals is at most $$$4n + \lceil \log_2 n \rceil$$$ (the starting interval has length $$$2n-1$$$ and is halved every time).
For an interval of length $$$l$$$, you ask at most $$$l+1$$$ queries. So you ask at most $$$4n + \lceil 2 \log_2 n \rceil$$$ queries in total.
2150E2 - Hidden Single (Version 2)
Author: TheScrasse
Preparation: TheScrasse, Dominater069
For a single value, if it appears twice, how many queries can you spend on average to detect that?
Combine a randomized solution with the solution to 2150E1 - Hidden Single (Version 1).
After the first layer of recursion in the solution to 2150E1 - Hidden Single (Version 1), you have spent around $$$7n/4$$$ queries, and there are around $$$n/4$$$ candidate values which can be the special value.
Consider the following procedure to approximately halve the number of candidates:
- split the array randomly into two halves (subsets);
- for each candidate, determine whether it appears in both halves.
With probability around $$$1/2$$$, the candidate appears in both halves and you use $$$2$$$ queries to detect it. Otherwise, you spend $$$3/2$$$ queries on average. So the average number of queries is approximately the solution to $$$x = 2/2 + (x+3/2)/2$$$, which is $$$7/2$$$. So you spend around $$$21n/8$$$ queries in total. Because of variance, you might end up asking significantly more queries, but with high probability you ask at most $$$900$$$ queries.
Bonus: you can further reduce the number of queries using some minor optimizations. For example, instead of splitting randomly, you can shuffle the array randomly at the beginning, and binary search every candidate. This is better because:
- there is an upper bound on the number of queries you make for each candidate;
- you ask queries on smaller subsets, so the probability of a candidate appearing twice in the same subset is slightly lower;
- you can break earlier if you find the special value.
The model solution based on this approach takes only $$$863$$$ queries on worst case with $$$10^4$$$ tests of size $$$n = 300$$$.
Author: TheScrasse
Preparation: TheScrasse, Dominater069
Use $$$k = 3$$$ for the first operation. Now, you have edges between all vertices at distance $$$1$$$ and $$$2$$$.
Use $$$\frac{D}{2} + 1$$$ for your second operation, where $$$D$$$ is the diameter.
Assume the graph is a tree, otherwise take an arbitrary spanning tree. First operation is $$$k = 3$$$, because that adds edges between all vertices at distance $$$2$$$, and we already have edges of vertices at distance $$$1$$$.
The second operation needs to be at least $$$\frac{D}{2} + 1$$$, where $$$D$$$ is diameter, because otherwise it is impossible to construct a path between the farthest nodes. And infact, it is possible to construct paths of length exactly $$$\frac{D}{2} + 1$$$ between all pairs of vertices, so we choose second operation as this.
The construction is as follows, suppose we want a path between nodes $$$u$$$ and $$$v$$$ (there are simpler constructions, the editorialist describes his). Let $$$c_x$$$ denote the closest node in the diameter to node $$$x$$$.
- $$$dist(u, v) \ge k$$$ : Just take a direct path between $$$u$$$ and $$$v$$$, and skip appropriate number of vertices.
- $$$c_u \ne c_v$$$ : First construct a path from $$$u$$$ to $$$c_u$$$ and similarly, $$$v$$$ to $$$c_v$$$. Now, we need to waste some operations because $$$dist(u, v) \lt k$$$. Assume the diameter is $$$1, 2, \ldots, D$$$ WLOG. Then, we can do the following operation: $$$(c_u, c_u - 2, c_u - 4, c_u - 3, c_u - 1, c_u + 1)$$$ and we successfully wasted $$$4$$$ steps.
This can be easily generalized to all lengths. Because $$$dist(u, v) \lt k$$$ implies $$$dist(c_u, c_v) \lt k$$$, and thus, $$$dist(1, c_u) + dist(c_v, D) \ge k$$$, so it is always possible to waste sufficient number of steps on the paths $$$1$$$ to $$$c_u$$$ and $$$c_v$$$ to $$$D$$$.
- $$$c_u = c_v$$$ : Root the tree at $$$c_u$$$. Assume that $$$u$$$ is an ancestor of $$$v$$$ (if it is not, make it an ancestor by jumping to parent). Either $$$dist(1, u) \ge k$$$ or $$$dist(D, u) \ge k$$$ [due to property of diameter, $$$k = \frac{D}{2} + 1$$$]. Then we can waste the sufficient number of steps on the path from $$$1$$$ to $$$u$$$, or $$$L$$$ to $$$u$$$ whichever one is larger. ($$$1$$$ and $$$D$$$ are diameter endpoints).
The entire solution runs in $$$O(n^3)$$$.
2150G - Counting Is Fun: The Finale
Author: satyam343
Preparation: satyam343
Let's first solve the following subproblem: given $$$x, y, k,$$$ how can we find the number of binary strings with Longest Nondecreasing Subsequence $$$\geq k$$$?
The answer is if $$$max(x,y)\geq k$$$, the answer is $$$\binom{x+y}{x}$$$. Otherwise, it is $$$\binom{x+y}{k}$$$. The proof for the first case is obvious. In the second case, we can approach it by bijecting it to paths on a grid. We start at the point $$$(0,0)$$$ and we want to get to the point $$$(x,y)$$$ through going East ($$$+1$$$ on $$$x$$$-coordinate) or North ($$$+1$$$ on $$$y$$$-coordinate). We claim that any path $$$p_0=(0,0), p_1,p_2,\ldots,p_{x+y}=(x,y)$$$, where there exists an index $$$i$$$ such that $$$(p_i).x-(p_i).y=y-k$$$ can be bijected to a good binary string. To prove that this is sufficient, note that by taking every occurrence of North on our path in the future, we have a subsequence of size $$$k$$$. To prove that this is necessary, note that if the longest nondecreasing subsequence in the binary string is $$$a$$$ $$$0$$$'s followed by $$$b$$$ $$$1$$$'s, then we can map the point right after $$$a$$$ $$$0$$$'s to the index $$$i$$$. Now, why is this mapping helpful? We can use the reflection technique to count the number of good paths. Let $$$i$$$ be the first occurrence where $$$(p_i).x-(p_i).y=y-k$$$. Now, reflect the remaining part of the path across the line $$$y-x=y-k$$$. Now we have mapped the number of good paths to the number of paths that arrive to the point $$$(x+y-k, k)$$$. This is similar to the proof of the closed form of Catalan numbers. Let this function be $$$f(x,y,k)$$$.
Now, let's solve the above problem again, but we are given a prefix of the binary string. Let $$$Z$$$ be the number of zeroes, $$$L$$$ be the length of the longest nondecreasing subsequence, $$$x$$$ and $$$y$$$ be the number of zeroes and ones to concatenate, and let $$$k$$$ be the target length of longest nondecreasing subsequence. I claim that the number of ways to finish the binary string is $$$\binom{x+y}{x}$$$ if $$$L+y \geq k$$$ or $$$Z+x \geq k$$$, $$$0$$$ if $$$Z+x+y \lt k$$$, and $$$\binom{x+y}{k-Z}$$$ otherwise. The first two cases are relatively simple to show. We can show the last case because the longest nondecreasing subsequence of the entire string must compose entirely of $$$0$$$'s in the prefix (or else $$$L+y\geq k$$$ would be true), so we can simplify this problem to the above problem as we only need a longest nondecreasing subsequence of $$$k-Z$$$ now. Let this function be $$$g(x,y,k,Z)$$$.
Now, to solve the problem, let's enumerate the length of the common prefix of $$$s$$$ and $$$t$$$, and then the index where the first half first has a longest nondecreasing subsequence length of exactly $$$k$$$ (call this index $$$i$$$). If this point is within the prefix, then we can complete the second half of the sequence directly using the formula we obtained above. There are two cases: either $$$t_i=0$$$ or $$$t_i=1$$$. If $$$t_i=0$$$ and $$$i$$$ is the first index where the longest nondecreasing subsequence has length $$$k$$$, then the longest nondecreasing subsequence must compose of only $$$0$$$'s. In this case, the length of the longest nondecreasing subsequence up to index $$$i-1$$$ must be $$$k-1$$$, and the number of zeroes are also fixed — this is also a direct application of the original formula. Otherwise (that is, if $$$t_i=1$$$), let $$$x'$$$ and $$$y'$$$ be the number of zeroes and ones in this half. At index $$$i-1$$$, the length of the longest nondecreasing subsequence should be exactly $$$k-1$$$. We can calculate this by taking the number of binary strings with LNDS length at least $$$k-1$$$, and subtract the number of binary strings with LNDS length at least $$$k$$$. Note that the number of $$$0$$$'s and $$$1$$$'s in the second half is also fixed (call them $$$x"$$$ and $$$y"$$$ respectively), so we can multiply the previous result by the number of binary strings with LNDS at least $$$k$$$. We can write this as getting the sum $$$(g(x',y',k-1,Z)-g(x',y',k,Z))\cdot f(x",y",k)$$$.
This looks like the solution will become $$$O(n^3)$$$ since we have to loop through all possible $$$x'$$$ values. However, let's look at $$$g(x,y,k,Z)$$$ again. It is $$$\binom{x+y}{x}$$$ if $$$y \geq k-C_1$$$ or $$$x \geq k-C_2$$$ (call this case $$$1$$$) for some constants $$$C_1$$$ and $$$C_2$$$, and $$$\binom{x+y}{k-Z}$$$ otherwise (call this case $$$2$$$). Consider $$$g(x',y',k-1,Z)$$$ and $$$g(x',y',k,Z)$$$. When both functions are in case $$$1$$$, we have $$$g(x',y',k-1,Z)-g(x',y',k,Z)=0$$$. When both functions are in case $$$2$$$, the difference is a constant. The only case we have to worry about is when one function is in case $$$1$$$ and the other is in case $$$2$$$. But note the conditions for case $$$1$$$ — it is either if $$$y$$$ is larger than some constant or if $$$x$$$ is larger than some constant. Thus, when $$$g(x',y',k-1,Z)$$$ leaves case $$$1$$$, $$$g(x',y',k-1,Z)$$$ will also leave case $$$1$$$. Similarly, when $$$g(x',y',k,Z)$$$ enters case $$$1$$$ (when $$$y$$$ becomes too large), $$$g(x',y',k-1,Z)$$$ will also enter case $$$1$$$ in the next iteration. So, we only have to calculate these cases for $$$2$$$ values of $$$X$$$. Combining this with some prefix sums of binomial factors (to deal with the $$$f(x",y",k)$$$ portion when $$$g(x',y',k-1,Z)-g(x',y',k,Z)$$$ is constant), we actually avoid running the loop entirely. Thus, we can calculate the answer in $$$O(n^2)$$$.








is there any greedy solution for div2 E?
I hope this code could help you^^
Yes, here's how I did it:
As editorial states, if Alice takes item $$$x$$$, she must also take all items $$$y \lt x$$$ that satisfy $$$pos_y \gt pos_x$$$. This condition is both necessary and sufficient.
Now, the greedy solution is as follows: for each $$$i$$$ from $$$1$$$ to $$$n$$$, if $$$v_i \leq 0$$$, we should definitely not take it at this moment. Otherwise, let's find an unused item $$$j \lt i$$$ with the smallest $$$pos_j$$$ satisfying $$$pos_j \gt pos_i$$$. If no such item exists, the condition is already satisfied and we can simply add $$$v_i$$$ to our answer.
Otherwise, if we ever end up taking item $$$j$$$, we will surely also take item $$$i$$$. Therefore, we can merge these two into a single item with $$$v' = v_i + v_j$$$ and $$$pos' = pos_j$$$. If $$$v' \gt 0$$$, we repeat this process again.
Let me know if anything needs clarification.
340848541
wonderful solution
beautiful solution. Thank You!
"Otherwise, if we ever end up taking item j, we will surely also take item i." — I can sort of see why it holds when I think about it visually using a dependency graph, but I'm not able to come up with a proof for this. Is there any intuitive explanation for why this is the case?
If we ever take item $$$j$$$, it means we must also have taken all other items with $$$k \lt i$$$ and $$$pos_k \gt pos_j$$$, and since $$$pos_j$$$ is the smallest position $$$ \gt pos_i$$$, this means we've also taken all items with $$$pos_k \gt pos_i$$$. Therefore, the condition is satisfied and we can take item $$$i$$$. Since item $$$i$$$ has $$$v_i \geq 0$$$, there's also no reason not to take it.
really good solution
I've solved E12 slightly differently from the editorial one, but couldn't prove why it works well.
I had
rec(l, r, b)where [l, r) is the range that contains shuffled indexes, andbis the values that appear only in [l, r) shuffled indexes. And checked every value inbif it appears in [l, mid), [mid, r) or in both. and descended in to the part with greater size ofb. At the end we have r — l == 1 and |b| = 1, and b[0] would be the answer.Part that made my code work from WA1 on E2 was descending to the range with greater size of the
b. So if it fails to find the answer in the range with the greater size ofb, it will descend to other half. Currently I don't know why that works.It is very interesting but your solution is equivalent to the editorial given (yes I think even the greater part)
At recursion depth 0, you will go into the correct direction because if left is greater in size, it is also correct, and vice versa. In other recuesion depths, you are basically performing the binary search as in editorial.
Ah lol, I am so stupid. Thank you!
UPD: Wait, is it really? I don't think greater size means right range. That range just could contain more pairs
Does anyone understand why to descend into the right half is significantly better than the left half when the size is equal?
These are my submissions: https://mirror.codeforces.com/contest/2151/submission/340223763 https://mirror.codeforces.com/contest/2151/submission/340223737
Same reason as above. One interval has bigger length, so if it has the same number of values it must have the single element.
That make sense. Thank you!
Now I've done some experiments I think my code is not equivalent to the editorial one. It does like Editorial on the recursion depth 0, but after that it can visit both childs in recursion, because greater size doesn't mean it is the correct way to go. So I still have a question of why does it work so well (why does it use so few queries).
code I am referring to 340210616.
UPD: Also applying "going to the side with greater size" optimization only in depth 0 seems okay to pass the tests. Here is the code 340323716
Yes so that is precisely what I said? Please read my comment again.
At depth 0, you choose correct side. At other depths, you simulate the binary search, exactly like the editorial in E2.
Your complexity is expected 1.75n at recursion depth 0, and then expected 0.25n * 3.5 due to left/right only having (expected) 0.25n candidates left as in editorial
I am really sorry. I didn’t read E2 editorial correctly, my bad… Now it’s clear.
Is there any reason for disabling hacks in Div1A?
To prevent hacks based on unordered sets. While I do not have any issues failing solutions that use unordered structures or equivalents, I would prefer participants to focus on solving the problems than hacking hashes.
I think stress testing would break code though logically fine but poorly implemented, not required for div2b
Got WA on div1D because I did
cout << a[0];instead ofcout << a[0] % mod;for $$$n=1$$$. I guess it would have been better to include this example in samples as $$$a_i$$$'s are usually less than the mod value :(I am sorry :( Actually we updated mod to $$$998244353$$$ recently to be consistent with 1G, and then forgot about this issue.
Div2D was a great problem!
A (too complicated) forward DP solution for Div2D:
(Must color the two extreme cells)
Since we can only color cells up to the (n+1)/2 row, we can compute this DP up to n/2.
Define c_i as the number of cells we colored so far. Then we can divide our previous colorings into 3: if we already colored the 2 columns i and n-i+1, if we colored exactly one of them, and if we colored none. If we colored none, we must color them now to satisfy conditions 2 and 3, and similarly for the other cases. This gives rise to the following DP formulation:
Where the ratios mentioned are the fraction of previous colorings that colored the 2 columns i and n-i+1. Since we had some choice in coloring previously, we keep track of that number, which is basically
If we have n columns and we colored k of them, then the ratio where we colored both i and n-i+1 is
Similarly for one specific column colored and the other not colored, it is
Now just plug n = n — 2*i and k = d_i, and you have the DP formula.
340207087
Div.2B was that hard! But like all the problems ABCD :)
I hate 1E.
Hey Nine_Suns, you solved Div1C in like 20 mins ... would you rather not mind explaining a bit ? like why those dp transitions ?
"A simple algorithm works, just make Alice take all items in S in increasing order, and when Alice wants to take some item but it is not the first remaining item according to her priority order, Bob takes items till it becomes first in her priority order."
Can anyone explain Div2E / Div1C editorial a bit easily, like how the above statement came ?
I hope this solution helps you.
Thanx buddy
Since editorials for D1F/G are not currently out, here are some somewhat handwavy outlines of my solutions:
F: WLOG, assume the graph is a tree (if it isn't, take a spanning tree and pretend the edges outside the spanning tree don't exist at the start). For the first query, take k = 3 and connect all pairs of vertices with distance 2. For the second query, take k = ceil(diam / 2) + 1, where diam is the diameter of the tree. We can construct paths with length ceil(diam / 2) between any two remaining vertices.
Indeed, by using the edges from the first query as needed, we can get from one vertex to another in any number of moves between ceil(dist(u, v) / 2) and diam and dist(u, v). If dist(u, v) < ceil(diam / 2), let d be the vertex on the diameter furthest from u. We can follow the path from u to v until it diverges from the path from u to d. Then, through a little casework, we can show that it's possible to visit every vertex between our current position and d and then return to the path to v. (If we don't need this many vertices, we can just not include d and the vertices closest to d on the path.) This lets us achieve any number of moves between dist(u, v) and the number of vertices in the union of the paths from u to d and u to v, which is at least dist(u, d). Thus, we can achieve any number of moves between ceil(dist(u, v) / 2) and dist(u, d). Since dist(u, v) <= diam and dist(u, d) >= ceil(diam / 2), this lets us achieve distance ceil(diam / 2), so we're done.
Here's my code. The complexity is O(n^3).
G: (very rough outline, I find this one fairly hard to explain...)
First, let's think about a subproblem that comes up throughout the task: how many sequences of a 0's and b 1's have longest nondecreasing subsequence (LIS) at least c? We can do complementary counting: for us to have no increasing subsequence of length c, at any point, the number of 0's used so far and the number of 1's remaining must be less than or equal to c. We can think about this as the number of grid paths from (0, 0) to (a, b) where at all points on the path, x + (b — y) < c. I think this subproblem is somewhat well-known in some circles (though it wasn't to me until I solved this task...). Assuming c >= max(a, b), this works out to (a + b) choose a — (a + b) choose c, so (a + b) choose c sequences have LIS at least c. (if c <= max(a, b), all (a + b) choose a sequences have LIS at least c.)
Now, let's get back to the main problem. To deal with the constraint that b is larger than a, iterate over the longest prefix shared between both strings. The next character of a must be 0, and the next character of b must be 1. This gives us O(n) (with n = x + y) possible prefixes of b, and we need to count the number of solutions with each prefix.
To reframe the LIS condition, we'll choose the minimum i such that B[0..i] has LIS k; then, we require that i exists (i.e. B itself must have LIS at least k) and that b[i+1..n] has LIS k. The easy case occurs when our prefix already has LIS at least k, so i has been predetermined. When this is the case, we can just ignore B[0..i], and we just need to ensure that the remainder of b has LIS at least k. This can be handled using the solution to the subproblem above.
The harder case is when our prefix has LIS less than k. In this case, let's iterate over i, i.e., we are iterating over the length of the shortest prefix with LIS k. (This is somewhat motivated by the solution to the subproblem, which largely depends on a+b.) For a fixed i, we can identify the minimum and maximum number of 0s in our prefix of length i, and for these two cases, we can directly compute the number of ways to choose our prefix (noting that we must subtract out cases where the prefix of length i-1 has LIS at least k) and multiply by the number of ways to choose our suffix with the remaining 0s and 1s.
Then, we can prove that the number of ways to choose our prefix is the same for all numbers of 0s strictly between the minimum and the maximum. The rough idea is that if we had more 0s than we needed to guarantee that our prefix has LIS k, the prefix of length i-1 would be guaranteed to have LIS k. Thus, we can have at most exactly the number of 0s needed to guarantee LIS k, which means any number of 0s less than the maximum won't guarantee LIS k. A similar argument applies to the 1s, meaning that if we have more than the minimum number of 0s, we aren't guaranteed to have LIS k. When we apply the subproblem solution, this ensures that we are in the (a + b) choose c case and not the (a + b) choose a case, and since a+b is constant, the whole expression is too.
From here, we can precompute the number of ways to choose a valid suffix with a 0's and b 1's with LIS at least k for all a and b, and using prefix sums, we can multiply the number of valid prefixes for each number of 0s times the number of valid suffixes over all possible numbers of 0s/1s for the suffix. Adding this to the minimum/maximum number of 0 case (which we compute separately as an edge case) gets us the answer for the given prefix of b, completing the solution. The complexity is O(n^2).
I doubt if my code will be helpful to read (though it does technically have a few comments...); if you'd like to take a look, it's at this link.
Problem D is good
I found problem B really challenging!
Can somebody explain me
C.As i am unable to understand it from the editorial.what about the rest of the entrances and exits? you must also assign those to a person which will instantly violate the conditions of k = 2.
Ok, the problem asks us to assign all the entrances and exists as the sensor detected all the 2*N points got it, got it My understanding of the problem was wrong since yesterday!!
thanks!!
For 1C, I thought the condition should be $$$pox_x \lt pos_y$$$ where $$$pos_i$$$ is the position of item $$$i$$$ in array $$$b$$$, instead of $$$b_x$$$ < $$$b_y$$$
Hey could you explain it in simple ways ?
Denotes $$$posa_i$$$ is the position of the $$$i^{th}$$$ object in Alice's preferences, $$$posb_i$$$ is the position of the $$$i^{th}$$$ object in Bob's preferences. The condition for a valid subset of object that Alice could take is: suppose Bob takes the object $$$x$$$ and Alice takes the object $$$y$$$, and $$$posa_x \lt posa_y$$$, then $$$posb_x \lt posb_y$$$ must hold.
For example $$$a = [1, 2, 3, 4]$$$ and $$$b = [2, 3, 4, 1]$$$ (it's actually the $$$7^{th}$$$ sample case). If Bob takes the object $$$1$$$, then Alice couldn't take the object $$$2$$$, $$$3$$$ or $$$4$$$ because in order to take the object $$$1$$$, Bob need to take the object $$$2$$$, $$$3$$$ and $$$4$$$ as well.
progressed a bit ... my skill issue ;) btw what are the transitions you are using ?
Agree
Yeah same ... can you explain this transition part ? autolambda
I saw your code for Div1C can you explain the dp like whats your dp state transition?? I am not able to fully understand editorial's dp solution
I have a general question about div2 e. During the contest, I found O(n^2) dp solution is quite naive.
That is, we have a row transition from condition (if b's j selected a[i], add 0) and always true column transition.
Although row transition is easily implemented by lazy propagation, I cannot solve this problem as I failed to implement column transition in O(logn).
I thought at each level i, we can keep the segment tree monotonic increasing. But the editioral says it is enough to change only one element. How can they think this simple trick easily? Just one thing for optimization always bothers me. Could you give some tip to resolve this situation? Most of the time, I can't think some minor point. Thank you in advance!
In the solution of Limited Edition Shop, it seems that there are some ambiguous points.
div2 D such an amazing problem. Once you find the key point by observation(the graph), you can immediately solve this problem
I need a O(n*n) solution C++ code for div2 E, the Editotrial is not very clear to me QAQ......
Can someone explain the solution to E1 in more detail? I dont understand what the array waste stands for because waste contains the value which appear both in [tl, tr] and outside it. just implies waste contains all values xD
I hope I add the conclution of Div1 C.
First, make $$$a_i=i$$$ is right.But how does the array b change?
Obviously, if the number $$$x$$$ becomes $$$y$$$, then find $$$i$$$ such that $$$b_i=x$$$, and change $$$b_i$$$ to $$$y$$$, which can be achieved by $$$O(n)$$$.
Next, Let $$$x$$$ choose, $$$y$$$ choose not(actually also $$$a_x$$$ and $$$a_y$$$).We will have two situations:
$$$x \lt y$$$ : We find $$$j$$$,$$$k$$$ such that $$$b_j=x$$$ and $$$b_k=y$$$.If $$$k \lt j$$$, we can know that this is impossible(If we want choose $$$y$$$, then we must choose array a until $$$k$$$. But $$$x$$$ is also chosen.). Similarly, we can know that $$$k \gt j$$$ is possible.
$$$x \gt y$$$ : We can choose $$$y$$$ first, so is possible both $$$k \lt j$$$ or $$$k \gt j$$$.
So, we can modify array b like this: If $$$b_j=x$$$, then let $$$Newb_x=j$$$.
Lastly, the array Newb is the array b mentioned in the solution ! We can copy the values from Newb to b.
This can explain why "For all $$$x\in S$$$, $$$y\in S$$$, if $$$x \lt y$$$, then $$$b_x \lt b_y$$$".
This is my code(Only the part of change array b):
I hope it can help someone!
For Div.1 F, if we fix a root, we only need to consider two cases:
$$$\operatorname{dist}(u,v) \ge k$$$ : This is exactly the same as in the official editorial.
$$$\operatorname{dist}(u,v) \lt k$$$ :
The path can be split into three parts: $$$u \to \text{lca}$$$, then an “up-and-back” segment starting from the lca, and finally $$$\text{lca} \to v$$$.
Suppose that when going upward from the lca we eventually reach some node $$$x$$$. Let the nodes on this path be $$$p_0=\text{lca},p_1,\dots,p_{\text{len}}=x$$$.
We can then take all vertices with even (odd) indices in order, followed by all vertices with odd (even) indices in reverse order.
It can be checked that at least one of these two traversals correctly handles the cases $$$u=\text{lca}$$$ or $$$v=\text{lca}$$$.
Finally, it can be proven that if we try both endpoints of the diameter as the root, then at least one choice will always work.
340368161
TheScrasse , There seems to be some error in the editorial for Div2E , (Limited edition shop).
As per editorial, we assume, preference of items for Alice is $$$A_i = i$$$.
So, imagine array A (preference of Alice) = [1 , 2 , 3 , 4 , 5 , 6 , 7].
And.............. preference order for Bob B = [3 , 4 , 2 , 6, 1 , 7 , 5].
Now, imagine, I want to select a set S = { 1 , 4 , 6 } ( basically I want to select items 1 , 4 , and 6 ). S' = { 2 , 3 , 5 , 7 } ( items which are not selected by Alice, so must be selected by BOB).
Let's look at the condition given in the editorial...
For $$$x \notin S$$$, $$$y \in S$$$ , this condition must hold true $$$B_x \lt B_y$$$.
If we get all the pairs (x , y) where (x < y) and $$$x \in S'$$$ (or $$$x \notin S$$$ otherwise) and $$$y \in S$$$ , we have 5 pairs. (2 , 4) , (2 , 6) , (3 , 4) , (3 , 6) and (5 , 6).
for (2,4) , $$$B_2$$$ = 4 , $$$B_4$$$ = 6, So we can see $$$B_2$$$ < $$$B_4$$$.
for (2,6) , $$$B_2$$$ = 4 , $$$B_6$$$ = 7, So we can see $$$B_2$$$ < $$$B_6$$$.
for (3,4) , $$$B_3$$$ = 2 , $$$B_4$$$ = 6, So we can see $$$B_3$$$ < $$$B_4$$$.
for (3,6) , $$$B_3$$$ = 2 , $$$B_6$$$ = 7, So we can see $$$B_3$$$ < $$$B_6$$$.
for (5,6) , $$$B_3$$$ = 1 , $$$B_6$$$ = 7, So we can see $$$B_3$$$ < $$$B_6$$$.
Even after this condition, holds, we can never select the set of items { 1 , 4 , 6 } from order of preferences. Please help clarify this.
------------------------------------------------------------------ Update :
As per TheScrasse's reply, the editorial error has been fixed. this comment's content is outdated.
I have not written the editorial for this problem. I think $$$b$$$ should be replaced with $$$b^{-1}$$$ (we care about positions of $$$x$$$ and $$$y$$$, not about the $$$x$$$-th and $$$y$$$-th element). Dominater069
In 2150D - Attraction Theory, I somewhat quickly arrived at counting the number of valid arrays of length $$$k$$$ as
after which I spent embarrassingly much time on figuring out how to to reconcile things with $$$1-x$$$ and $$$1-x^2$$$ in the denominator. The intended solution ultimately does it by fixing the parity of the first and the last non-zero number, which I eventually also arrived to. After doing so, I also figured out that it is actually simple to count while still being in genfunc space:
which now has a uniform way to extract $$$x^{n-k}$$$, independently of the parity of border parts. Ultimately, it is still equivalent to what intended solution is doing, but I wanted to share the way to do in purely genfunc way. Then, the total sum over border elements can also be expressed as $$$s = [x^{n-k}] \frac{(1+x)^3}{(1-x^2)^{k+1}}$$$, and over inner elements as $$$\frac{c \cdot n-2s}{k-2}$$$.
I think there's a mistake in div2C editorial, starting from "Now we have to calculate the following terms:", I don't understand the rationale behind the formula, why there're multipliers up to k, aren't multipliers of +-1 next to a_i enough? There isn't an explanation how terms S1, S2 and S3 are combined to calculate the final answer. Solution (code) makes sense though, but it uses different approach, deriving k from k-2.
2151C — Incremental Stay is a shit and uncleared answer
Sorry for necroposting. I got Div1E1 from a TLE bot on discord. I think the bound of $$$4n + 2 \lceil \log_2 n \rceil$$$ queries is very loose in most cases and thought it can be reduced down to something around $$$3.7 \cdot n + c$$$ where $$$c$$$ is some constant (for small cases). Next is a semi-formal proof.
Denote by $$$f(d, w)$$$ the number of queries used to find the value, given that we have $$$w$$$ "waste" and $$$d$$$ elements that could be the "lonely value". We can show that $$$f(d, w) = \frac{5}{3} d + w + f(\frac{d}{3}, \frac{d}{3}+\frac{w}{2})$$$.
We do that by noting that for a random permutation for a number that appears twice (the chance of it being only in the first half) = (the chance of it being only in the second half) = (the chance of it being in both halves) = $$$\frac{1}{3}$$$. If we query some number with one half and we get $$$0$$$ then we don't need to query the other half. Thus we have $$$\frac{1}{3} \cdot 1 + \frac{1}{3} \cdot 2 + \frac{1}{3} \cdot 2 = \frac{5}{3}$$$.
Similar to the editorial we query each of the wastes once so $$$+w$$$. Note that on average we get half of the wastes on each half (again, if we consider a random permuation of the indices instead of an interval) so we have $$$\frac{w}{2}$$$ waste in the next step. We also have $$$\frac{1}{3} \cdot d = \frac{d}{3}$$$ expected waste in the next step (from the values that appear in both halves). In total $$$\frac{d}{3} + \frac{w}{2}$$$ waste in the next step.
If we unfold the definition a bit and ignore the terms containing $$$d$$$, we will see a pattern (consider $$$\text{_}$$$ as a placeholder for $$$\text{constant} \cdot d$$$).
$$$f(d, w)= \text{_} + w + f(\text{_}, \text{_} + \frac{w}{2}) = \text{_} + w + \frac{w}{2} + f(\text{_}, \text{_} + \frac{w}{4}) = \text{_} + w + \frac{w}{2} + \frac{w}{4} + f(\text{_}, \text{_} + \frac{w}{8}) = \dots = \text{_} + w \cdot \displaystyle{\sum_{k=0}^\infty \left( \frac{1}{2} \right) ^ k} = \text{_} + 2w$$$
This leads us to $$$f(d, w) \leq \frac{5d}{3} + 2w + f(\frac{d}{3}, \frac{d}{3}) \leq \frac{5d}{3} + 2w + \frac{2d}{3} + f(\frac{d}{3}, 0) = \frac{7d}{3} + 2w + f(\frac{d}{3}, 0)$$$.
Let now $$$g(n) = \frac{7n}{3} + g(\frac{n}{3})$$$. Note that $$$g(n) = f(n, 0)$$$. We now get that $$$g(n) \leq \frac{7n}{3} \cdot \displaystyle{ \sum_{k=0}^\infty \left( \frac{1}{3} \right)^k } = \frac{7n}{3} \cdot \frac{3}{2} = \frac{7}{2} n$$$. Thus $$$f(n, 0) \leq \frac{7}{2}n$$$.
This proof is not very formal, there are missing expected values and stuff. I also tested it locally with random tests and the maximum factor is around $$$3.7$$$ for large values of $$$n$$$. Sadly, for small values it fails quite badly, sometimes reaching and even slightly exceeding $$$4$$$ (although a constant like $$$40$$$ or something might fix this still)
can anyone explain, in Div1B, for the first test case, why the following grid is not valid?
10001 10001 00100 00000 00000
here 1 represents black and 0 white.
this grid satisfies all the given conditions still they haven't considered it as a valid solution for test case 1