Thank you for helping Marisa, Reimu, Cirno, Sanae, Amanojaku and Momoyo!
2228A - Marisa Steals Reimu's Takeout
Author: Sanae
Solution: Sanae
Lemma 1. There exists an optimal solution in which each zero is divided separately.
Proof. Suppose in some optimal solution, a zero is grouped with other elements. Separating it into a singleton group does not decrease the total score. Hence, we can transform any optimal solution into one where all zeros are divided separately without worsening the objective. $$$\blacksquare$$$
Therefore, we can restrict our attention to the subsequence consisting only of $1$s and $2$s.
Lemma 2. There exists an optimal solution in which any group containing both $$$1$$$ and $$$2$$$ is exactly either $$$[1,2]$$$ or $$$[2,1]$$$.
Proof. Consider a group that contains $$$1$$$, $$$2$$$, and possibly other elements. Splitting it into $$$[1,2]$$$ (or $$$[2,1]$$$) and the remaining part does not decrease the total score. Consequently, we can transform any optimal solution into the desired form. $$$\blacksquare$$$
By Lemma 2, we can greedily extract all $$$[1,2]$$$ or $$$[2,1]$$$ pairs from the remaining sequence.
Finally, for the leftover elements, the best strategy is clearly to take triplets $$$[1,1,1]$$$ and $$$[2,2,2]$$$ whenever possible.
2228B - Remilia Plays Soku
Author: Sanae
Solution: Sanae
We assume $$$n\geq 4$$$ in what follows.
Let us analyze the strategies of both players. Reimu aims to get as close to Remilia as possible, while Remilia aims to get as far away from Reimu as possible. Remilia will use her moves to increase the distance without being caught, and indeed it is optimal for her to use as many moves as possible.
The distance on the circle can be measured with $$$d=min(|x_1-x_2|,n-|x_1-x_2|)$$$. Once $$$d=1$$$, Remilia can increase $$$d$$$ by $$$1$$$ since the circle is large enough ($$$n\geq 4$$$). Similarly, Reimu can decrease $$$d$$$ by $$$1$$$ each turn.
Therefore, we can calculate the outcome directly. The answer is the initial distance plus the extra time gained by Remilia's moves, $$$min(|x_1-x_2|,n-|x_1-x_2|)+k$$$.
Note that when $$$n\leq 3$$$, the answer is always $$$1$$$, because Reimu can always catch Remilia in one step.
2228C1 - Cirno and Number (Easy Version) and 2228C2 - Cirno and Number (Hard Version)
The case $$$a=b$$$ is trivial. We consider two cases separately: $$$a \gt b$$$ and $$$a \lt b$$$.
We first discuss $$$a \lt b$$$ in what follows. Consider the longest common prefix of $$$a$$$ and $$$b$$$, scanning from the highest digit to the lowest significant digit. Let $$$x$$$ be the digit of $$$a$$$ be at that position and $$$y$$$ be the digit of $$$b$$$ be at that position. If $$$d_n \gt x$$$, choose $$$y$$$ as the smallest $$$d_i$$$ such that $$$d_i \gt x$$$. Then set the digits beneath in $$$b$$$ to $$$d_1$$$. This guarantees that $$$b$$$ is the smallest number greater than $$$a$$$ and the decimal representation of $$$b$$$ contains only digits from $$$d$$$. Otherwise, if $$$d_n \lt x$$$,we should change an earlier digit to satisfy the inequality. Note that we can make the number of digits in $$$b$$$ more than $$$a$$$.
Similarly, we assume $$$a \gt b$$$. Consider the longest common prefix of $$$a$$$ and $$$b$$$, scanning from the highest digit to the lowest significant digit. Let $$$x$$$ be the digit of $$$a$$$ be at that position and $$$y$$$ be the digit of $$$b$$$ be at that position. If $$$d_1 \lt x$$$, choose $$$y$$$ as the largest $$$d_i$$$ such that $$$d_i \lt x$$$. Then set the digits beneath in $$$b$$$ to $$$d_n$$$. This guarantees that $$$b$$$ is the largest number smaller than $$$a$$$ and the decimal representation of $$$b$$$ contains only digits from $$$d$$$. Otherwise, if $$$d_1 \gt x$$$,we should change an earlier digit to satisfy the inequality. Note that if $$$d=[1,2,3]$$$, then $$$b=0$$$ is invalid, because $$$0$$$ dosn't belongs to $$$d$$$.
Depending on the implementation, the time complexity can be $$$O(10n\log a)$$$, $$$O(10n\log^2 a)$$$, $$$O(n\log a)$$$ or $$$O(n\log^2 a)$$$.
2228D - Sanae, Cross and Color
Author: Sanae
Solution: Sanae, fanhuaxingyu
We can apply the principle of inclusion--exclusion to compute the answer. First, enumerate a vertical dividing line at integer coordinate $$$x$$$ from $$$1$$$ to $$$n$$$; the contribution is recalculated only when the set of points on the left side changes ~---otherwise, we skip redundant computations.
For each such $$$x$$$, we then enumerate a horizontal dividing line at integer $$$y$$$ from $$$1$$$ to $$$n$$$, while carefully excluding overlapping cases to avoid double-counting. Let the y-coordinates of the points on the left side be $$${y_1,y_2,\dots,y_k}$$$, sorted in increasing order. For consecutive y-values $$$y_i$$$ and $$$y_{i+1}$$$, all values in the form $$$k+0.5$$$ in the interval $$$(y_i+0.5,\;y_{i+1}-0.5)$$$ give the same left-side point set. Thus, the left side produces a set of y-intervals, and similarly, the right side also forms a set of y-intervals. To count all valid configurations, we need to count the number of interval-pairs (one interval from the left, one from the right) that intersect in at least one point. This can be computed efficiently using a Fenwick tree (Binary Indexed Tree) and a set data structure.
The overall time complexity is $$$O(n\log n)$$$.
An observation is that any valid dividing line must cross at least one of the two groups determined by the smaller y-coordinate.
Suppose the line crosses the group with smaller $$$x$$$ and smaller $$$y$$$. Enumerate the point whose $$$y$$$-coordinate is maximal within this group (and among those with the same maximal $$$y$$$, take the one with the larger $$$x$$$). Using appropriate pre-computation, we can efficiently determine the feasible vertical dividing position $$$x$$$.
The case where the line crosses the group with larger $$$x$$$ but smaller $$$y$$$ is symmetric to the above.
The overall time complexity is $$$O(n)$$$.
2228E1 - Amanojaku and Sequence (Easy Version) and 2228E2 - Amanojaku and Sequence (Hard Version)
Author: Sanae
Solution: Sanae
Tutorial: CirnoNine, fanhuaxingyu
First, consider solving the whole array. We can first compute the total number of possible configurations of $$$a$$$ using stars and bars. Let this number be $$$tot$$$. The remaining sum to be freely distributed is $$$sum= m - \sum_{i} a_i[a_i\neq -1]$$$, and the number of undetermined positions is $$$cnt=\sum_{i} [a_i = -1]$$$. It is obvious $$$tot=\binom{sum+cnt-1}{cnt-1}$$$. We adopt the convention $$$\binom{-1}{-1}=1$$$ to simplify boundary cases.
Denote $$$S_1=2\sum_{b \text{ is valid}}\sum_{1\leq i \lt j\leq n}b_ib_j (n+1-j)$$$ and $$$S_2=\sum_{b \text{ is valid}}\sum_{1\leq i \leq n}b_i^2 (n+1-i))$$$. We should compute each part individually.
To do the computation, we need to first introduce some useful math conclusions. We give the $$$T_0,T_1,T_2$$$.
We recall the following standard combinatorial identities: \begin{enumerate} \item If $$$n\geq 0$$$, then $$$\binom{n}{m}=\binom{n}{n-m}$$$
\item For any $$$n,m$$$,then $$$m\binom{n}{m}=n\binom{n-1}{m-1}$$$
\item If $$$n,m\geq 0$$$, then $$$n\binom{n+m}{n}=(m+1)\binom{n+m}{m+1}$$$ \end{enumerate}
The $$$T_0$$$ is classic,while $$$T_1,T_2$$$ need some work.
The computation of $$$T_1$$$ is straightforward.
Similarly, we compute $$$T_2$$$:
We now derive $$$T_3$$$:
&= (m+1)(\sum_{x=0}^{n-1} x^2\binom{x+m+1}{m+1}+2\sum_{x=0}^{n-1} x\binom{x+m+1}{m+1}+\sum_{x=0}^{n-1} \binom{x+m+1}{m+1})\\ &= (m+1)(T_2(n-1,m+1)+2T_1(n-1,m+1)+T_0(n-1,m+1)) \\\\ &\quad \text{Note: It can be further simplified, but we can just stop here.} \end{aligned} $$$
We first deal with $$$S_2$$$.$$$S_2=\sum_{1\leq i \leq n}\sum_{b \text{ is valid}}b_i^2 (n+1-i))$$$.If $$$a_i\geq 0$$$, $$$b_i=a_i$$$, so the formula can be written like $$$a_i^2(n+1-i) tot$$$. If $$$a_i=-1$$$,the formula can be written like
Second, we cope with $$$S_1$$$.
$$$S_1=2\sum_{b \text{ is valid}}\sum_{1\leq i \lt j\leq n}b_ib_j (n+1-j)$$$
After selecting $$$i,j$$$, we only need to compute $$$\sum_{b \text{ is valid}}b_ib_j$$$.
There are three cases:
\begin{enumerate} \item $$$a_i\neq -1 \text{ and } a_j\neq -1$$$.
$$$\sum_{b \text{ is valid}}b_ib_j=tot\cdot a_ia_j$$$
\item $$$a_i\neq -1 \text{ and } a_j = -1$$$, $$$a_i\neq -1 \text{ and } a_j = -1$$$.
It remains to consider $$$a_i\neq -1 \text{ and } a_j = -1$$$, because another case is similar.
It can also be computed by the expectation of each position.
\item $$$a_i = -1 \text{ and } a_j = -1$$$.
It is the trickiest part of the solution. We analyze this case carefully.
It is just a constant, which is easy to compute.
\end{enumerate}
Then, it is time to cope with $$$S_1$$$.
$$$S_1=2\sum_{b \text{ is valid}}\sum_{1\leq i \lt j\leq n}b_ib_j (n+1-j)$$$
For type $$$1$$$, it suffices to compute
.
For type $$$2$$$, it suffices to compute
.
For type $$$3$$$, it suffices to compute
.
All required quantities can be maintained using a segment tree, and the interval queries and modifications can be processed easily. Note that $$$n-j+1=(n+1)-j$$$. So process $$$X \cdot(n+1)$$$ and $$$X \cdot j$$$ in two data structures. Implementation details are omitted for brevity; see the accompanying code.
By querying on the tree, we can get the answer. And the information can also be modified efficiently.
After all, we can solve the problem in the total time complexity $$$O(1\ 000\ 000)-O((n+q)\log n)$$$ and total space complexity $$$O(1\ 000\ 000+n)$$$.
Note $$$\binom{-1}{-1}=1$$$ in the code.
For a fixed query interval, denote it by $$$b_1,b_2,\ldots,b_L$$$.
Define
and
Here, $$$A_i$$$ is the prefix sum contributed by fixed positions, and $$$T_i$$$ is the number of free positions in the first $$$i$$$ elements.
Let
The total value that must be assigned to all free positions is
If $$$N \lt 0$$$, there is no valid sequence, so the answer is $$$0$$$.
If $$$k=0$$$, there is only one possible sequence. The answer is $$$\sum_i A_i^2$$$ if $$$N=0$$$, and $$$0$$$ otherwise.
Now assume $$$k \gt 0$$$ and $$$N\ge 0$$$.
Let the values assigned to the $$$k$$$ free positions be
where
The number of such assignments is
For prefix $$$i$$$, there are exactly $$$T_i$$$ free positions inside it. Therefore, the contribution of assigned values to this prefix is
So the real prefix sum at position $$$i$$$ is
Therefore, we need to calculate
Expanding the square gives
Now we calculate these three parts separately.
First, consider the $$$A_i^2$$$ part.
Since $$$A_i$$$ does not depend on the assignment, every valid assignment contributes the same value. Hence this part contributes
Second, consider the $$$2A_iX_{T_i}$$$ part.
By symmetry, among all weak compositions of $$$N$$$ into $$$k$$$ parts, every variable has the same total contribution. Thus
Therefore,
Substituting $$$t=T_i$$$, we get
After summing over all $$$i$$$, this part contributes
Third, consider the $$$X_{T_i}^2$$$ part.
We need the second moment of
For all weak compositions of $$$N$$$ into $$$k$$$ parts, we have
and for $$$p\ne q$$$,
Since
we get
Equivalently,
Substituting $$$t=T_i$$$ and summing over all positions, the final formula is
All divisions are done modulo $$$998244353$$$ using modular inverses.
Now we explain why this can be maintained by a segment tree.
For every interval, the formula only needs the following values:
Therefore, each segment tree node maintains:
Here, $$$len$$$ is the length of the segment, $$$sum$$$ is the total fixed value in the segment, and $$$cnt$$$ is the number of $$$-1$$$ positions in the segment.
The other values are:
All prefix values are defined relative to the left end of the current segment.
When merging two adjacent segments $$$L$$$ and $$$R$$$, every prefix inside $$$L$$$ remains unchanged.
For a prefix inside $$$R$$$, the entire left segment is already before it. Therefore,
and
So the merge formulas are:
Thus, the maintained information is closed under concatenation, so it is suitable for a segment tree.
A point update only changes one leaf, and then all nodes on the path to the root are recomputed.
For a range query, we merge $$$O(\log n)$$$ segment tree nodes in left-to-right order. This gives all values required by the formula above.
After precomputing factorials and inverse factorials for combinations, the total complexity is
Solution
Let sum be the sum of all fixed values in the interval, and let cnt be the number of -1's.
If:
sum > m; orcnt = 0andsum ≠ m;
then no valid sequence exists, and the answer is 0.
The following discussion focuses only on valid cases.
For a single query, we have a simple brute-force approach with complexity $$$O(nm)$$$.
Let:
- $$$A_i$$$: The sum of fixed values among the first $$$i$$$ positions;
- $$$C_i$$$: The count of
-1's among the first $$$i$$$ positions; - $$$rem = m - sum$$$.
Then:
Here, $$$x_i$$$ represents the total sum distributed to the first $$$C_i$$$ unknown variables (the -1's).
Fixing a prefix $$$i$$$, if the unknowns in this prefix are assigned a sum of $$$j$$$, then the suffix unknowns must be assigned $$$rem-j$$$.
The number of ways is given by the classic Stars and Bars method:
However, this approach still has excessive complexity. We consider further optimization.
This recurrence uses identity transformations: - $$$n\binom{n+k-1}{k-1}= k\binom{n+k-1}{k}$$$, - $$$n(n-1)\binom{n+k-1}{k-1}= k(k+1)\binom{n+k-1}{k+1}$$$, - $$$\sum_{k=0}^{n} \binom{k}{a}\binom{n-k}{m-a}=\binom{n+1}{m+1}$$$.
Thus, we obtain an $$$O(n)$$$ solution for the problem, which corresponds to the solution for E1.
How can we optimize further?
Observing the formula above, it becomes clear that if we can quickly maintain interval information such as:
- $$$sum$$$,
- $$$cnt$$$,
- $$$\sum_{i=1}^nA_i^2$$$,
- $$$\sum_{i=1}^nA_iC_i$$$,
- $$$\sum_{i=1}^nC_i$$$,
- $$$\sum_{i=1}^nC_i^2$$$,
a Segment Tree can handle these operations within $$$O(\log n)$$$ time complexity.
Therefore, the solution for E2 is to use a Segment Tree to optimize the recurrence formula, achieving a total time complexity of $$$O(M+(n+q)\log n)$$$.
2228F - Momoyo and the Network
Author: Sanae
Solution: Sanae, juan_123, SDSXC
First Solution
We binary-search the answer $$$V$$$. The monotonicity is clear: a larger $$$V$$$ is harder to achieve, and as $$$k$$$ increases, the feasible $$$V$$$ decreases.
Root the tree arbitrarily at $$$1$$$. For a node $$$x$$$, define $$$dp(x)$$$ as the maximum length (in edges) of a downward-only path starting at $$$x$$$ such that every vertex on the path satisfies: the weight of the component containing that vertex (after cutting the path) is at least $$$V$$$.
During DFS, at node $$$x$$$, we have the $$$dp$$$-values of its children. We need to check whether we can form a path of length $$$k$$$ that passes through $$$x$$$ (or starts at $$$x$$$). This reduces to finding two distinct children $$$y_1$$$, $$$y_2$$$ such that $$$dp(y_1) + dp(y_2) + 2 \ge k$$$ (or a single child with $$$dp(y) + 1 \ge k$$$ if the path starts downward from $$$x$$$).
Sorting the $$$dp$$$-values of the children allows an $$$O(\text{deg}(x)\log \text{deg}(x))$$$ check per node via two-pointers.
The overall time complexity is: $$$O(n\log V\log n)$$$.
Second Solution
Instead of computing $$$dp$$$ by length, we can pre-compute the total weight of each subtree and sort the children of each vertex accordingly. For a candidate $$$V$$$, we can check feasibility by sorting children according to their subtree weights (or the weight of the ``outside-the-path'' component) and using a two-pointer scan to see if a $$$k$$$-edge path exists that meets the $$$V$$$ threshold everywhere.
This eliminates the $$$\log n$$$ factor from sorting $$$dp$$$-values inside the binary search.
The overall time complexity is: $$$O(n\log n+n \log V)$$$.
Third Solution
It's the same as the juan_123's, just for reference.
Apply centroid decomposition. For a centroid $$$rt$$$, any $$$k$$$-edge path either lies entirely within one of $$$rt$$$'s subtrees (handled recursively) or passes through $$$rt$$$. In the latter case, the path consists of two legs, one in each of two different subtrees of $$$rt$$$.
For each vertex $$$u$$$ in a subtree, compute the minimum component weight along the path from $$$u$$$ to $$$rt$$$ (this is the minimum of the ``remaining component weights'' for vertices on that segment). Now, choosing two vertices $$$u$$$, $$$v$$$ in different subtrees gives a path through $$$rt$$$; the overall value is the minimum of the two minima from $$$u$$$ and $$$v$$$ and the components attached to other children of $$$rt$$$.
A key observation: if the largest component (by total weight) attached to $$$rt$$$ is not included in the path, it cannot be the minimum component. We only need to consider the possibility that the path touches the largest component, or handle the case where the largest component is excluded, which simplifies the check.
Carefully pruning with this observation, and using appropriate data structures to query for pairs of legs whose total length is $$$k$$$, leads to an $$$O(n \log n)$$$ solution.
Pruning cases where $$$k$$$ exceeds the size of the current component yields improved performance.
The overall time complexity is: $$$O(n\log n)$$$.
First, we consider how to characterize the connected components formed after deleting the edges along a path, specifically focusing on the component containing a vertex $$$x$$$ on the path.
Let the root of the tree be $$$r$$$. Define $$$sz_x$$$ as the sum of weights in the subtree of $$$x$$$, $$$fa_x$$$ as the parent of $$$x$$$, and $$$tot$$$ as the sum of all $$$a_i$$$. Let LCA denote the lowest common ancestor of all points on the path. We analyze the position of $$$x$$$ on the path:
- If $$$x$$$ is an endpoint of the path but not the LCA, the edge connecting $$$x$$$ to its parent $$$fa_x$$$ is deleted, while the edges to its children remain. The size of the resulting component is
. 2. If $$$x$$$ is neither the LCA nor an endpoint, the edges to $$$fa_x$$$ and to one specific child (say $$$y$$$) are deleted. The component size is
. 3. If $$$x$$$ is the LCA, then 1 or 2 edges to its children (say $$$y$$$ and $$$z$$$) are deleted. The component size is
.
We employ Centroid Decomposition. At each step, we fix a centroid $$$r_0$$$ as the root and consider the contribution of all paths passing through $$$r_0$$$. For any such path, the LCA is $$$r_0$$$. Let $$$son_{r_0}$$$ be the set of children of $$$r_0$$$.
For contributions of type 1 and 2, we DFS the subtrees rooted at the children of $$$r_0$$$. We compute $$$val_{u,d}$$$, representing the maximum possible minimum component size if we select a path of length $$$d$$$ within the subtree of $$$u$$$ ($$$u \in son_{r_0}$$$), without yet considering the component containing the root $$$r_0$$$.
Now, consider a pair $$$(x, y, d_0, d_1)$$$ where $$$d_0 + d_1 = k$$$, $$$x, y \in son_{r_0}$$$, and $$$x \neq y$$$. This represents a path with endpoints in the subtrees of $$$x$$$ and $$$y$$$ at depths $$$d_0$$$ and $$$d_1$$$, respectively. The minimum component size for this configuration is:
Next, we handle the term $$$tot - sz_x - sz_y$$$ associated with the root component.
Note that for any $$$u$$$ and $$$d$$$,
. This is because $a_i > 0$, and $$$val$$$ represents splitting a subtree into parts, which cannot exceed the sum of the whole subtree.
Let $$$v$$$ be the child of $$$r_0$$$ with the largest subtree size ($$$sz_v$$$). If $$$x \neq v$$$ and $$$y \neq v$$$, the size of the root's component $$$s = tot - sz_x - sz_y$$$ satisfies
. Since this component fully contains the large subtree of $v$, it will never be the minimum. Thus, we can ignore the root's contribution in this case and use prefix/suffix minimums to calculate the contribution efficiently.
Otherwise, one endpoint lies in the subtree of $$$v$$$. We can enumerate the other endpoint's subtree, making the calculation straightforward.
The total time complexity is $$$O(n \log n)$$$.
A crucial detail is that during the DFS for subtree sizes, if we encounter a node that was previously a centroid root, we must add the size of the subtree blocked by that centroid to the current subtree size.
Root the tree arbitrarily at $$$1$$$.
For a node $$$u$$$, define $$$dp(u)$$$ as the maximum length (in edges) of a downward-only path starting at $$$u$$$ such that every vertex on the path satisfies: the weight of the component containing that vertex (after cutting the path) is at least $$$V$$$.
Define $$$f(u,x)$$$ as the maximum $$$V$$$ satisfies: $$$dep(u)+dp(u)\geq x$$$.
Define $$$b(u)$$$ as $$$\sum\limits_{v\in subtree(u)}a(v)$$$.
According to the solution above, we could easily write a $$$O(n^2)$$$ brute force to calculate $$$f(u,x)$$$ and the answer. Try to optimize it by using long path decomposition.
First we should calculate $$$f(u,x)$$$. The transition is: $$$f(u,x)=\max\limits_{fa_v=u}(\min(b(u)-b(v),f(v,x)))$$$ for $$$x \gt dep(u)$$$ and $$$f(u,x)=b(u)$$$ for $$$x=dep(u)$$$.
The most difficult part is the $$$\min(b(u)-b(v),f(v,x))$$$. Note that, $$$f(v,x)\geq f(v,x+1)$$$, so every time we set a prefix of $$$f(v)$$$ to $$$b(u)-b(v)$$$, we use something similar to ODT to solve this. Instead of maintain $$$(x,val)$$$ to represent $$$f(u,x)=val$$$, we maintain $$$(l,r,val)$$$ to represent $$$f(u,x)=val$$$ for all $$$x\in [l,r]$$$. The other part is easy to solve.
Then we should calculate the answer. We calculate the answer at the lca of the path. Assume that the lca is node $$$u$$$.
If $$$u$$$ is one of the endpoint, then the answer is $$$\max\limits_{fa_v=u}(\min(f(v,dep(u)+k),b(1)-b(v)))$$$. It seems that we should binerary search for the position where $$$f(u,dep(u)+k)$$$ store. But note that $$$f(v,x)$$$ is useless while $$$x \gt dep(u)+k$$$, so we erase them. And the $$$f(u,dep(u)+k)$$$ is at the end of $$$f(u)$$$.
If $$$u$$$ is not a endpoint, then the answer is $$$\max\limits_{fa_v=fa_w=u,x+y\geq 2dep(u)+k}(\min(f(v,x),f(w,y),b(1)-b(v)-b(w)))$$$. It is easy to solve if there is no $$$b(1)-b(v)-b(w)$$$.
Define $$$son(u)$$$ as $$$v$$$ with the maximum $$$b(v)$$$ satisfies: v is the son of $$$u$$$. Note that $$$\min(f(v,x),f(w,y))\leq b(1)-b(v)-b(w)$$$ while neither $$$v$$$ or $$$w$$$ is equal to $$$son(u)$$$. So we use brute force while $$$v=son(u)$$$ and $$$w=son(u)$$$.
The overall time complexity is: $$$O(n)$$$.









i feel bad that i missed n=3 case in B :(
I also missed that case n <= 3 I looked for all the options but didn't find a solution.
me also ;( i didn't notice this case even though i found the answer
I created video editorial for D. Sanae, Cross and Color.
I discovered the greedy algorithm, but I forgot to specifically handle the case where n<=3))))
yes, that's insane i missed it too :(
today is more cheaters in this contest than expected cf should take strict action on this
how did people use fenwick trees for D? I tried this but got 6-7 TLEs / MLEs, despite trying to optimize constant factors, use lighter trees, rewrite in C++, etc
eventually I had to use a prefix/suffix approach to pass
That wasn't even enough for me. I ended up passing with O(n) inner loop and pragma AVX. I'm going to run some benchmarks once practice opens up and will report back
I passed easily with $$$O(n)$$$(without optimizations) and got TLE in Pretest #19 with $$$O(n \log n)$$$. I guess that's normal.
counting sort orz
yes, getting TLE with O(nlogn). But the tutorial lists O(nlogn) solution as one of the possible solution
let d be the number of digits of a,can any tell if we generate all possible numbers with the 2 given digits of length=d and check for min difference (2^18, recurrsion), i dont know digit dp, and check for the largest number with (d-1) digits and smallest number with (d+1) digits , will it work??
No because there are up to 10,000 test cases, and you might be doing 2^18 operations for each one
you would think so, but notice there are up to 10 choose 2 oprion for 2 digits, so you can just save it for each pair, for a testcase, dont go over all the number but just upper_bound, lower_bound on the vector with the 2 digits you need
so for your question, if implemented correctly YES
now the solution isnt truly digit dp but more just logical thinking of what you can do and the common observation that
10^i > sum over j < i of 10^j
I wasted too much time on D because it doesn't allow $$$O(n \log n)$$$ algorithms to pass, so i didn't finish calculating E1's equation :(
Feeling bad because I succeeded to pass the example of E1 ten minutes ago, which is just 25 minutes after the contest :(
Hope that my rating won't decrease for that..
In fact, $$$O(n\log n)$$$ can pass if it is well implemented.
But I failed twice and had to use $$$O(n)$$$ instead, which made me fail to pass E1 :(
Sorry
Could you please share a well-implemented version of nlogn code. Would be really appreciated.
My solution runs in 1046ms with
std::sort374824095
Here's my solution without Fenwick Trees or 2 Pointers, which runs in 890ms (while still using
sortonce andupper_bound3*ntimes).Here is my code for D
It just uses std::sort and std::lower_bound
Passed in 984ms
374937300
I don't think thats bad, the question should demand O(n) to pass.
I think the time constrains are really good and O(nlogn) shouldn't be allowed. you need to make the observation, even if they are not hard to make it linear
Maybe the problem F is so much similar to 2222G - Statistics on Tree?
Yes, to my surprise, too.
Maybe it is easier to that problem as we don't need to do much analysis to time complexity.
In Problem D, What is the idea behind forcing the constraints that some (N log N) solutions pass and others not ??
If the author think that this kind of forcing constraints will make the problem anti AI, I think he made it harder for humans than AI.
Initially, I only intended for the $$$O(n)$$$ solution to pass...
I feel like a more explicit decision should have been made. As of now, it seems the n log n editorial solution works only when written very tightly and in C, and java/python/etc users also get screwed.
Also visually n log n with 2 million * 21 looks like it should run fine. Even when I sped up the inner loop to O(n) (but still had std::sort and std::set to do coordinates), I still got TLE.
I got rid of set and only used sort and that managed to pass. I also think there was a way to remove sets by first storing it in a vector of vectors then later adding it adding precomp of like n but not that much.
Make your complaint.
Stupid D.
AI-solveable E.
I believe the contest was good in it's core, and I am still thinking on F
but my only complaint is this, in problem E you can't take a question from a combinatorics exam, (I dont really means it's from that but what I am saying is that it's only math and not common one, like the tricks are really just math and not ordinary one, like a question you would see in the end of a combinatorics semester in University)
and I belive that solving just math, and then saying ohhh segment tree to do it faster is not a true cs question. like I believe everyone which is above like 1600 understood that once you solve E1 segment tree will solve E2.
so thats the only thing that bothered me just because I was hoping for something beautiful and got a page of math instead :(
anyhow, thanks for the contest it had been a lot of fun!
I am a fan of wdoi, but this contest is not good enough.
E is too complex and meaningless as Div2 E. I don't think any problem with difficulty <=3000 should use so looong calculation.
And F has a very classic and simple solution: just use binary search, then use simple dp and sorting to check. You can just pass with $$$\Theta(n\log n\log V)$$$ complexity, or sort before binary search and use two-pointer to get $$$\Theta(n(\log n+\log V))$$$.
Plz bring better problems to participants at next contest of wdoi, and accept suggestions about problem. Thx.
Addition: I expect E2 worth >=2600 difficulty, but 100+ participants pass this problem, and F is easier with less pass. How many cheaters in Div2??? That is insane.
If an(/a) expert/specialist pass this E2 in 1 hour is common, I think people can still challenge AI :)
Strongly agree that F is easier and requires significantly less time than E (even a single E1). The amount of AC's is probably caused by nominal problem difficulty. E.g. I spent 40+ minutes on E1 (without success) and read F just 5 minutes before contest end. Finally solving F took me 50 minutes, but for E1 I spent half of next day and still confused with tons of formulas, still no AC. But in overall I liked problem F, I think it's suitable as Div2E. E is a math problem, not very good for a programming contest at all.
E makes me vomit, the hell is this calculation. I thought the same thing as tutorial but couldn't collect patience to sit and write all this.
(though so much to learn)
Someone passed in 30min.
In the testing, the average time to solve is E is within 1h.
Oh, crazy. But I think, if the same difficulty (E1+E2 worth 3500 points, same as F) costs very different time, this is unacceptable for participants who try E and ignore F most of the time.
And, someone passed in 30min can't show anything. The implementation complexity is very different for rating<2100 and rating>=2800. (Maybe same as C2, it also needs clear thinking and implement.) Many of the hard problem can be solved in 30min, but if participants fall into cases of counting and coding in this problem, it is normal to spend >=1.5 hour.
(By the way: I think may be more CNOI style than Codeforces style, as you can spend more time on a difficult problem, because most of CNOI contests have 4 problems and >=4 hours.)
Thx for your reply.
f looks so classic.
my submission:- https://codeforces.me/contest/2228/submission/374854997
can anybody tell me what's wrong with this
plz mention what approach you are using for anyone to read your code easily.
so first of all we get the string of a and remove first charecter until it is not in D
then we make two cases
b > a
in this case pick sallest number > d if no then don't compute this case and make all other numbers smallest possible
compute both numbers
and take abs difference
same is done with the second case
now when b has less digit then a just get x = number of digits in a and take maximum digit in d x — 1 time to get this value and again update answer
the case with more digits is handled this way
here sz is number of digits in a
605 4 2 5 6 9
Correct answer: 6
Use this test case to debug. It is incorrect to remove all the numbers in the beginning that match. There are cases where it is optimal to not match all the beginning digits.
correct testcase pointed out by pancakes. Another simple testcase could be:
149 0,1,4,5 Answer = 150-149=1 If we try to match the longest prefix, we have to make 145 as the closest possible number. Then, you will report 149-145=4 as your answer.
One good advice for these adhoc problems is trying to prove whatever you assumed as your solution. Or even trying to find testcases where your solution might not work. If you find a suitable test case, you will end up finding the solution by just trying to solve the problem for that test case.
Misunderstood B :(
If Remilia does not move at turn 1, k is still 1, so Remilia can move at turn 2. Not "can move before turn k", true is "can move most k turns".
Just got it, thankyou for explaining!
In problem B, Lets say n=4, x1=2, x2=4 and k>0, won't the distance between them reduced by 1? Help me understand this situation.
If Remilia moves at turn 1, the distance will become 1, and Reimu can catch in her turn 1. If Remilia doesn't move, the distance still 2 at turn 1 of Reimu, Reimu move to position 1 or 3, distance=1, then at Remilia turn 2, she move to the other position, then the distance still 2.
The key is, if move cannot increase the distance, Remilia should wait and save the moving chance for following turns.
Ohh Thank you. This makes clear. I misunderstood that "staying" would also decrement her 'k' value.
if initially the distance of both sides are the same, The first player stops at its place for 1 round. And then the second player would decrease the distance by moving in either direction. After the 1st round, they start chasing each other in the same direction. So it's still min_dist + k.
Yep! Misunderstood this one, as k is the time going on, Thanks for making it clear!!
But, somehow I had to add $$$1$$$ to the answer if the $$$\operatorname{abs}(dist1-dist2)\leq1$$$, because $$$\text{Remilia}$$$ would choose not to move in this case, so $$$k$$$ remains the same but the initial $$$min\_dist$$$ decreases by $$$1$$$.
I don't think remelia can choose not to move however the guy can.
Same Here
I didn't like this one :(
A was fine B wasted a lot of time because I thought k was the total number of Remilia moves (it's total number of moves Remilia makes where she doesn't stay in place). It's my fault for misreading but still feels bad. C isn't that bad conceptually but it got REALLY bad once I started implementing. idk I find these kinds of questions where there isn't an insight just caseworking super boring. D looks interesting but I didn't get to it because of ABC :(
What was the intended solution for C1? Is it the same as C2? If so, why did you split C into two subtasks? The editorial's solution doesn't seem like it would be more complex to implement for 10 digits instead of 2. I started writing a brute-force solution for C1 (checking all 2^18 possibilities), but then realized that there are 10^4 tests per case and abandoned it.
I think it was just easier to think when there were 2 digits as to what needs to be done
I used binary search for C1 and C2. I don't really know how to explain it look at my submission: 374842701, if you struggled with implementation I would look at it because it's a lot simpler than greedy
would say L contest but I got paid off with expert performance
Also, are hacks disabled for any problems?
Bro wtf is your code doing? please explain
Ok, so first we define a function $$$\operatorname{gen}(x, w, b)$$$ where $$$x$$$ is the list of numbers, $$$w$$$ is the the amount of digits of the generated number, and it finds the $$$b$$$-th smallest number (0-indexed). We can do this by representing $$$b$$$ as a base $$$n = \operatorname{len}(a)$$$ mask, for example:
For each $w$ from $$$1$$$ to $$$18$$$, we will binary search for the smallest value greater than $$$a$$$ that can be created from the digits, and use $$$\operatorname{gen}$$$ to create these values. The value with the smallest absolute difference per width will either be the value found from binary search, or the generated value that is just smaller than it.
Sorry if this is unclear it's a bit difficult to explain as I said
https://www.youtube.com/watch?v=AzvIHqztzKg
He as the same idea for C(1/2) like sc3developer. Maybe the video will help!
CaseForces
C has many cornercase without effective examples.What a "wonderful" problem.
B was super obvious but unfortunately I spent a lot of time trying to find the n=3 edge case. C1 and C2 are the same solution basically and problems like this are super boring, not creative just implementation hell.
My solution discussion stream ABC2D
E1 we can solve for sum of cubes or higher powers right?
probably, would require a lot of algebra though. or maybe there is an elegant way to use the solution for squares to construct solutions for cubes.
didn't C need some heavier formalism to really define if b = 0 is always a possible solution or not? For me, at least, that wasn't clear at all; but I might be bugging.
what is the point of asking n=2 soln for c1
So, I did some testing and I don't really like D.
During the round:
After, the round, I tried a true O(n) solution with counting sort: 1.79s AC, actually was worse.
My conclusion is that I feel like the time limits were set WAY too tight, and were not super effective at discriminating between O(n) and O(n log n) at all. This problem felt like constant factor hell in C++ and I'm not sure it's even solvable (in n or n log n) in java/python. One of my friends daniel.glabai had O(n) inner loop in java and TLE19'd as well.
Java can pass and it’s fast enough.
https://codeforces.me/contest/2228/submission/374837428
I'm not sure how that runs so fast. Is it the manual input reader?
I'm not sure how everyone else feels, but in general, problems where a correct approach TLEs or runs in very borderline time aren't interesting or fair in my opinion. It seems like many people with valid O(n) or O(n log n) approaches get TLE. What was gained by using 2 million instead of 1 million, or even 200k (I don't think there is any sqrt decomposition that needed to be killed)?
PS: I hope you're not taking the negative feedback too personally. I am aware of and deeply respect the insane amount of effort it takes to write a round. Maybe I should put my money where my mouth is and start testing rounds :p (but I'm not sure if that's something you can just volunteer for)
Yes, some correct approachs are killed, which is my fault. Thank you for understanding.
yeah, i failed with O(nlogn) during contest as well with TLE in TC19. just a tiny bit disappointed as well because O(n) passed in 1.75s which is not super comfortable.
But yeah, good to learn that if n ≤ 2e6, then O(nlogn) might not be a safe option, and if you can think of O(n) approach, you should go with that implementation.
Here is my side by side O(n) and O(nlogn) implementation (commented out the O(nlogn) one): 375001273
Yeah, I think here it was sets being too slow since the fastest solution I had still had overhead from sorting. I guess it makes sense since mergesort is extremely cache friendly (I'm not sure that's the one std::sort uses but theirs is probably even faster) vs lots of address dereferences and cache misses when walking down tree nodes full of pointers.
Can anyone share their soln. to C1? Or it would be wonderful if you could point out the problem in mine..
https://codeforces.me/contest/2228/submission/374822532
I'm not sure where's your problem, but maybe you can make a testgen and run it.
My solution is https://codeforces.me/contest/2228/submission/377282896
It uses the small m and its O(n)
I feel in B there is a mistake for example let's take the case of 6 1 4 3
so now both are at a max possible distance and according to the submitted soln the ans comes out to be 6 but that's incorrect because when x2 moves in whichever direction x1 will move in the same and the suddenly their difference in distance decreased by 2.
so answer should be 4 instead of 6. pls correct me anyone if this is wrong !
Thanks !
You forgot that Remilia can just wait at the same position for the 1st move. That would be optimal choice here.
yeah i got that figured out I had thought of the same answer as editorial but double thoughts on this case + base case I forgot ;(
CirnoNine's solution to E1 asserts that:
Why do these properties hold? Or are these just "well-known" identities of weak compositions? (If it's the latter, any reference?)
Wait I can't really understand question D's second solution, can someone pls explain to me? Thx
hi sir if u have time could you please help me i want to get a good job i just want to ask:- 1. how many problem should i solve a day 2. what to do when i am not able to solve low rated problem 3. any secret for faster progress?
Just do more problems, learn some Data Structures, and actually reason them out and understand what they mean. If you can't solve low rated problems just do more of them. Took me around 200 questions to build intuition for around 800-1200 range. For low rating problem just stack all your free time solving them and try to get them as fast as possible.
Thanks for the advice, The 200-question milestone gives me a realistic target to shoot for. I'll start stacking my free time and grinding the low rated ones to build that speed and intuition.
In 2228B lets say n=7 x1=1 x2=4 and k=2
1st second
-Now Remilia moves to x2=5 from x2=4 optimally, this is the optimal move for Remilia because going to x2=3 will decrease the distance between the two.
-Since Remilia chose x2=5, Reimu will try to minimize distance so he will come to x1=7 from x1=1 optimally.
2nd second -Now Remilia moves to x2=4 from x2=5 optimally.
-Since Remilia chose x2=4, Reimu will try to minimize distance so he will come to x1=6 from x1=7 optimally.
further seconds
Now Remilia cannot move anymore so she will stay at x2=4 and Reimu is at x1=6 and the difference between then is 2
So ans is 2(initial second) + 2(last differene between the two after Remilia is out of moves)=4
But according to this tutorial ans = min(7-(4-1),(4-1))+k = min(4,3)+2 = 3+2 = 5
Why?
Same doubt. But initially, Remilia at x = 4 can also stay there, right? Going to x = 5 will be the same distance as staying at x = 4 initially.
understood Thanks buddy
Remilia is not required to move on every turn. If she doesn't move it doesn't count towards decrementing k so the optimal 1st second choice is to not move. I had the same misread in contest it's not worded well imo.
Yup, so considering staying as the optimal choice, even when we have another choice for moving. So, in the example n=7, x1=1, x2=4, k=2, Remilia initially has two equally good choices before Reimu moves: staying at 4 or moving to 5, since both keep the current distance at 3. So, we should consider staying? right?
yep you should stay because that doesn't use up a k. In general if $$$2x_1-2x_2 \in \{-1, 0, 1 \} \mod n$$$, Remilia should not move on her first turn
Got it
This might be the worst contest of all time, the one that eats up your rating by providing garbage trash test cases, it seemed like intentional and of no use except giving you a -50, makes you feel like trash after the contest and ruins your day, fuckass implementation problems, all the future contest makers should take a learning from this shit
I solved 4 problems, thank you for the competition, I liked it
It may be easier to understand D after discretisation? Because the number of ways to place a dividing line would just be Max — Min. We can sort all points in increasing order of x and then iterate the position of horizontal line. Then the number of valid, distinct ways to place vertical line will be max(0, min(prefMax, suffMax) — max(prefMin, suffMin)). prefMax is the maximum y, after discretisation, of all points on the left of horizontal line, and similarly for suffMax, prefMin, suffMin.
In problem B, Lets say n=4, x1=2, x2=4 and k>0, won't the distance between them reduced by 1? Help me understand this situation.
My interpretation was, as long as the one being chase has a remaining move k yet, the min distance will not decease, only when k becomes zero the distance will start closing in. So in other words the total distance is actually k + the minimum distance.
Yeah that's what I thought. Let me know if u find understand this bit
Liked the problems a lot. Great contest
I hated the constraints in D, you can use O(nlogn) but not so much.
Also missed a very crucial case for C1 and C2, took very long to find. example:
199 30 1 2I hate that I noticed the case where I could be getting the bug and proceeded to fix it incorrectly.Lets not talk about A :(
Can someone explain me in question B if the distance is going to decrease if remelia moves either way then won't the answer be max(D1,D2)-2+k. In the case 4 1 3 1. In first move distance decreases by 2
I thought the same, but look, Remilia is not forced to move every turn. "k" is the number of moves she may use, not the number of turns she must move. So, in your example, she stays there; seeing that, Reimu will move to either 2/4, and from there, Reimilia will move farther for k times, and finally will be caught after 3 seconds.
Got it thanks a lot
Is it only me to pass C by digit DP lol :(
could you tell your way?the way find the string close to array a is so tedious.
does anyone has a more in depth explanation or tutorial of C1 & C2? or a visual explanation? I don't get how we got the longest common prefix of a and b when we still don't know what b is? or are we building b as we read the digits of a from the most significant digit to the least significant digit? In
O(n log a)I assume thenpart is iterating the digits of b? or a? and inlog ais it a binary search? and what are we searching here exactly?yes , the longest common prefix is match a and b from high to low. b can longer or more less to the a,and use the min di or max di get two possible b.if a , b have the same length. we need to from high of a and find the char in d.it use log10 (a) times,log a is mean the digits of the a.and we can use the way of tutorial.the way means find all possible close numeber to a.and write a function to find the closest b.
well I have a doubt regarding problem b in the case suppose if n = 4 and they are in opposite positions ex 1 and 3 and k is 2 so in the first step rameilia skips and reimu goes to 2 and then again rameilia goes to 4 and reimu goes to 3. time is 2 sec till now and in next second she will catch rameilia so ans is 3 in this case but according to k + min(n-abs(x-y),abs(x-y)) comes out to be 4 so am I wrong somewhere in my resoning please help me
the skip didn't comsume the k,so in the third second remeila can move to 1
ok got it thanks
it was impossible to get accepted on D in python :( nobody succeeded
For problem C:
I know the thinking for greedy to solve it, but I really don't want to think. So I use binary search to solve it.
We could calc the k-th smallest digit we can constructe. So it's possible to find the two digit greater and less than
anearest.However, if
d[0]is $$$0$$$, things will become very troublesome. In the end, the competition was not resolved.The problem C sucks.
My answer to Problem C1:
someone explain me more easy approach for this
void solve() { int a,n,d1,d2;
cin>>a>>n>>d1>>d2; if(a==0) cout<<d1<<endl; // if zero print d1 else { int size = to_string(a).length(); int temp = a; int arr[size]; // store a in reverse int index = 0; while(temp>0) { arr[index] = temp%10; temp = temp/10; index++; } bool same = true; // same number can be formed for(int i = 0;i<size;i++) { if(arr[i]==d1 || arr[i]==d2) continue; same = false; break; } if(same==true) cout<<0<<endl; else { // Larger Number Creation int large_arr[size]; // store the larger number comparing with a left to right int ind1 = 0; bool both_d1_and_d2_smaller_then_current_num_so_no_greater_first_diff_formed = false; bool greater_first_diff = false; bool d1_choosen = false; for(int i = size-1;i>=0;i--) { // the large number is already greater then a, so choose small digit for later places if( greater_first_diff == true ) { large_arr[ind1++] = d1; continue; } // if first greater diff has not come and both d1 and d2 are smaller // then the arr[i] -> no larger number of same size can be created // but we can change the last choosen digit if it was d1 if( (arr[i] > d1 && arr[i] > d2) && greater_first_diff == false ) { if( d1_choosen == false ) { both_d1_and_d2_smaller_then_current_num_so_no_greater_first_diff_formed = true; break; } else { int temp_index = ind1 - 1; for(int k = temp_index;k>=0;k--) { if( large_arr[k] == d1 ) { // change the first occurence of d1 large_arr[k] = d2; break; } else large_arr[k] = d1; // all after the changed digit must be small } large_arr[ind1++] = d1; // setting value of current one greater_first_diff = true; } } else if( arr[i] == d2 ) large_arr[ind1++] = d2; else if( arr[i] == d1 ) { large_arr[ind1++] = d1; d1_choosen = true; } else if( arr[i] < d1 && arr[i] < d2 ) { // if both smaller then current digit choose smallest one large_arr[ind1++] = d1; greater_first_diff = true; } else if( arr[i] > d1 && arr[i] < d2 ) { // if d1 < a < d2 -> then choose d2 for greater first difference large_arr[ind1++] = d2; greater_first_diff = true; } } int large; if( both_d1_and_d2_smaller_then_current_num_so_no_greater_first_diff_formed == true ) { // create a number with size+1 digits of repeated small digits(d1) large = 0; bool flag = false; if( d1 == 0 ) flag = true; // if d1 is 0 then only first digit will be d2 rest will still be d1 int lop = size + 1; int y = lop-1; for(int i = 0;i<lop;i++) { int mul = power(10,y); int numb = 0; if(i==0 && flag==true) numb = d2*mul; else numb = d1*mul; large = large + numb; y--; } } else { // else make the number stored in large_arr large = 0; int y = size-1; for(int i = 0;i<size;i++) { int mul = power(10,y); int numb = large_arr[i]*mul; large = large + numb; y--; } } // Smaller Number Creation int small_arr[size]; // store the smaller number comparing with a left to right int ind2 = 0; bool both_d1_and_d2_greater_then_current_num_so_no_smaller_first_diff_formed = false; bool smaller_first_diff = false; bool d2_choosen = false; for(int i = size-1;i>=0;i--) { // the small number is already lesser then a, so choose large digit for later places if( smaller_first_diff == true ) { small_arr[ind2++] = d2; continue; } // if first smaller diff has not come and both d1 and d2 are greater // then the arr[i] -> no smaller number of same size can be created // but we can change the last choosen digit if it was d2 if( (arr[i] < d1 && arr[i] < d2) && smaller_first_diff == false ) { if( d2_choosen == false ) { both_d1_and_d2_greater_then_current_num_so_no_smaller_first_diff_formed = true; break; } else { int temp_index = ind2 - 1; for(int k = temp_index;k>=0;k--) { if( small_arr[k] == d2 ) { // change the first occurence of d2 small_arr[k] = d1; break; } else small_arr[k] = d2; // all after the changed didgit must be large } small_arr[ind2++] = d2; // setting the current digit smaller_first_diff = true; } } else if( arr[i] == d1 ) small_arr[ind2++] = d1; else if( arr[i] == d2 ) { small_arr[ind2++] = d2; d2_choosen = true; } else if( arr[i] > d1 && arr[i] > d2 ) { small_arr[ind2++] = d2; smaller_first_diff = true; } else if( arr[i] > d1 && arr[i] < d2 ) { small_arr[ind2++] = d1; smaller_first_diff = true; } } int small; if( both_d1_and_d2_greater_then_current_num_so_no_smaller_first_diff_formed == true ) { // create a number with size-1 digits of repeated large digits(d2) small = 0; int lop = size - 1; int y = lop-1; for(int i = 0;i<lop;i++) { int mul = power(10,y); int numb = d2*mul; small = small + numb; y--; } } else { // else make the number stored in small_arr small = 0; int y = size-1; for(int i = 0;i<size;i++) { int mul = power(10,y); int numb = small_arr[i]*mul; small = small + numb; y--; } } if( small == 0 && d1 != 0) cout<<large-a<<endl; else cout<<min(large-a,a-small)<<endl; } }}
collapse code pls.
for Problem B If the two paths from Reimu to Remilia have the same length (i.e., the distance is exactly n/2 and n is even), then Remilia cannot make the shortest distance longer by moving. Her best move is actually to stay. So her k moves don't help at all, and the answer should just be that distance, not distance + k. Am I wrong? Thanks.
if the both paths between them has same distance (leaving n=2 and n=3 cases), for that particular moment Remilia will stay but Reimu will move to any one of the two paths to catch her, then Remilia will shift again to make distance of both paths equal (she can do this k times). so eventually answer is k shifting + shortest distance between both (that is n/2 in this case for n even).
Thanks a lot! I was totally reading k as a time limit at first, but now it makes sense — it's the total moves she gets. Your example really cleared it up for me. Appreciate the help!
In B
if diff1 and diff2 are same then why answer is not k + diff — 1
cause for 1st sec --> Remilia will not move
cause if she moves ---> new diff will become worse
so she wont move then --> diff--
then normal operations again !!
what did i missed then ???
case n=3,2
374796655
But I understood It was given k moves
I mis counted it when diff1 == diff2 I thought one of the k moves is being used
SDSXC what is dep in your solution to F?
dep(u) represents the distance from node u to the root(node 1)
Hi, could you please elaborate on the O(n) solution for problem D?
I understand the O(n log n) idea, and I can imagine getting O(n) if we use counting sort because coordinates are in [1, n]. But I do not understand the intended O(n) observation from the editorial.
Specifically:
What exactly are "the two groups determined by the smaller y-coordinate"?
What does it mean that a valid dividing line must cross one of these groups?
What point are we enumerating?
What is the "appropriate pre-computation"? Which values should be precomputed?
How do these precomputed values give the feasible vertical dividing position x?
You can implement this in O(n) with count sort but here is my idea to visualise better and has O(nlogn) complexity. Sort all the points based on their x co-ordinate. Now, imagine there is a set "Y" that contains all the y co-ordinates from the n points. Now, we iterate over all the points in sorted order and when we see that the current points' x co-ordinate is greater than the last one, that means, we can draw a vertical line between current and previous point.
Vertical Line image
Now, we will add the number of ways we can add a horizontal line for this vertical line to the answer. To calculate number of horizontal lines, we keep two multisets y1 and y2. y1 = multiset of y co-ordinate to the right of our vertical line y2 = multiset of y co-ordinate to the left of our vertical line Initially y2 = all y co-ordinates, y1 = empty When we move to points, we move its y co-ordinate from y2 to y1
Now, for calculating number of horizontal lines, let's take a cut at y=a, then y1 will be divided into 2 parts, all points less than 'a' and all points greater than 'a'. Lets call these as left and right part of y1. Similarly for y2. We need to choose 'a' such that the left and right parts of y1 and y2 are not empty (to make sure that no quadrant is empty).
Example: y1 = [1, 2, 5, 8], y2 = [3, 6, 7], Y = [1, 2, 3, 5, 6, 7, 8] In this example, we need to make cuts in Y. So if we make cut between 2 and 3 (a = 2.5), y2 will be cut such that its left part if empty ([] and [3, 6, 7]). If we make cut between 3 and 5, y1 cuts as [[1, 2] and [5, 8]] and y2 cuts like this [[3], [6, 7]]. basically element ≤ 3 will be in first part, and elements ≥ 5 will be in other part and we want both parts to have atleast one element to make sure all quadrants have some points In above example, the quadrants will have following y co-ordinates: 1st [1 and 2] 2nd [5 and 8] 3rd [3] 4th [6 and 7]
Lets call e = max(min(y1), min(y2)) and f = min(max(y1), max(y2)). To achieve the above, we need to make cuts in Y between e and f. Let: pos[x] = position of x in set Y. Number of possible cuts = pos[f] — pos[e] We add this to answer.
Here is my O(nlogn) submission: 374843238
Bro, I solved this problem in O(n). But, I always try to understand and learn from editorial. And, guess what? I'm trying to understand Solution 2 from last 30 minutes but literally can't get a single word of this solution. I think, this is the most terrible solution of a problem I've ever seen.
For C1/C2, even the $$$10^{17}$$$ seems too much. In some cases, you need to take a number which has more digits than $$$a$$$, which leads the numbers go above the upper bound for
long long. My solution passed after finally using__int128_t. This affected my score very badly. I got 10 unsuccessful submissions before realizinglong longwasn't enough. For a Div. 2 C1/C2, requiring__int128_tjust because of unexpected intermediate overflows feels incredibly misleading.I also used __int128, but later realized that the only case when __int128 is needed is a = exactly 10^17 and only when trying to use b composed of 18 digits. However, if a = exactly 10^17, using 18 digits for b is never optimal, because, for example, using a 16 digits (or even 1 digit) b is clearly better. Thus, to avoid __int128, just handling "a = 10^17" separately (skipping 18 digits attempt) should be enough.
Is it only me or did C1 feel quite more difficult compared to other Div 2 contests?
Ckilled me... but please share code forC, I would love to see if there is a simpler way to code it.also is there another approach which can tackle C1 but maybe not C2 ?
I have simpler approach. I test every possible more optimal number. I found them with a testgen.
My first "greedy" for C1 was to get the maximum and then only minimums and then the reverse. It turn out wrong so I listed all possible optimal variants.
It's
O(n+m^2)Code: https://codeforces.me/contest/2228/submission/377286323
If you want I can share testgen.
This is such an excellent contest! I love it(only except the problem D with a tight time limit).
can someone explain to me in detail about the problem D im a bit of confused and didnt get clear about that :( TMT
You will need drawing board to understand this better. Check this out: https://codeforces.me/blog/entry/153619?#comment-1365644
I thought you were supposed to brute force c1? since per test case n=2 gives you about 2^17+2^18+2^19 operations. (brute forcing on the length of b, from a.size-1 to a.size+1. wasn't that the point?
horrible to put such difficult to calculate in E,and meaningless to divide it E1,E2
Is there code for juan_123's solution for Problem F? If so, please link me to it, can't find it in juan_123's submissions.
Is the "first solution" of F an intended solution? I was barely able to pass (2800ms) and that is with some optimizations. Edit: nvm optimizing to 1 log is trivial.
i struggled very hard to solve problems and i solved 5 out of 8. previously i got a rating of 744 now it became zero(unrated in my account) why i don't know.
Guys, I solved problem D in O(n) time complexity. 377025802. After that, I'm trying to understand the editorial of this problem. Solution 1 is very clear. But, I'm not getting anything of Solution 2. Can anyone explain Solution 2 properly?
For B, if the distance between the two is n/2 exactly when n is even, then would it not be optimal for x2 to not move initially?