Thanks everybody for participating in the round!
Div2A. Blocked
Author: misteg168 Preparation: misteg168
Hint1
Solution
Code
Div2B. OIE excursion
Author: danx Preparation: danx
Solution
Code
Div1A Grid-L
Author: rlidon2006 Preparation: misteg168
Hint1
Hint2 (strong spoiler)
Hint3
Solution
Code
Div1B Unique values
Author: misteg168 Preparation: misteg168
Solution B1
Code
Solution B2
Code
Div1C
Author: misteg168 Preparation: misteg168
Hint1
Solution
Code
Div1D. MEX Replacement on Tree
Author: Misuki Preparation: Misuki
Hint1
Hint2
Hint3
Hint4
Hint5
Hint6
Solution
Code
DivE. Weird Chessboard
Author: rlidon2006 Preparation: misteg168, rlidon2006
Hint1
Hint2
Hint3
Hint4
Hint5
Hint6
Hint7
Hint8
Hint9
Hint10
Solution
Code








Auto comment: topic has been updated by misteg168 (previous revision, new revision, compare).
Auto comment: topic has been updated by misteg168 (previous revision, new revision, compare).
Nice Contest!
I created video editorial for E. Coloring a Red Black Tree.
371035963 Is this the cleanest way to write Div1 B2 :) ?
Nice Contest
Auto comment: topic has been updated by misteg168 (previous revision, new revision, compare).
i think i did exactly" If there are two equal elements in a , one of them will be blocked. Otherwise, sort a decreasing."
still didn't worked
nevermind i am just dumb, i fucking solved it lexicographically instead of "integer order"
the correct solution with AC is
[deleted]
[deleted]
Contestant so goated (HE IS ACTUALLY THE GOAT) that his contest submission is used as the formal code for solution E. Though, would love to have the author's code for problem E added as well :)
Here you go:
Note this is very optimized for speed, you don't actually need to optimize the constant this much to get AC.
Thanks!
Auto comment: topic has been updated by misteg168 (previous revision, new revision, compare).
Auto comment: topic has been updated by misteg168 (previous revision, new revision, compare).
It turns out this greedy implementation for D1C passes. Does anyone have an idea why?
https://codeforces.me/contest/2219/submission/371036202
Me too bro
Assuming you understand the elements of the solution (you should pick some order to go through the nodes in, and then for each keep picking it until it turns red), here:
Consider you have two neighboring nodes $$$u$$$ and $$$v$$$ that are used one after the other in the order with red neighbor ratios $$$\frac{a}{b}$$$ and $$$\frac{c}{d}$$$ respectively (at the time that they're used). Then, the expected value here is $$$\frac{b}{a} + \frac{d}{c + 1}$$$. If you swapped their order, the expected value here is $$$\frac{d}{c} + \frac{b}{a + 1}$$$. So, it's beneficial to swap if $$$\frac{d}{c(c+1)}$$$ is bigger than $$$\frac{b}{a(a+1)}$$$.
Note that this is a comparator based on the nodes themselves! So, at any given step, if the node with the minimum value of $$$\frac{b}{a(a+1)}$$$ is at some later point in the projected order, you can keep bubbling it up earlier and never lose (note that if two nodes didn't neighbor, swapping them in the order doesn't matter).
Maybe not the most formal but yeah roughly this exchange argument is the reason.
"(note that if two nodes didn't neighbor, swapping them in the order doesn't matter)" could you explain that a bit more, please? I'm having trouble visualizing the case:
R-1-2-3-R, where R is a Red node.
1 and 3 don't neighbour, but isn't there a possibility that even though 1 is better than 3 at first, choosing 3->2->1 is better than 1->2->3 or 1->3->2? (Considering 1, 2 and 3 may have other neighbours besides the ones drawn)
UPD: I found out why it works
For any black node $$$u$$$ with degree $$$\deg(u)$$$ and $$$cnt(u)$$$ red neighbors, the probability of selecting a red neighbor in one operation is $$$\frac{cnt(u)}{\deg(u)}$$$.
$$$\therefore$$$ the expected number of operations to color node $$$u$$$ red is:
Suppose you have two adjacent black nodes, $$$u$$$ and $$$v$$$. If you color one, it becomes a red neighbor to the other, increasing the other's $$$cnt$$$ by $$$1$$$ and reducing its expected cost.
Let's compare the total expected cost of the two possible color orderings:
To minimize the expected operations, we must order $$$cost(u,v)$$$ and $$$cost(v,u)$$$ (exchange argument):
Rearranging:
Some more rearranging and simplifying:
Use this as a heap (priority queue) comparator
how tf tourist hardcoded div1E ive seen him many times hardcoding unlike other top coders
Surprisingly appeared in his dreams!!
He might have used any generator or something similar with intended logic, ran it locally and got the values. What else can be the reason? And everyone has their own coding style. We can see people become LGMs with different approaches, not something one should focus on.
Yeah, and your llm coding style is funny too! Which we probably should focus on
Doesn't matter to me! I don't have any ulterior motive with my rating (unlike others). I would probably defend it, but it's too futile. You might have your own reasons, let CF decide that. Thanking you for your kind words.
It is not "hardcoding", he seems to be using the same fractal as the official solution. (We run the visualizer)
haha i was wondering the same
what happened to u today bro, why didnt u solve the contest after B??
i am no more into coding am pursuing an altogether different stream(humanities) so i just code for the sake of refreshment in my breaks and to maintain that streak thats it the moment i get even a little discomfort in thinking i just quit ill be participating in icpc thats it for the sake of passion that i previously had nth more than that
Wow... I realised the trick for Div2B with m = 2 but I didn't realise it would work for m > 2... Is it still possible to simulate Div2B? I tried with some kind of method but it kept getting WA2
Auto comment: topic has been updated by misteg168 (previous revision, new revision, compare).
I solved Div2C problem in a way that I can't even imagine why it's correct. 371022604
what was your approach can you elaborate a bit?
Since geometry isn't my strongest suit, I used a constructive approach instead of a pure mathematical one. My approach is based on the observation that if we have enough L-pieces, we can always form a base $$$n \times n$$$ grid. From there, I expanded the grid into an $$$n \times m$$$ rectangle by adding columns.
Each additional column of height $$$n$$$ requires $$$n$$$ L-shaped pieces and $$$1$$$ unit segment (
_). If I have an excess of unit segments, I can perform an "exchange": 2 unit segments can be replaced by 1 L-piece at any corner of the grid.To find the balance, I solved the equation for $$$k$$$ (the number of exchanges): $$$n \cdot (P - 2k) = Q + k$$$ $$$\Rightarrow k = \frac{n \cdot P - Q}{2n + 1}$$$
I simply iterated through all possible $$$n$$$ up to $$$\sqrt{S}$$$ and checked if a valid $$$m$$$ exists that satisfies the total segment count.
A, B, C were great but clearly today was not my day, misread the question in A, made a silly error in B, and panicked in C which resulted in me ignoring a obvious condition, end result being 0 solves.
Hint 2 of D1A should have if and only if instead if an oly if. Just a small typo :)
Done, thanks!
Auto comment: topic has been updated by misteg168 (previous revision, new revision, compare).
Could anyone please explain Div2C's solution. I'm having a hard time understanding it.
For any answer to exist, it must satisfy the following 2 conditions :
1. A valid $$$n \cdot m$$$ matrix should exist.
2. It must be possible to arrange these p (segments) & q (L-shapes), to cover all edges of the $$$n \cdot m$$$ matrix.
For condition-1, for a $$$n \cdot m$$$ matrix to exists, then its total edges must be equal to $$$p + 2 \cdot q$$$.
Now let,
$$$E_h$$$ = Total horizontal edges of $$$n \cdot m$$$ matrix
$$$E_v$$$ = Total vertical edges of $$$n \cdot m$$$ matrix
Then,
Here, it can be proven that :
Now let n <= m (for ease of understanding), then we can iterate over all n in sqrt(p + 2 ⋅ q) time. Then for FIXED p, q and n we can easily find m as,
Hence, we get our possible (n x m) candidate dimension for given p and q.
Now, how to check if this dimension is correct or not? For that lets analyze the edge difference of the matrix :
If the difference is non-zero, then it can only be obtained by using straight segments only.
Cause using a L-shape increases both horizontal and vertical edge count by +1, no matter what orientation of it was used. But using a straight segment affects only one type of edge, either horizontal or vertical i.e. this difference gap could only be achieved by using straight segments only.
So, from here we get our necessity condition :
Let,
$P_h$ = Total horizontal edges using only straight segment
$$$P_v$$$ = Total vertical edges using only straight segment
Also, since the difference is caused only by straight segments only, we get,
Also,
Adding these two equations we get,
This means p and |m-n| MUST have the same parity.
So, the remaining p's must be even numbered. Hence, these remaining segments come in pairs (1 horizontal + 1 vertical) and can be used as L-shape. See the editorials explicit construction for its sufficiency.
Hope this helps :)
TYSM for this wonderful contest that promoted me to pupil!!! :)
For the sufficiency proof of div2C why can we assume that $$$p = m - n$$$? and why $$$p, q$$$ that satisfies the constraints will always give the construction in solution?
I looked at it like this:
1. You can always construct an $$$ m * m$$$ grid with $$$2*m*(m+1)$$$ $$$ L-shapes$$$ perfectly.(You first make the diagonal, then set all the pieces in the upper and lower triangle facing inward.)
2. Now WLOG if you increase one side to $$$m+1$$$, then you can place all the new pieces such that only 1 line segment is missing, if you only try to increase 1 side of the grid. Then you can generalise this for any $$$m $$$ and $$$ n$$$.
sqrt is too fast in D1A, i couldn't see anyone doing the bonus... what's the time complexity? should an A2 be made?
My approach was a bit different for C ,atleast the condition to check.
L-shape contributes 1 horizontal edge and one vertical edge no matter how we place it.Total number of horizontal edges is (m+1)*n and vertical edges is (n+1)*m
therefore q >= min(m*(n+1),n*(m+1)) = min(m,n) + m*n ----------- 1
also this condition is sufficient to check as then you can use the construction in the editorial .
for implementation i saw that E = p+2*q , where E is the number of edges in the grid also E = 2*n*m + n + m It can be seen that (2*E+1) = (2*m+1)*(2*n+1) = d*e
so basically we have to check odd divisors of 2E+1 say d,e
just set n = (d-1)/2 and m = (e-1)/2 and check the condition 1 if no pair satisfies return -1
code
with fast factorization p,q 1e18 is doable with the original constraints
thank you
same idea
in the third line for the condition check , ig it should be q <= min(n,m)+m*n, as the number of horizontal or vertical edges provided by L-shape cannot exceed the ones in the grid.
div1A illustration should say n=6 not n=7
we are having no contests until April 23rd, damn...
can sb pls explain for div2b how the answer for the following test case, is yes?
there is nothing you can do in the first second?! imo the question has to have two conditions :
you are not starting on the first element , you are starting before the first element. AND
you can either stay or move right or left so you can just wait.
ooooooohh ok thank you
does any body have an answer for the extra in div1A div2C ?
I think my solution is faster, although I am not 100% sure.
https://codeforces.me/contest/2219/submission/371142745
inc increases linearly so sum of inc increases quadratically. it's not any faster...
div2 B and Cis too hard
Let $$$k = p + 2q$$$, and consider the equation
Thus, we may factorise $2k+1$ by
using the General Number Field Sieveprecomputing all primes up to $$$\sqrt{2k+1}$$$, then checking whether each of them is a factor of $$$2k+1$$$. From there, we can iterate over all possible values of $$$(2n+1,2m+1)$$$, and check whether they satisfy $$$|m-n| \leq p$$$.Time complexity: $$$O(\sqrt{k} \log \log k + \frac{t \sqrt{k}} {\log k})$$$ (there are $$$O(\sqrt {k} / \log {k})$$$ primes less than $$$\sqrt {2k + 1}$$$ by the Prime Number Theorem).
I have written the craziest solution for C lol, took so much time optimizing finding patterns and what not, just wanted to share it over here if anyone is interested.
https://codeforces.me/contest/2219/submission/371142745
Problem C was interesting, thanks.
TETOOOOOOOOOOOOOOOOOOOOO
YEahhhh
Interesting problems, thanks!
https://judge.yosupo.jp/problem/factorize for this, 100 cases is about 0.1s* if p,q<=3e17, and the total number of divisors is up to about 1e7 in all cases, so doing recursion would iterate through 5e6 divisors which is doable in 1s. Squfof is probably also doable, but seems like everyone does rho instead. 1e18 is probably also doable with the same method.
*depends on implementation
Editorial isnt linked to the div 2 round
Best approach for C (DIV1 A)
An expended solution of div.1 B / div.2 D .
https://codeforces.me/contest/2219/submission/371894684 Does anyone know why such construction method works? I found this totally acceptable by accident (and luck of course)
For question C, would the pattern consisting of two L shaped pieces placed together to form a square be a valid combination? Im not sure if this is made clear in the question