A Sequence Game
Idea from cmk666, Prepared by cmk666
B Even Modulo Pair
Idea from 244mhq, Prepared by NetSpeed1
C Dungeon
Idea from Link_Cut_qwq, Prepared by Xiaohuba
D Copy String
Idea from JoesSR, Prepared by JoesSR
E Journey
Idea from Link_Cut_qwq, Prepared by Xiaohuba
F1/F2 Chain Prefix Rank
Idea from JoesSR, Prepared by JoesSR
G Pointless Machine
Idea from Daniel777, Prepared by zjy2008
H PalindromePalindrome
Idea from zjy2008, Prepared by zjy2008
UPD: Unfortunately, the original analysis regarding the number of "relevant" pairs of disjoint substrings was incorrect. The proof was derived from an online blog concerning the range distinct palindrome substring query problem, but that proof turned out to be flawed. Consequently, we previously believed the complexity of the standard solution to be $$$O((n + q) \log n)$$$; however, after the contest, we discovered that it is actually $$$\Theta(n \log^2 n + q \log n)$$$ (and this lower bound is reachable). We apologize for any confusion caused.








first.
GuessForces
Thanks for problem B, quite an interesting idea!
But I think, if there is only one even number, there is no need to check if we can make an answer of
(x,y)wherey > xandxis even, because ify = kx + r, andxis even, thenkxis even, and since we know thatyis odd, thenrwill be odd therefore(x,y)is not an answer in this case.I know number say otherwise(based on submissions)... but anyone felt D easier than C ?... I solved D under 40 minutes, but spent more than 90 minutes, in C :(
But I think D is the hardest in A~E. C is a traditional greedy problem I think.
Yeah C is a traditional greedy problem..Just 2-3 observations..
same, I think D is easier than C, also C is easier than B I spent 2 hours in B but solve C and D in 20 min
As a Master, I spent as much as 45 minutes on B, which was close to or even exceeded the time I spent on E.
Damm, now I feel less stupid
Same thing here, i was literally confused at B and solve it after C (going C before B is actually a good idea i got from Pinely Round 5, where B is harder than C) Kind of surprised how "easy" D is (it is easier than usual Div.2 D imo), and solved it after 45 mins. My implementation is kind of simpler than the editorial said, though they have the same idea.
Exactly, the two latest content have this issue. Codeforces contest is got more similar to ICPC :)
I think C is harder to get the idea, and D is harder to write the code.
I think that D is the hardest in ABCD
Same bro , though upsolved D
so B is just...brute force? uhhh man
proving the complexity of brute force is challenging :(
Why?
I notices if x is included, then x + n < 2*x, st. n is odd, is always included, then the x cover all val > x, which are at even distance from x, and x + n covers all val > x which are at odd distance from x. So, for all integers > x is exhausted when such a sequence is found.
if x is odd and x + n, st. n is remainder, n is odd, then x + n is even(odd + odd), and even can occur only once, therefore I have to search in 2*x + n(st n is odd), this keeps on happening inductively.
If x, x + n, y, occur in the increasing sequence, y > 2*x due to the above proof, therefore the number of possible odd consecutive modulus pairs get exhausted as the ranges lowerbound is increasing exponentialy.
Either way you increase lowerbound by 2 every (<3) numbers, making it terminate with n < log2(10^9).
B is not "just" brute force, the important observation is about bounding the naive solution. If your takeaway was that it was "just" brute force, then you are missing the point.
yea I get it, what I meant to say is that i just never for a moment considered brute forcing an apparent n^2 search due to pure habit...I am just disappointed at myself for that, and this problem taught me a real lesson.
If you want to try a problem with a similar observation (the naive solution being fast) you can go check out D from the recent Div4: https://mirror.codeforces.com/contest/2167/problem/D
Although knowing ahead of time that this is the idea is a big spoiler, maybe it is still helpful to improve.
thanks for being so helpful! I'll check that problem out:)
B is a pretty good problem -- very tight mathematical observation. I had a vague feeling about bounding during the contest but could not formalize it; had since been working entirely in the wrong direction (and spent more 2+ hours..)
Anyways, does considering the "special case" where y < 2 * x comes naturally? It is natural in hindsight but feels difficult to perceive during the contest. I wonder if similar ideas had shown up frequently in CP? (i.e., considering a manageable special case and that special case must appear; otherwise the constraints on the input will be violated).
it does come naturally. if x,y are odd. y=x*q+r;
r will be even when parity of floor(y/x) is same as y. Considering you don't want to craft a possible solution. You will make floor(y/x) as even. Considering smallest even number i.e 2.You can at make make an array of size 31. Hence size is bounded.
can you please explain the complexity of B or suggest anything to understand this? i still couldn't find how an n^2 with this constraints!
problem B is awesome. problem G is genius. problem F is nice (though i have no idea how to come up with a solution for F1 which does not solve F2 by just adding some data structure). finally, sorry for being negative but... problem E..? it is so... obscure... the problem does not feel natural at all. i don't understand why in this problem the cost of a path is defined as it is. making it just the maximum edge on the path would make the problem just slightly easier (except if i don't see some obvious solution) but the problem would make soooo much more sense. and i would even call it a great problem. but the real problem statement just broke my brain.
P.S. also, expression $$$w_{\max_{i=1}^k e_i}$$$ does not make any sense because we can't use an edge as a subscript. it should be $$$w_{\textsf{argmax}_{i=1}^k e_i}$$$
https://mirror.codeforces.com/contest/2164/submission/347783243
can you help me in finding why my code fails in the 5th test case.
May be because: whenever you are incrementing the j to j + 2, you should also reset the t = odd[i] for the next iteration of j. now it is like odd[i] * 1 * 3 * 5 * 7...... which may not be right, i think !
For E, I guess having the cost of the path defined in that way forces you to actually construct the reconstruction tree and propagate values downward instead of being able to do everything in one pass. It was educational for me since I haven't done many reconstruction tree problems (please recommend any good ones!) but I do agree that the cost definition felt contrived and took away from the beauty of the problem.
Great problems.
During contest, I came up with a solution that is really hard to implement on F2, and finally completed it after contest.
Let $$$F_{i,j}$$$ denote the number of nodes in the sub-tree of $$$i$$$ that is greater than a total of $$$j$$$ nodes on the path of $$$1$$$ to $$$fa_i$$$. Then the answer can be represented as $$$ans = \prod_{p, j} \binom{F_{s_{p, 1}, j} + F_{s_{p, 2}, j} + \dots}{F_{s_{p, 1}, j} , F_{s_{p, 2}, j} , \dots}$$$ where $$$s_{p, x}$$$ are childs of $$$p$$$ and $$$\binom{a_1 + a_2 + \dots + a_n}{a_1, a_2, \dots, a_n} = \frac{(\sum a_i)!}{\prod a_i!}$$$.
Using dsu on tree and some data structure to maintain $$$F$$$ gives us a solution of $$$\Theta(n \log^2 n)$$$.
Code: 347782283
I had a pretty similar idea, but the way I optimized it is by noticing that the only time a factorial for some node in the numerator and a factorial for its parent in the denominator aren't the same is in the "interval" that adding this node splits(for example, if the root has value $$$a$$$ then the relevant intervals for the root are $$$(0,a)$$$ and $$$(a,n+1)$$$, and if one of its children has value $$$b \gt a$$$ then the relevant intervals for that child are $$$(0,a)$$$, $$$(a,b)$$$ and $$$(b,n+1)$$$; it split $$$(a,n+1)$$$ into $$$(a,b)$$$ and $$$(b,n+1)$$$); so, every node other than the root, if in its subtree $$$l$$$ nodes are on the left side of the split and $$$r$$$ are on the right side, multiplies the answer by $$$\frac{l!r!}{(l+r+1)!}$$$, and the root multiplies the answer by $$$l!r!$$$.
is there any other solution for problem b
Yes, you just brute force
https://mirror.codeforces.com/contest/2164/submission/347898913
Upload codes
For B,I came up with a pretty interesting solution. We store all number in a map with key as the largest power of 2 which is smaller or equal to number. If one power has more than 2 values associated with it then answer will be just 2 of those 3 with same parity of remainder with the key, other a brute force solution will work because almost 64 elements can be there with no kay having more than 2 values by pigeon hole principal.
this contest was hard for me !
i fell a little too much, but for C i didnt understand why using the largest damage sword is not good for stage 1 ?
i mean if we use the greatest damage say z , and there are swords with x<z and y<z damage
then at the end after all the nonzero-c monsters are evaluated, we will have sword with either damage z or >z
and we will also be left with other n-1 swords in vector a
this is my submission 347754985
Oh, you missed one point here. You can obviously eliminate non-zero C monsters with the help of the largest damage sword, and you'll end up gaining a sword with damage max(x, max C of eliminated monsters). But you lost more power swords you could have collected in this process. For example, suppose initially you have swords with power 1, 1, 1, 3, and you have non-zero C monsters with C values 4, 4, 4, 4, and their respective health values are 1, 1, 1, 1. If you kill all those monsters with the largest damage sword, then finally you'll have 1, 1, 1, 4 for killing zero C monsters. But if you had used smaller damage swords also, then you could have swords with values 4, 4, 4, 4 to kill zero C monsters. I hope you get it. So ideally you should always use lowest possible damage sword to kill any moster to get maximum profit.
oh thanks a lot , understood!
Because you might need to "upgrade" worse sword.
Lets say input
1
2 3
1 6
1 5 6
5 0 0
If you use your 6 sword for the (1, 5) monster, then it does keep its level 6. But you can only kill one more monster after with it. If you kill (1, 5) with damage 1, your sword gets upgraded to 5 and then you can kill both other monsters — one with each sword.
got it , thanks a lot!
When did codeforces add visualizers? This is so useful! Will this be the standard going forward?
https://mirror.codeforces.com/assets/contests/2164/E_ooxathoosh9Oob4quoiv.html
I believe they existed in the last Global Round as well. Maybe they are a thing in all Global Rounds?
Please anyone tell me why my code is not working on problem C
Admittedly, I had never heard of a reconstruction tree until several people mentioned it to me after the round. But during contest I came up with a solution to E that does not use KRT or anything of the sort; in fact, I believe there are several such solutions. So, for other people who aren't familiar with KRT, here is a quick sketch of my solution. (It's actually probably very similar or equivalent to the editorial solution behind the scenes, but framed without a reconstruction tree.)
Like in the editorial, it is clear that you need an Eulerian circuit, and it is known that one exists if and only if all vertices have even degree. So we will add virtual edges between the odd degree vertices, then take the sum of the weights of all of the original edges and virtual edges.
Let $$$G_i$$$ denote the graph that contains all of the vertices, and the first $$$i$$$ edges. Then if $$$u$$$ and $$$v$$$ belong to the same connected component as $$$i$$$ in $$$G_i$$$, it is clear that we may add a virtual edge between $$$u$$$ and $$$v$$$ which has weight $$$w_i$$$. Furthermore, it's easy to see that this is a complete characterization of virtual edges we can add.
We have this notion that somehow only "suffix mins" of $$$w$$$ seem to be relevant, because for $$$i \lt j$$$, we have that $$$G_i$$$ is a subgraph of $$$G_j$$$, so if $$$w_j$$$ is smaller than $$$w_i$$$, we would like to just use this instead of $$$w_i$$$. However, this isn't quite true, because it might be the case that we want to connect two vertices in a different connected component from $$$j$$$ in $$$G_j$$$. So we introduce the notion of "connected component suffix mins," which consist of edges such that no lighter edge later gets directly connected to its connected component. (It's okay if a lighter edge gets indirectly connected, since remember that we are only adding virtual edges of weight $$$w_i$$$ when looking at graph $$$G_i$$$, and the stuff that gets indirectly connected have labels less than $$$i$$$.) Now, it is easy to see that these are the only relevant edges, because all other virtual edges can be exchanged for one with the weight of a relevant edge. Furthermore, we retain the nice property of monotonicity of suffix mins; that is, if you fix a particular pair of vertices, at some point they end up in the same connected component, and from then on, its connected component's suffix min is monotonically increasing.
We can find all of the connected component suffix mins by merging them small-to-large while we add edges one by one in index order via DSU. In other words, for each connected component, maintain a set of its relevant edges. Then suppose we are adding edge $$$i=\{u, v\}$$$. We merge the sets of relevant edges for $$$\texttt{find}(u)$$$ and $$$\texttt{find}(v)$$$, delete all elements with larger weights than $$$w_i$$$ (because they're not suffix mins), and insert edge $$$i$$$.
Now, we create a second DSU, where we insert the edges one by one in index order, while also maintaining the number of odd degree vertices in each connected component. Whenever we add a relevant edge $$$i$$$, if its connected component has two or more odd degree vertices, we can pair them off until there is at most one, adding an edge of weight $$$w_i$$$ for each pair. By the monotonicity property, it is clear that this is optimal, thus concluding the solution.
In fact, what you did is basically the same as the Kruskal Reconstruction Tree but done in a non-explicit way. The first dsu you did is the process of building the tree and updating the minimum costs, and the second one is the process of finding the answer from bottom to top in the reconstruction tree.
I think the dsu solution to this problem based on enumerating over the edges by their ID is just doing the same thing as KRT. It's just u did the same greedy while invisibly building that tree structure. The KRT sol. is just kinda making the dsu sol.'s greedy updating procedure offline.
In problem B, We just have to run a O(n^2) loop to check if there is a pair exist or not....(no need to check any other cases like 1 even / multiple even).....
yessir
Why doesnt it TLE?
you just have to return/break whenever you find the ans... here is the Submission Link
Reason:
For making a -1 case
A1 < 2*A2 < 4*A3 < 8*A4 < 16*A5.....
so the -1 case can go at most array with length 30 as 2^30 > 10^9
can you give a more specific explanation? (why is it a1<2*a2<...)?
Because we have two integers x and y ( x < y ) if 2 * x > y , then y % x = y — x and the difference between x and y, is even if they are odd .
Sorry for my English xD
Edit: i forget explain why to this
To ensure that this condition is not met, we must place the following element of the array a integer y > 2 * x , and this increases exponentially
But what if x and y have different parity? Then y-x is odd?
Still that would just increase the iteration by maybe 1 or 2 because if there are 2 evens then no need to check for anything, so we are only solving 1 or 0 evens.Just run this loop and you will get the answer before index 30.
But he said that we don't need to check for cases 1 even / multiple evens. (Siyam Talukder)
If lets say we have a tc where a1 is even, a2 is odd, a3 is even, a4 is odd. Still if you run n^2 it will pass because there are two even which will end making a3%a1 even. So even if we don't check it specifically it still gets covered in the brute force.
really thanks you!
basically u can think that if any two odd numbers are taken then if a<2b then a%b ==0 as per the euclid lemma..
now consider no two numbers satify the conditions then clearly 2^n a0 = an
solving for n you get a easy bound which can be accepted in n^2..
MF what are $$$f_i$$$ and $$$f_{a_i}$$$?
$$$f_i$$$ means the minimum weight among every edge that is i or i's ancestor.
Alright, what does this relation mean then?
You may refer to the updated tutorial for better understanding.
Thanks homie.
why did you said MF bruh
I had no idea how to solve problem B without bruteforce. But after reading the editorial, I got goosebumps. It was nice and interesting problem
B was harder than C
As soon as I read the statement of C, I completely forgot that we only need to take max(c[i], x). I ended up spending 1.5 hours trying to solve an extended version of the problem where you only get a sword of strength c[i] after defeating the i-th monster. Totally wasted effort, lol. If anyone actually solved that “unwanted extended version,” I’d love to see your solution!
Did the same LOL! T_T Wasted around 30-45 min in the same but glad that i ended up seeing the "real" question.
Same here :).
this was very difficult so so very difficult I have only do 1 problem
can anyone explain the problem H properly .
B is such a good question. I came up with the approach after 2 hours. I want to share my first solution which gave me TLE.
I came up with following conclusion, since we need $$$y\mod x$$$ to be even. Then $$$r=y-x*d$$$ which is remainder should be even. For that if we have 2 even numbers then it solves that case. But for odd case, I went like this:
find_l_rfunction, this function is just finding the leftmost index $$$l$$$ such that $$$odd_l \gt = low$$$ and finding rightmost $$$r$$$ such that $$$odd_r \lt = high$$$:lucky -138
same
can someone tell why my code is failing for C
347839121
I think your logic is wrong for this problem.
you could miss some cases where:
some weaker sword could kill a monster that your current sword skips.
You might upgrade one sword unnecessarily and miss a better kill with another sword.
You can see my code for this:
https://mirror.codeforces.com/contest/2164/submission/347748229
yeah i did realise that later , i was just upgrading one sword , wheni. could upgrade multiple to get maximum in c==0 case
At problem E ,should the graph we build be connected ? I mean the one which has all the odd nodes ? And also if someone could explain the solution a bit? I don't understand how we take the optimal edges.
Like how I understand it we build the krt first then every node that has odd degree we put it as 1 and when we go up if we have t nodes in our subtree we pair them and consider them as even from now on?
I'll explain an easy solution. First note that the number of odd degree nodes in a connected graph is always even, so in the final graph, all nodes can be matched. Now lets build the orignal graph step by step adding edges in order. At some point, before adding $i$th edge, lets suppose you have the number of odd degree vertex in each component stored. Note that we just need to pair these odd degree vertices with other vertices in the same component. So we also maintain all the weights of the edges used to connect these vertices.
When adding an edge, we can always take a random stroll to this edge, add this to our path and join two odd deg vertices in these components. Since this edge has the highest index among all the edges, the path value would be the weight of this edge. So for all the pairs we had made in the components, we can use this edge too. So we just remove the ones which were previously joined using an edge with higher weight. All we need is to simulate this. Here is my submission for reference 347799843
In problem B, If there is no valid pair of x and y. Then why doesn't it give TLE as the time complexcity is O(n^2) ?
you can stop the loop after index 40 maybe no need to run the whole loop.
Problem H Part II proof 2 isn't making sense to me, does anyone understand it?
zjy2008
Separately, for part I, I believe "The border of a palindrome prefix" and "The border of a palindrome suffix" should both be replaced with "border of a palindrome substring," or else it would fail on a string like abb...ba.
The author said the solution is translated to English using a translator and it seems that we meet some translation issue. A correct version will be posted soon. Sorry for the inconvenience.
Could you also send a link to the Chinese version of the solution?
and I think Problem H Part II proof 1 can only lead to $$$O(n\log n)$$$ pairs of useful palindromic substrings
Could you update the translation, or give a Chinese ver of the editorial?thanks :(
I think "very soon" has already passed :(
upd: the correct version has been posted, sorry for the delay.
The meaning of "runs" can be found in https://www.luogu.com.cn/problem/P6656:
For a string $$$S$$$ of length $$$n$$$, a run is a tuple $$$(i,j,p)$$$ that satisfies the following conditions:
$$$p$$$ is the minimal period of $$$S[i,j]$$$.
$$$j-i+1\ge 2p$$$.
$$$i=1$$$ or $$$S[i-1]\neq S[i-1+p]$$$.
$$$j=n$$$ or $$$S[j+1]\neq S[j+1-p]$$$.
Just wanted to share that for problem F2 I used linked list with square root decomposition for inserting and querying specific locations. I had a pointer for every 1000th element so overall it worked in 500*N.
UPD: Tutorials for problem E and H are updated.
I think the updated editorial of H still has some problems.
"$$$A$$$ and $$$B$$$ are palindromes, and $$$A$$$ is a prefix of $$$B$$$." Does it actually mean $$$B=AC$$$,where $$$A$$$ and $$$C$$$ are palindromes?
What if the period doesn't occur at least twice,such as $$$BA$$$?In that case,the conclusions about run can't be used,and the effective palindrome pairs might be $$$O(n\log n)$$$,which means the total time complexity might be $$$O(n\log^2 n)$$$.I think one possible hack is $$$S[i]=\log(\text{lowbit}(i))$$$,which looks like
abacabadabacaba....I'm sorry that you are right. It's $$$O(n\log^2 n)$$$. I didn't think carefully in this case and considered it just a big constant. :(
However, by applying global binary search, this problem can still be solved with a time complexity of $$$O((n+q) \log n)$$$.
尽管如此,通过整体二分,这个问题仍然可以做到 $$$O((n+q) \log n)$$$ 的复杂度。
guessforces
C is a great problem.
It tell me a database called multiset
Can anyone figure out why this https://mirror.codeforces.com/contest/2164/submission/349829120 is wrong for problem C
is there a way to see hidden testcases?
It ACed, it isn't wrong.
Also, there's no way to see the test.
sorry pasted the wrong (wrong as in not intended) submission. figured it out. thanks anyway :)
Problem E can be solved (I guess unintentionally) in $$$O(n \log^2(n))$$$ by sorting edges by weight, storing vertices in DSU and merging their edges (by smaller to larger), and then doing BFS over edges with smaller index than our current one. And cherry on top: using priority_queue instead of set.
349991751
C can be solved by repeat binary search right? O(nlogn) [edit: im dumb sry]
considering newbies like me please write solutions for problems like D in a more readable format omg T_T
how should i solve using this approach, sort all the bi's and rearrange ci's corresponding to the bi's . after that pick up any sword with damage x and consider all the monsters with health <=x and kill the monster with max ci value . but how do i find this monster with max ci value efficiently. please help.
Is there any binary search approach for C..
H can be Accepted in $$$\mathcal{O}(q\sqrt{n}\log n)$$$,actually the data isn't strong enough.
Can someone, roughly, explain the time complexity of my code for D?
This is the "optimal" one: https://mirror.codeforces.com/contest/2164/submission/356135976 And this is the non-optimal one: https://mirror.codeforces.com/contest/2164/submission/356135935
I'm not sure why both of them passed, as it seems like K * N to me(the final loop).
I wrote the optimal one first, and it passed, so I decided to check if the l and r were necessary, somehow got the same runtime, so I resubmitted, and it worked a little bit faster
$$$O(n \cdot k_{\max})$$$ should pass as the constraint says $$$(1 \le n \cdot k_{\max} \le 10^6)$$$ and $$$10^6$$$ operations can easily pass.
Ohh, I didn't notice the multiplication. Thanks!
B was really a great problem..