Hello, Codeforces!
We are glad to invite you to take part in Codeforces Round 1052 (Div. 2), which will start on Sep/21/2025 17:35 (Moscow time). You will be given 6 problems and 2 hours and 15 minutes to solve them. One of the problems will be divided into subtasks.
The problems are authored and prepared by WRKWRK, and me LMydd0225.
We would like to thank:
- Error_Yuan for his intelligent coordination.
- A_G, BucketPotato, Dominater069, EasonTAO, Serval, Sugar_fan, __baozii__, _istil, ApraCadabra, Caylex, FetFot, mzen, mwen, omsincoconut, Proof_by_QED, -firefly-, Sofija06, windreaper, chromate00, kreeeesh_17 and sinmim236 for testing.
- mygr for providing a piece of code to one of the problems.
- Alexdat2000 for Russian translations.
- MikeMirzayanov for Codeforces and Polygon platforms.
- You for participating.
Hope you will enjoy the problems!
The scoring distribution is $$$500 - 1000 - 1250 - (1500 + 1000) - 2500 - 3500$$$.
UPD 1: The Editorial is out.
UPD 2: Congratulations to the winners!
Div. 1:
Div. 2:








As a tester, orz Error_Yuan! (Yuan shen!)
Not as a tester, I'm not a tester.
test
er
as a tester
BatmanOfficial your not a tester bro
really? i dont knew that. thank you so much.
the testers name are given above in the blog
as tester. awesome problems — hope everyone gets a positive delta!
Thanks for testing this contest <3
As a tester, I think these questions are really great and I highly recommend trying to solve them.
as a tester,I tested.
It's a great contest!
As a tester, I am a tester.
Auto comment: topic has been updated by Error_Yuan (previous revision, new revision, compare).
As a tester, it’s my first time testing !!
orz
what does it mean? I see so often but i have no idea about meaning
It's a bowing down sign
Thanks
But is it abbreviation?
just look at how it looks
orz
A guy bowing down, head is o, z are legs and r are hands and upper-body
As a tester,
As a tester, this is an auto comment.
As a tester, I murdered 1 problem and solved 0 problems.
As a tester, o_o ,retset a sA
Output: True
As an author,there was an Auto comment.
Looks like it would be a speedforces round
Why do you think so?
wow, 5 contests in a week
As a participant, I will participate in this contest
Oh wow, Baozii is actually cursed with testing all rounds.
The only one he didn't test in all of these was Global Round
Hoping to cross 1800 today! Also rooting for the first LGM of my country, Dominater069
Authors, please check RIGHT NOW that AI doesn't solve the problems from this round... Don't want to repeat the experience from the previous round
Good luck for everyone!
As a participant, I wish to reach CM :)
Hope to not get negative delta :)
Good luck to everyone!
Target : atleast 3 questions in 1 hour
Thanks to you
Hoping to reach specialist this round :)
Can't believe I actually did it !!!
Very excited
If this contest helps me to cross 1000 mark or get low
can't submit.. stuck on verifying you are a human screen for few minutes now :(
able to do it from submit page / not from problem page itself
try using m1.codeforces.com for submitting, sadly we don't have a quick way to fix this now :(
thanks for reply, I was able to work around and submit my code.
I was not able to upload file from right side on the question page..
but if I go to submit section .. select problem name and then submit.. then I didn't get that human check
hope it gets fixed next time
Exposing New Cheating Techniques in Codeforces Contests:
During a recent Codeforces contest, I discovered new cheating methods being employed, which I believe should be publicly exposed to protect the integrity of the community. One of the individuals involved is _ironman_3000, who submitted solutions in multiple programming languages. The submissions appear to be AI-generated, and he spread his solutions across various languages, such as Rust, Node.js, and others, likely to evade plagiarism detection.
Another individual, AnuragYADAV_10, from Kanpur, India, is also part of this group. Anurag is a student of Aslaan_Khan, who has previously been exposed by the community for similar misconduct. This group of cheaters not only uses AI to generate solutions but also harms the community by spreading solutions in different languages to bypass plagiarism detection systems.
AnuragYADAV_10 submitted his solutions in multiple languages, including Rust, C++, Python, and Kotlin, seemingly to avoid detection. However, he failed to realize that the Codeforces community itself serves as an effective plagiarism detector. His submission behavior is highly suspicious: fast solution submissions, unusually high rankings, and a history of skipping problems, all point to dishonest practices.
It’s also worth noting that aslaan001's account has been disabled due to misconduct, and his partner com_gaurav_dev has faced similar consequences. These individuals are part of a group known as the ALGO CLUB on Telegram, where they spread solutions and later convert them into different languages, disrupting the community's fair play.
For further context, here are the LinkedIn profiles of the individuals involved:
Yeah, I remember. Proof_by_QED, AttractorsTheory, fresh_tangerines exposed them earlier. The pattern of using multiple languages and AI-generated solutions is definitely familiar. Both were already in your Cheaters Database (https://cf-cheater-database.vercel.app/), but this Anurag guy seems to be a new addition. Lets add him too
Just saw AnuragYADAV_10 submissions, he already has a black belt in skipping.
Was chilling in 4000–5000 rating band, and suddenly teleported to top 200 like a programming wizard.
Knows Rust, Python, C++, Kotlin; a true multi-language legend.
Anyway, not my problem... I’m pretty sure he will be banned today itself if Proof_by_QED looks.
Could someone attach cheaters' photos like AnuragYADAV_10 so that we can have a look? Anyone, please?
This is the CHEATER :- AnuragYADAV_10
Why are so many 0 rated accounts replying to this comment? Very fishy. Good job nonetheless.
so small d2 score
vary goods problems i solved A-B-C but C in my mind hahah
C and D1 should have been a little harder. Especially D1.
I dont think so jumping to conclusion was tricky....
Very nice problem set
what was the idea for d2?
i couldn't figure out but , thinking of trie and greedy may be
its just greedy no trie
its core logic is same as d1 ?? that for larger number we will find a number which fits best ??
oh, i did not solve D1 like that. though D1 solution like that sounds good because for D1 there is no wastage, but in D2 there is going to be wastage.
Yes, something like that. But, instead of finding the best fit for r, you need to find the best fit for l too. Suppose the best fit for r is l2, and for l is r2. You now have [l, r2] and [l2, r] both of which will symmetrically map among themselves, but you have to check if r2 <= r or l <= l2.
Case 1: r2 < r, then you have an interval [l, r2] such that l | r2, l+1 | r2-1, ..., r2 | l is the best mapping. So, you can fill that interval and recursively solve for [r2+1, r]
Case 2: r2 = r, then l | r, l+1 | r-1, ..., r | l is the best mapping so fill that
Case 3: r2 > r, then you cannot find a fitting interval. But, you can be sure that l < l2. So, now the best mapping becomes l2 | r, l2+1 | r-1, ..., r | l2, and you can recursively solve for [l, l2-1].
Thanks author for stupid guessForces round and the -100 delta! Much appreciated <3
Bro, you did not even participate in the contest :)
Don't know why but B felt too tough!
same
first WA then TLE then AC.
Yeah bro, I became pupil yesterday and now preparing to get back to where I was... Had 4 wrong submissions in B
There was a very similar problem to D1 recently , not sure ( maybe even 2 months back ) . It was div-3 E or F problem I think. If someone finds it please share.
I skipped up-solving it then , could not solve it now either in contest :( . (can't seem to find the older problem now)
Would you like clue? or a solution? sometimes it takes a bit of time for the editorial to pop up
I am not sure if this is correct
Odd case is simple -> you always get n * n+1 -------- for even you try to do some recursive breakdown on powers of 2 pair them in a way to get maximum OR ( did not really fully figure this out)
Yes, any clue would help
I made the array (p) 0 to r then started iterating from r (i=r to 0) , calculated the number x which has opposite bits of i (for example if i=9 (1001) then x=6 (110)) then swapped p[i] and p[x] , and also maintained boolean array (if a number is already swapped once then it will not swapped again). Hope it helps :P
he just asked for a clue not the solution xD
xD
try to make "11111....n" in bits as much as you can. And we are doing bitwise OR in this case. Think about it
Amazing questions!!! Great Comp!!!
If I only managed to program E in time :(
Solved E with segment tree + treap + wavelet tree, loved it!
339769904
The definition of Overkill :)))
Despite realizing during the implementation that it is possible to replace treap and wavelet tree with another segtrees I decided to stick with treap + wavelet tree cuz it would be too boring to use only segtrees, lol.
What I tried and didn't finish but 99% sure is correct
we will go from left to right and solve each i
let last_pos[i] = the max index we say the number i until now
let last[i] = min(last_pos[0], last_pos[1] .. last_pos[i])
now we can see that last in non-increasing, actually, it's really easy to update this array with the add of a new element arr[i] in the end of the current pref we are solving, try to draw what last looks like, I do not have the power to draw here but it's const line who are decreasing
now we can notice from the question there is no point in checking a index who is not one less than last[j] for any j
so we can save a segment tree of max and point update for this points, because adding arr[i] only increases a continuous range of them, the mex is increasing as j gets lower, we can solve this Q ;)
such an overkill
can you explain the idea behind your code
Hope that I wrote somewhat clearly, I just really don't know how to draw here
actually i have already AC E
i'm just curious to see how could he even think of this solution in contest :)
It's amazing how much code chatgpt can write in 2 hours ;)))) LOL
I'm flattered: https://github.com/wery0/CP-library
Yeah, I'll explain my approach in a minute, right now you can think about Bonus version of problem E (see editorial), my solution can actually be extended to it.
For an array $$$a$$$ of length $$$n$$$ let's define an array $$$m$$$ as $$$m_i = mex(a_i, a_{i+1}, \ldots, a_n)$$$, and an array $$$c$$$ as $$$c_i = | \{ j\ |\ a_j \gt m_i,\ \ i \leq j \leq n \} | = w(i, n)$$$ i. e. $$$m$$$ is an array of suffix mex'es of $$$a$$$ and $$$c$$$ is an array of suffix weigths of $$$a$$$.
The idea is to calculate and maintain these $$$m$$$ and $$$c$$$ for every prefix of the given array. The answer to the problem for a given prefix is just $$$max(c)$$$.
Observation 1: $$$m$$$ is non-increasing.
Observation 2: If we represent $$$m$$$ with tuples $$$\{l,\ r,\ v\}$$$ s. t. $$$m_l = m_{l+1} = \ldots = m_r = v$$$ for every tuple, we can see that $$$c_l, \ldots, c_r$$$ is non-increasing, therefore $$$max(c_l, \ldots, c_r) = c_l$$$.
Given the observations lets maintain minimum number (i. e. all adjacent tuples have different $$$v$$$) of tuples $$$\{l,\ r,\ v,\ c_l\}$$$ which are fully representing the state of $$$m$$$ and partially representing the state of $$$c$$$. If we have our tuples for array $$$a$$$ of length $$$n$$$, here is what happens to them when we wanna add $$$a_{n+1} = t$$$:
Values $$$l,\ r,\ v$$$ in all tuples with $$$v \neq t$$$ are not affected: mex on the corresponding suffixes stays the same.
Tuple $$$\{l,\ r,\ v = t,\ c_l\}$$$ gets deleted as there are no more suffixes with $$$mex = t$$$. Now we need to split segment $$$a[l; r]$$$ into several subsegments that have the same $$$mex$$$. I'm doing this with D&C and the Observation 1 which works in $$$O(\log^2(n) \cdot \text{# of new subsegments})$$$: one of the $$$\log(n)$$$ comes from D&C, other one from evaluating specific value of $$$m$$$, which takes $$$O(\log(n))$$$ (see https://mirror.codeforces.com/blog/entry/117688 for the idea).
Now instead of the old tuple $$$\{l,\ r,\ v = t,\ c_l\}$$$ we have several new subtuples $$$\{l_i,\ r_i,\ v_i\}$$$. For each of them we need to calculate $$$c_{l_i}$$$ which is the number of elements in $$$a_{l_i}, \ldots, a_n$$$ that are greater than $$$t$$$. I'm doing this with wavelet tree in $$$O(\log(n))$$$ but it can be replaced with a persistent segment tree.
Now if $$$v$$$ of the last tuple $$$\neq mex(\{t\})$$$, add a new tuple $$$\{n+1,\ n+1,\ mex(\{t\}),\ 0\}$$$, otherwise just extend the last tuple.
Now in all tuples with $$$v \lt t$$$ increase $$$c_l$$$ by $$$1$$$. I'm using treap with lazy propagation to maintain these tuples inorder and performing this increasing operation but it can be replaced with a regular segtree with lazy propagation.
Using potential analysis we can see that the amortized time complexity of this solution is $$$O(\log^2(n))$$$ per every extension of $$$a$$$. And overall my implementation is $$$O(n\log^2(n))$$$ time and $$$O(n)$$$ memory.
Hahaha
I was trying something with lazy segment + another segment tree + mergesort tree but ran out of time to debug my solution :(
can someone explain me the problem with my code and share there approach on C- 339784912
ammm Didn't really get the first for, why if cnt%2 == 1 you cout no, s = "0001" is perfectly fine 3 2 1 4
D2 hints please, i can't get through the last example
so we kind of continue the D1 trick
what we do, lets say we are now solving the range l to r inclusive we look at r msb (most significant bit) we split the range to those who have it turned on and those who don't.
now if neither groups are empty we can show that it's optimal like in D1, if we denote p = (1 << msb) — 1
we will assign p to p+1, p-1 to p + 2 and so forth until we got out of the range, then the remining solve recursively, if one of them is empty, it will be the one whose bit is not turned on, just remove from everyone p
For D1 my code was
Can you guys tell me what I am missing my approach was to calculate 1's complement from end and then pair it with their 1's complement. But I am getting wrong ans.
test_case :
I got my mistake man was not handling the the part where used[i] == -1 after setting all the values. The code i was missing was
Nice catch!
Cool problems! I wonder what D2 solution is
so we kind of continue the D1 trick
what we do, lets say we are now solving the range l to r inclusive we look at r msb (most significant bit) we split the range to those who have it turned on and those who don't.
now if neither groups are empty we can show that it's optimal like in D1, if we denote p = (1 << msb) — 1
we will assign p to p+1, p-1 to p + 2 and so forth until we got out of the range, then the remining solve recursively, if one of them is empty, it will be the one whose bit is not turned on, just remove from everyone p
I dont know how to show my comment so I just copyed it again
code
include
include
include
include
include
include
include<math.h>
include
include
include
using namespace std; using ll = long long; using vll = vector; using vvll = vector; using pll = pair<ll, ll>; using vpll = vector;
vll ans; ll s;
void solve(ll l, ll r, ll add) { if (l > r) { return; } if (l == r) { ans[l — s + add] = l + add; return; } ll x = ll(log2(r)); ll p = (1LL << x) — 1; if (p < l) { solve(l — p — 1, r — p — 1, add + p + 1); } else { ll rem = min(r — p, p — l + 1); for (ll i = 0; i < rem; i++) { ans[i + p + 1 + add — s] = p — i + add; } ll pos = p — rem + 1; for (ll i = pos; i <= p; i++) { ans[i +add — s] = p + rem — (i — pos) + add; } if (r — p < p — l + 1) { solve(l, p — (r — p), add); } else { solve(p + ( p -l + 1) + 1, r, add); } } }
int main() {
}
Bro why does it looks so bad Swear to god I have enters IDK why it decided to delete them
just leave a line after ``` then paste the code,i guess this will work
haha I had this idea in my head and skipped it anyways, because its ReCuRSioN. ty ^_^
greedy bitwise constructive... i hate cf brainrot so much :/
for problem C, in what testcase does this approach for building the permutation fail:
for all i where s[i] == '1': make p[i] = i + 1
for every subsegment of the permutation where s[i..j] == '0', just set s[i] = i + 1, but in the reverse order in that particular subsegment.
reason: because to make an element stable, we need all elements smaller than it to be only on the left, and larger than it only on the right. if we encounter any subsegment of 0's with length only 1, then it isn't reversible, hence no suitable permutation is possible.
Looks correct to me. Are you sure this is not an impl issue? Maybe you missed the last segment while checking for adjacent equals?
yes. it was an implementation issue indeed. ty
As a Participant aiming for the Pupilllllll
Thank you for fast editorial. Within 2 min of contest getting over.
I liked the problems. If I don't get FST it will be my best performance.
The contest was good!
Cool org, I joined ^-^. Congrats on E.
congo, if you would have solved D2 also, it would have been an mindblowing contestt for you.
For sure! It would be ultra-super-mega-lucky.
my performance was really bad at this contest:(
Can someone explain me the first problem?
orz
A subsequence is balanced if every unique element appears the same number of times. Your task is to find the length of the longest balanced subsequence you can form. You may delete elements from a (possibly none) to make it balanced. Do this for multiple test cases and output the maximum length for each.
3 Hints:
Count how many times each unique number appears (frequency map).
Try all possible values of k (from 1 up to max frequency), where k means "use each number exactly k times".
For each k, add k for every element that has at least k occurrences — choose the k that gives the maximum total length.
thank you
thank you rainboy, for not solving the first three problems.
orz
Hello everyone, I am new to CF. Could someone kindly tell me whether this contest is rated or not. TIA.
Yep it's rated
wasted 1hr on B and solved D2 after 5mins of contest ;)
i submitted B in python(pypy) compiler.. and passed 10 pretest.. and got TLE in 11the testcase during system testing..after contest i applied same logic same datastructures.. just used c++ and it passed.. so brutal. i am never using python in here again
Almost Specialist...let's go
Proud to present screencast
I don't want to annoy codeforces people too much. I only plan to link screencasts on codeforces comments with an overall #1
It is great, but I think you should explain the thought process after the contest ends, it will be very helpful :)
I don't understand, I do fairly well in other platforms but always get humbled on cf. I got like 4k rank in the previous div 4 contest and my rating went up and now got 7.4k rank in the div 2 and thought well that is okayish but then my rating goes -9. Should I stop giving div 2 contests as a whole? But div 3 contests are rare and div 4 are very rare. What should I do to improve myself? Please give guidance.
Which platforms do you usually use? It helps to practice ABC problems of recent contests. It's mostly observation and intuition, so it takes some time to get used to.
I do codechef and Leetcode usually, but also GFG occasionally. If I grind more problems on this site, will that help in getting better ratings?
Of course, it will help you. I get stuck on ABC too sometimes TwT
I always manage AB in a div 2 that's it T_T But there will come a time where I'll be candidate master for sure >:0
1st place Easy_AC clearly used ChatGPT
omg rainboy made a comeback :)
Dear Codeforces, I got a warning which said my solution significantly coincided with I dont know who's solutions. I dont know that person, and I dont know how our solutions resembled either. Shit happens but kindly dont terminate/ban or do anything with my account from here onwards I will try my best to follow your rules and policies and will never give you a chance to question my honesty. It wont happen again, since I am going to be writing codes like no one else starting now
You must have gotten the submission link which was similar to your solution.
Dear admin, solution 339769708 coincides with ChessRonak/339752202, I believe it is a mistake of your system, me and his do match on approach but we have no correlation to each other including in the standings, he uses a different template i use a different template the only part i find matching is the alogrithm.
If i really cheated many would follow and end up like me and i wouldve gotten notice but theres only 1 person .
The problems were pretty easy and the implementation of mine is the result of using templete and doing it fast as possible.
Heres the logic of my program :-
During contest i was thinking of a approach to the solution, firstly i took help from the code snippet in the problem(logic wise),
then i just take the input, then i check it and skip the ones then i check for consecutive segments of 0 if the segment is only 1 char i output no,
if i didnt output no, i go to the next phrase of outputting the permutation first for every 1 i change p[i] = i+1, then for the zero segment i rotate it front by 1 to break the sortedness, then i finally output it.
Hope that helps!
[Comment Deleted]
Hi ! I've got a system message which said I am cheatting cause my solution https://mirror.codeforces.com/contest/2146/submission/339756701
looks like https://mirror.codeforces.com/contest/2146/submission/339739120
First of all, I would like to say that I accept the fact that I have to explain myself for the high similarity.
The problem is pretty straightforward and my implementation is just the results of healthy coding habits :)
For proving my sincerity, this all the submission I did before, cause Ive tried to write the same algorithm in different ways.
https://mirror.codeforces.com/contest/2146/submission/339749300 https://mirror.codeforces.com/contest/2146/submission/339752752 https://mirror.codeforces.com/contest/2146/submission/339755011
Thx for listeing to me, and thx for hunting cheaters !
Dear Codeforces Admin,
I have received a message that my submission for problem Equal Occurrences (https://mirror.codeforces.com/contest/2146/submission/339743879 ) matches the submission of another participant (https://mirror.codeforces.com/contest/2146/submission/339733197 ).
I completely understand that the similarity may appear high. However, I would like to clarify that this happened unintentionally. The problem itself was relatively straightforward, and therefore the solution approach and resulting code structure may look similar. I do not know the other participant, and I did not collaborate or share code with anyone during the contest.
Regarding the order of solving problems: I actually solved problem A after solving problem C. This was the first time I tried solving problems out of order, just to see if this approach might help me manage time differently. Normally I do not solve in this way, but in this contest I decided to experiment with the order.
I genuinely request you to kindly consider my submissions valid, as skipping them would negatively affect my account and progress. For additional reference, here are my submissions to the other problems of this contest: 339740435, 339758027, 339782226.
I always try to follow good coding practices and write efficient code. If my style has unintentionally contributed to the similarity, I will make sure to adjust and diversify my implementations in future contests to avoid such situations.
I sincerely apologize if my submission caused any concern and assure you that I am committed to fair participation.
Thank you very much for your understanding and for taking the time to review my explanation.
Respectfully, Strange_Star101
Dear Codeforces Admin,
I hope you are doing well. I had previously raised an issue regarding my skipped submissions in this rounds, but I have not yet received any response.
I would like to kindly request you to review my case and unskip my submissions. As I had mentioned earlier, I do not know the other participant with whom my code was matched, and I strongly believe that my submissions were made fairly.
This situation has been discouraging and is affecting my progress on the platform. I respectfully request you to consider my appeal and reinstate my submissions.
Thank you for your time and understanding. I look forward to your positive response.
Sincerely,
Strange_Star101
Dear Admin,
I received a coincidence warning for my submission (https://mirror.codeforces.com/contest/2146/submission/339719342) on the problem (https://mirror.codeforces.com/contest/2146/problem/A).
I strongly believe this to be purely coincidental as the problem was pretty standard, and the use of the common variables between mine and the other person's code is unintentional.
I confirm that i don't know the other person and I did not share my code or view the other person's code. Also i did not use any public platform for testing.
I would highly appreciate if you could manually review my code alongside the matched submission(https://mirror.codeforces.com/contest/2146/submission/339741962).I genuinely request you to kindly consider my submissions valid, as skipping them would negatively affect my account and progress.
Thank you for your time. Regards, The_SilntCodr
Was this contest declared unrated as it is showing in my profile?