Let $$$Z$$$ be the number of zeros in the array, and $$$N$$$ be the number of elements equal to $$$-1$$$.
Every zero must be incremented to $$$1$$$, which costs $$$Z$$$ operations. If $$$N$$$ is odd, then after removing zeros the product is negative. To fix this, we must convert one $$$-1 \to 1$$$, which costs $$$2$$$ operations.
Thus the answer is: $$$\text{ans} = Z + 2 \cdot (N \bmod 2)$$$.
The time complexity is $$$O(n)$$$ per test case.
Let $$$a$$$ be the sorted array. For each pair we want to minimize the maximum difference. If we pair far apart elements, the difference will be large. The optimal strategy is to pair consecutive elements after sorting: $$$(a_1,a_2), (a_3,a_4), \dots, (a_{n-1},a_n)$$$.
The difference of each pair is $$$a_{2i}-a_{2i-1}$$$, and the maximum of these values is the minimized maximum difference.
Thus the answer is: $$$\text{ans} = \max {a_{2}-a_{1},\ a_{4}-a_{3},\ \dots,\ a_{n}-a_{n-1}}$$$.
The time complexity is $$$O(n \log n)$$$ per test case.
Let $$$Z$$$ be the number of integers in $$$[0, k-1]$$$ that are missing from the array, and let $$$C$$$ be the number of elements equal to $$$k$$$.
To make $$$\text{MEX}(a) = k$$$, every number from $$$0$$$ to $$$k-1$$$ must appear, which costs $$$Z$$$ operations. Also, $$$k$$$ must not appear in the array, so we need $$$C$$$ operations to remove them. While removing copies of $$$k$$$, we can directly replace them with missing numbers, so the total number of operations is the maximum of these two values.
Thus, the answer is: $$$\text{ans} = \max(Z, C)$$$.
The time complexity is $$$O(n)$$$ per test case.
Let the target be to make all occurrences of one letter contiguous. Do it for both letters and take the minimum.
Fix a letter $$$c\in{a,b}$$$ and let its positions be $$$p_0 \lt p_1 \lt \dots \lt p_{m-1}$$$. If we place these $$$m$$$ copies into a consecutive block starting at some index $$$L$$$, the number of adjacent swaps needed is
Set $$$b_i=p_i-i$$$. The function $$$\sum_i |b_i-L|$$$ is minimized when $$$L$$$ is a median of $$${b_i}$$$. Hence the optimal cost to cluster all $$$c$$$'s is
The answer is $$$\min(\operatorname{cost}(a),\operatorname{cost}(b))$$$.
This counts the minimal number of adjacent swaps because each swap reduces the sum of pairwise inversions between the two types by exactly $$$1$$$, and aligning to a consecutive block achieves the minimum. The algorithm computes positions, builds $$$b_i$$$, takes the median, and sums absolute deviations — all in linear time after positions are known.
The time complexity is $$$O(n)$$$.
2149E - Hidden Knowledge of the Ancients
Let’s fix the left border $$$i$$$. Let the valid right borders be $$$j \ge i$$$ such that
- $$$[a_i,\ldots,a_j]$$$ has exactly $$$k$$$ distinct numbers;
- $$$l \le j-i+1 \le r$$$.
Maintain two sliding windows:
- $$$x$$$ — the maximum right index such that $$$[i..x]$$$ has exactly $$$k$$$ distinct;
- $$$y$$$ — the maximum right index such that $$$[i..y-1]$$$ has at most $$$k$$$ distinct.
All valid right endpoints lie in $$$[x, x+1, \dots, y-1]$$$. Intersect with the length constraint $$$[\,i+l-1,\, i+l, \dots, i+r-1\,]$$$. The number of good subarrays starting at $$$i$$$ is
Add this to the answer, then move $$$i$$$ forward and update the frequency counters.
Each element is processed once by the two pointers; with coordinate compression the total time is $$$O(n\log n)$$$.
2149F - Nezuko in the Clearing
Suppose we use exactly $$$m$$$ rests. Then the $$$d$$$ moves split into $$$s=m+1$$$ consecutive runs of moves between rests. If a run has length $$$t$$$, its health cost is the triangular number
because the run's $$$j$$$-th move costs $$$j$$$ health.
For fixed $$$m$$$, to minimize the total cost we make the run lengths as equal as possible. Let
Then $$$r$$$ runs have length $$$a+1$$$ and $$$s-r$$$ runs have length $$$a$$$, so the total health spent is
Rests add health: each rest gives $$$+1$$$, so over the whole trip the available health is $$$h+m$$$. Because health must stay $$$\ge 1$$$ after every move, we need
This gives a monotone predicate in $$$m$$$, so we binary search the smallest $$$m\in[0,d]$$$ satisfying it. The answer is the total number of turns:
We use a randomized method: for a query $$$[l,r]$$$, let $$$L=r-l+1$$$ and $$$T=\left\lfloor \tfrac{L}{3}\right\rfloor$$$. We repeatedly sample random positions from the segment, and each sampled value is treated as a candidate. If a value truly occurs more than $$$T$$$ times, then it occupies more than one third of the segment, so the probability of hitting it in one random draw is
After $$$k$$$ independent draws, the probability of missing it is at most $$$(\tfrac{2}{3})^k$$$, which becomes negligible for $$$k=50$$$.
To check a candidate, we precompute for each distinct value $$$x$$$ the sorted list of its positions $$$f_x$$$. The exact frequency of $$$x$$$ in $$$[l,r]$$$ is
If
then $$$x$$$ is included in the answer.
Since there are at most two true heavy elements, and each is found with high probability through random sampling, we collect all verified candidates, remove duplicates, and output them in sorted order.
Each query runs in $$$O(k \log n)$$$, where $$$k$$$ is a small constant (e.g. $$$50$$$). This is efficient given the constraints, and the error probability is astronomically small.
It is a known fact that in any segment there can be at most two elements occurring more than one third of the time($$$ \gt \left\lfloor \dfrac{L}{3} \right\rfloor$$$). Therefore, for each query, it is enough to maintain at most two candidates.
We build a segment tree where each node stores up to two candidate values with counters. The merge operation is exactly the Misra-Gries algorithm with capacity $$$2$$$: if a value matches, we increase its count; if a slot is free, we put it there; otherwise we cancel counts.As a result, querying $$$[l,r]$$$ yields up to two potential answers. To check them, we precompute for each distinct value the sorted list of its positions. The exact frequency of a value $$$x$$$ in $$$[l,r]$$$ is
where $$$f_x$$$ is the list of indices where $$$x$$$ occurs. If this number is greater than $$$T$$$, then $$$x$$$ is included in the answer. After verifying at most two candidates, we output them in sorted order, or $$$-1$$$ if none qualify.








Auto comment: topic has been updated by __rose (previous revision, new revision, compare).
This contest thought me not to abuse unordered_map again, nice contest
You can use
unordered_maporgp_hash_tablewith a custom hash function to avoid collisions: https://mirror.codeforces.com/blog/entry/62393GOT MY FIRST +69
congratulations!
I'm getting a message that says You are not allowed to view the requested page when I try to access the codes
Same bro!!
Thanks for the wonderful round! I successfully reach blue :)
why does it show "N/A" when I read others' code?
i will fix it in few minutes
same issue
i have fixed it, u may see it now.
thanks
still cant access solution codes linked
E is still not fixed,not able to view sol.
fixed
340571620 Can anyone tell me what's wrong with my code for D ??
for this test_case : "abaaba" your answer is 1 but the right one is 2.
For example, when counting the steps required for
a(which is at index i) to move to the front ofb(which is at index j), we should add the number ofbbetween them that are beforea, rather than usingi - j. Because theainfront of thisaare already move infront of theb.Where is the editorial?
You are not allowed to view the requested page ...
i have fixed it
Auto comment: topic has been updated by __rose (previous revision, new revision, compare).
x — the maximum right index such that [i..x] has exactly k distinct; y — the maximum right index such that [i..y−1] has at most k distinct.
Won't that end up with x and y being the same index? Since unique elements in [i..a] <= [i..a+1], so the maximum index with at most k distinct will have exactly k distinct elements (unless there's no such a which has at least k distinct elements in [i..a])
Yes, you are right. I think the editorial was written in a hurry, so they made a mistake. You can do last index with at most k-1 distinct elements and last index with at most k distinct elements.
maybe "x——the minimum right index such that [i...x] has exactly k distinct ?"
Auto comment: topic has been updated by __rose (previous revision, new revision, compare).
Auto comment: topic has been updated by __rose (previous revision, new revision, compare).
The submission link in D redirects to that of E.
The submission link in E doesn't open.
thanks, i will fix it
Alternate sol for D
for each letter a and b we try out all possible prefix blocks and calculate
the cost of making this prefix and corresponding suffix for that letter
still cant access code for E
fixed
For problem G, there is an $$$O(n \sqrt{n})$$$ approach, which didn't get mentioned in the tutorial.
For the occurence of a number, let's define a fixed bound value, say $$$B$$$.
Consider the number of $$$a_i$$$ which occurs not less than $$$B$$$ times, and we can say that it is not more than $$$\frac{n}{B}$$$, according to the pigeonhole principle.
Then, for a query interval $$$\left[l, r\right]$$$, define $$$g = \lfloor \frac{r - l + 1}{3} \rfloor$$$, and one can process it differently when $$$g \ge B$$$ or $$$g \lt B$$$.
Generally, the time complexity is $$$O\left(\frac{n^2}{B} + q \left(B + \frac{n}{B}\right)\right)$$$. Applying the powerful A-G inequality, we can set $$$B = \sqrt{n}$$$ and obtain the optimal time complexity $$$O\left(\left(n + q\right) \sqrt{n}\right)$$$, which is sufficient for the time limit.
However, the pre-calculated counting array has a size of $$$O(\frac{n^2}{B})$$$, which is too large for the limit of 256MiB. One need to alter the value of $$$B$$$ to satisfy both the time and space limit.
Or, one can obtain the counting array while iterating the often-occuring numbers separately, since each of them contributes the answer indepentently. Then the space complexity would be $$$O(n)$$$. (idea: Noobish_Monk)
Accepted submission: 340653926. $$$O(n)$$$ space submission: 340710637.
P.S. I apologize for my poor English. ((
To optimize memory you can just bruteforce all numbers that occur at least $$$B$$$ times and for each number you do
1) calculate the number of occurrences on each prefix
2) check for every query if the number is in the answer or not
You are right. Thanks!
To brainstorm, Mo's Algorithm with Rollback 回滚莫队 can also achieve $$$ O(n\sqrt{n}) $$$ time complexity and I think it is trivial to come up with. My submission: 341966789
Auto comment: topic has been updated by __rose (previous revision, new revision, compare).
Auto comment: topic has been updated by __rose (previous revision, new revision, compare).
In the editorial for E, shouldnt the word 'maximum' be replaced with minimum in the following statement:
"x — the maximum right index such that [i..x] has exactly k distinct;"
can anybody tell me whats wrong with my submission :
https://mirror.codeforces.com/contest/2149/submission/340460656
Genius idea with "delimiter" $$$(l+r)/2$$$ ! But I think error happens, because your always start from left part, so example like: aa|bbbba|aaaab|aa
ohk i got it what i am doing wrong :) thankyou
Can someone please tell me what's wrong with my code for E? 340636181 It passes all tests exceptthe last one
Can we solve G with sweep line + Misra-Gries + sparse table?
G can also be solved using extended boyer moore's algorithm with segment tree in O(NlogN).
Each node will store 2 potential candidates — {(candidate_1, votes_1), (candidate_2, votes_2)} as there can be at max be 2 candidates only. In simpler words, the segment tree will store the result of extended boyer moore's algorithm on the range [l,r].
Merging Logic — 1. If a candidate from the right matches an existing one, then their votes are added. 2. If a slot is open, the new candidate fills it. 3. If all candidates are different, they cancel each other by reducing their vote counts.
For each of the two candidates returned by the query, you must calculate its true frequency within the range [l, r]. This can be done quickly by pre-calculating the indices of all numbers and then using binary search. If the true frequency is strictly greater than ⌊(r — l + 1) / 3⌋, then it is a valid answer.
__rose In solution F, you have taken the input cin >> d >> h; But according to the question it should be cin >> h >> d; As it said that health should be the first input then the distance d.
The merge operation for G is genius.
During the process of Misra-Gries algorithm ,if a key is excluded from the k-1 candidates, we can prove that every occurence of the key uniquely specifies k-1 occurences of other keys. Thus in the end, its frequency multiplying k can't outnumber n.
And it still make sense if you merge the infomation.
Hii can anyone help me here, Why is my solution to Problem C giving TLE on 7th test case Submission ID: 340421354
According to my understanding this is still O(n) or max O(nlogn) I'm all ears, It's been a while since i started CP again, maybe I'm missing something Thank you in advance
Can anyone explain this in question F editorial ?
I didn't understand the logic.
First observe that taking a rest for more than one turn is suboptimal. If you were to do so, you could've used the point of health restored in the previous turn to move one unit of distance. And if you were to repeat that process, to take one rest and move one unit, you could get to the end in at most $$$2d$$$ turns (except for the case where you start with $$$h = 1$$$, where you must first rest before you move).
To even move for a longer distance in one consecutive run you'd have to rest for $$$3$$$ turns, just to move $$$1$$$ unit, where with the same $$$4$$$ turns you could've moved $$$2$$$ units of distance instead. In fact, if you were to run for $$$k$$$ consecutive units of distance, you could choose instead to run for $$$k-1$$$ consecutive units, and have $$$k$$$ extra health to run at least one more unit (possibly more, given that the amount of health consumed for each consecutive move increases), at the expense of one extra turn to rest (and while at it, regenerate $$$1$$$ point of health).
Thus, it seems like if you had enough initial health to run to the end, it's optimal to use it all to get to the point $$$d$$$ in the least number of turns. However, if this weren't the case, you must take a rest. And given that moving towards the end already takes $$$d$$$ turns, the only additional turns you use to get to your destination are the ones where you rest, which we said shouldn't be longer than $$$1$$$ turn.
Therefore, the number of turns used to get to point $$$d$$$ can be split into $$$m+1$$$ consecutive runs, where $$$m$$$ is the number of rests you take, as said in the editorial. If you fix $$$m$$$ to some value, you already know the number of turns you need to get to the end, it would be $$$d+m$$$. However, to determine whether you can get to the end or not with that number of rests, you must use all of the rests optimally.
Let's say with fixed $$$m$$$ you'd do $$$m+1$$$ consecutive runs towards the end, such that one is longer than any other. As previously observed, you could run for a unit less of distance in the longer run and use that health to run at least the same amount of distance, possibly even longer, while consuming one of your rests. In that case, you'd be using your health more optimally than if you had chosen to run longer in one run, maximizing your chance to get to the end with the consecutive runs you'll do later. However, you can't run many short runs, as that will probably require more turns to rest than $$$m$$$. Therefore, it's best to make all runs as equal in distance as possible, while being long enough in total to reach the end with $$$m$$$ rests.
Note that it's likely that the distance $$$d$$$ isn't divisible by $$$m+1$$$ for fixed $$$m$$$. So to make all runs are equal as possible, some runs will be of length $$$\left\lfloor \frac{d}{m+1} \right\rfloor$$$ while others will be of length $$$\left\lceil \frac{d}{m+1} \right\rceil$$$. In fact, $$$d \mod (m+1)$$$ will be of length $$$\left\lceil \frac{d}{m+1} \right\rceil$$$, while the rest will be of length $$$\left\lfloor \frac{d}{m+1} \right\rfloor$$$ (all being of length $$$\left\lfloor \frac{d}{m+1} \right\rfloor$$$ wouldn't be enough, and all being of length $$$\left\lceil \frac{d}{m+1} \right\rceil$$$ will overshoot the destination). Think of it like calculating each length as $$$\left\lfloor \frac{d}{m+1} \right\rfloor$$$, and assigning the remaining distance $$$d \mod (m+1)$$$ one by one each to a different run. If it isn't divisible, $$$d \mod (m+1) \neq 0$$$, and $$$\left\lceil \frac{d}{m+1} \right\rceil = \left\lfloor \frac{d}{m+1} \right\rfloor + 1$$$, so $$$d \mod (m+1)$$$ will be of length $$$\left\lfloor \frac{d}{m+1} \right\rfloor + 1$$$, and the rest of length $$$\left\lfloor \frac{d}{m+1} \right\rfloor$$$. Otherwise $$$d \mod (m+1) = 0$$$ and all $$$m+1$$$ runs will be of length $$$\left\lfloor \frac{d}{m+1} \right\rfloor$$$.
Then, the value $$$m$$$ can be binary searched to be minimal as possible. Observe that this is possible because if you could arrive to the destination with the initial health $$$h$$$ for some $$$m$$$, it's also possible to arrive with a higher $$$m$$$, since you'd be allowed to take more rests. With more rests, each of the $$$m+1$$$ runs will be at most of the same length as the ones used with $$$m$$$, which will consume less health given what was stated before that using shorter runs will use health more optimally.
why are we considering r runs as a+1 length and (s-r) runs as a length ? It could be other way around also ?
It cannot be the other way around, because in that case you would be overshooting the destination. Given $$$s = m+1, r = d \mod s, a = \left\lfloor \frac{d}{s} \right\rfloor$$$ holds:
yeah I understood
Well we can also notice that consecutive runs take t*(t+1)/2 health points to run t distance which is subotimal when you make one run longer than other. It will always be better to make the runs as equal as possible to avoid this situation. this can be easily seen as t*(t+1)/2 increase by a power of 2. So if we have a and b as two parts and suppose a>b than a**2 + b**2 will always be max when one of them is greater as the minimum value of this fucntion is 2*a**2 as can be easily proved by basic calculus.
Just adding my two cents...
i have a problem:in D,i use int to store my ans,it's wrong.The answer code also use int but it's AC.why?I apologize for my poor English。
Is there any way Mo's Algorithm could be used to AC problem G?
I tried it to make it as efficient as possible, but it still TLEs: 340736003
is not "as efficient as possible".
Your data structures have a constant factor it almost rivals galactic algorithms. Is that "as efficient as possible"?
Replace your handling code with randomization. You only need a simple count array.
Try Misra-Gries with rollback Mo's?
340642523
Thanks!
Yes, there is a way to use MO's Algorithm without randomization to solve this problem, checkout my submission: 340804466
Thanks!
Is it really high-risk to use randomized algorithms or non-deterministic algorithms (like problem G this time or using simulated annealing in some problems) in Codeforces competitions? I often hear statements like "randomized algorithms are easily hacked." So, what are the methods to hack mt19937 or rand? How can we make hacking randomized algorithms more difficult?
If they know what random numbers your program is generating, they can generate a test that avoid/abuse those numbers. Also if your failure rate is too high, then they could stress your solution until it fails. To avoid the first problem, uses something that the hacker doesn't know on time, like a seed that depends on the time (e.g
chrono::steady_clock::now().time_since_epoch().count()). Do NOT use fixed seeds. To avoid the second problem, you could optimize your program to allow for more iterations. See https://mirror.codeforces.com/blog/entry/61675 and https://mirror.codeforces.com/blog/entry/61587.Editorial of F says —
How to arrive at this conclusion ?
We are given d distance and we have to divide it into equal parts. I suppose you understood until this part. So now notice that if we divide d by s than
d = a*s + r where r = d % s and a = floor(d/s).
Now adding and subtracting a*r we get:
d = a*s + r = a*s + a*r + (r- a*r) = a*(s-r) + (a+1)*r
So now you can see that we can have simply run r runs of a+1 length and s-r runs of a length. Notice that 1<=r<a and thus longer runs will always be less than shorter runs of a lenght. I understood it this way but if anybody have a better solution please feel free to add.
can someone tell me how to hack a unordered objects ... theory behind it
See https://mirror.codeforces.com/blog/entry/132929
For D there is one more logic very similar to it which I wrote in contest I never thought that the issue would be declaring
ans = INT_MAXwill give me WA and couldnt think later on :(.Check the code — 340845649 in contest and this only diff is setting ans =
LLONG_MAX.Ideally some questions are good saying that the answer might not fit in 32 bits it should have been provided here _/_.
A simpler and more readable implementation of Tutorial for E.
For 2149D - A and B I implemented it with O(n) solution.340468951
Notice, that in problem 2149G - Buratsuta 3 in the randomized solution author remaps all of the values to the segment $$$[ 0, 2 \cdot 10^5 - 1 ]$$$ in the preprocessing state and then stores all indices as the array of vectors, giving constant time access in query. This solution works in $$$\mathcal{O}(k\log{}n)$$$ per query in the worst case.
If you use the map of vectors to store all of the indices instead, the solution will not pass the TL, but will still have the $$$\mathcal{O}(k\log{}n)$$$ complexity per query, but with a larger constant factor. For example, the input with $$$n = 2 \cdot 10^5$$$ distinct numbers and $$$q = 2 \cdot 10^5$$$ queries with $$$l = 1$$$ and $$$r = n$$$ runs in 7s on average on my machine, when using the map of vectors, and will get TLE in online judge (see this 340925425 submission).
However, using the array of vectors or hashmap of vectors with custom hash runs for about 5.5s on my machine (on the above test) and will pass the TL (see this 340922007 submission, for example). Also, the author's solution with remapping to array of vectors runs in 2.5s on server, which is a second faster, then the hashmap of vectors solution. So, consider using these structures, if your solution doesn't pass TL.
Suppose we use exactly m rests. Then the d moves split into s=m+1 consecutive runs of moves between rests. If a run has length t, its health cost is the triangular number
T(t)=t(t+1)2 because the run's j-th move costs j health. For fixed m, to minimize the total cost we make the run lengths as equal as possible.
This is in the editorial for the F Question. Can someone explain how is minimizing the total cost related to the run lengths being equal?
The cost of a run of length t is -> -> T(t) = 1 + 2 + ... + t= t (t + 1) / 2. if one run is long (a) and another is short (b) , then when we transfer one move -> the long run loses its last step which costs (a) the short run gains a new step which costs (b + 1) since a >= b + 2 so it is a > b + 1. Explanation: when we have the inequality a >= b + 2 this means that a is at least two units greater than b. if a is at least two greater than b , then it is certainly greater than b + 1 . by the transitivity of inequalities: if x >= y and y > z , then x > z. applying this with x = a , y = b + 2 , z = b + 1 , we obtain a > b + 1. then we have a > b + 1 . so we remove more cost than we add Therefore the total cost decreases and the minimum is reached when all run lengths are as equal as possible.
ohkk got it thanks
Suppose we have $$$m$$$ rests, and therefore $$$m + 1$$$ runs $$$^\ast$$$. Pick some random $$$m + 1$$$ runs of length $$$l_1,\, l_2,\, \ldots,\, l_{m+1}$$$, such that $$$l_1 + l_2 + \ldots + l_{m+1} = d$$$. This whole run may not be optimal, but we can optimize this run with the following steps:
The process will converge, because for each iteration the cost either decreases by at least one or stops, when there is no decrease in cost. You can see, that the system will get into the state, where you have $$$x$$$ runs with some length $$$l$$$ and $$$y$$$ runs with some length $$$l + 1$$$, where $$$x + y = m + 1$$$.
For example, consider we have $$$4$$$ rests in the start and therefore $$$5$$$ runs. And pick $$$d = 21$$$.
Pick some random runs, for example, $$$l_1 = 8,\, l_2 = 5,\, l_3 = 2,\, l_4 = 5,\, l_5 = 1$$$.
Then, for the first iteration, the cost will change in $$$-8 + 2 = -6$$$, and the lengths will be $$$l_1 = 7,\, l_2 = 5,\, l_3 = 2,\, l_4 = 5,\, l_5 = 2$$$.
On the second iteration, the lengths will become $$$l_1 = 6,\, l_2 = 5,\, l_3 = 2,\, l_4 = 5,\, l_5 = 3$$$ with the cost change $$$-7 + 3 = -4$$$.
Continuing the iterations, unless the process converges, we get $$$l_1 = 5,\, l_2 = 4,\, l_3 = 4,\, l_4 = 4,\, l_5 = 4$$$, from where the cost will not change on next iterations, yielding the optimal run for the given number of rests.
$$$\ast$$$ First of all, if $$$d = 0$$$, then you can always complete the run with any number of rests. So, we will assume, we have $$$d \gt 0$$$.
You can show that it is not effective to rest at the start, if you have $$$h \ge 2$$$. If you have only one health at the start and at least one rest, then you can do
m--andh++, or if one health and no rests then impossible to complete the run.And, of course, there is no point to rest in the end, cause when you reach the end alive, there is nowhere to run (very non-philosophical).
What is the purpose of making so tight requirements for
G - Buratsuta 3problem (at least in randomized solution)? You forced to remap values of original inputs, because usingmapis too slow. Answer format require possibility of restoring original $$$a_i$$$ value. Just several steps, which doesn't make solution more interesting. Anti-hash tests is icing on the cake in this problem. It's not common, to see the authors want you optimize constants.Nice idea thought.
Just an illustration, how long it took me, to come up with this optimization :)
I’m trying to understand the trade-offs between different containers in C++ for competitive programming.
Specifically, when should I prefer map, unordered_map, unordered_map with a custom hash, or just use a vector instead? I know that they have different complexities, memory usage, and collision risks, but I’d like to know some practical guidelines or common situations where one is better than the others. Any suggestions would be appreciated!
inequalityforces
It is still showing N/A for me for all the codes.
Hi, how does this condition C(m)≤h+m−1 satisfies that we don't have zero health in the intermediate steps before reaching the destination ?
Let $$$C(m) = c_0 + c_1 + \cdots + c_m$$$. Where $$$c_i$$$ is the cost of the $$$i$$$ th positive length run. Suppose for simplicity that $$$m = 2$$$. We need $$$(h \gt c_0) \land (h + 1 \gt c_0 + c_1) \land (h + 2 \gt c_0 + c_1 + c_2)$$$. Note that the last condition is equivalent to $$$C(m) \le h + m - 1$$$.
Knowing that $$$c_2 \ge 1$$$, it's easy to show that $$$(h + 2 \gt c_0 + c_1 + c_2) \Rightarrow (h + 1 \gt c_0 + c_1)$$$. Similarly it's easy to show that $$$(h + 1 \gt c_0 + c_1) \Rightarrow (h \gt c_0)$$$. Hope this helps!
thank you so much
Can somebody explain why it is SUM(pi — L + i) rather than SUM(pi-L)
Hello,
My solution 340473928 for problem 2149C has been marked as coinciding with several others. I would like to clarify that I did not share my code with anyone nor did I copy from others. I worked on the problem independently.
It is possible that the similarity occurred because the problem has a standard approach and many participants may have ended up with similar implementations. I also did not use any public code-sharing platform during the contest.
I kindly request you to review my case. I assure you that I respect the rules of Codeforces and will be careful in future contests to avoid any unintentional issues.
Thank you for your understanding.
— Xyra
Nice problemset, but i found it kind of bizarre that in G all of the arrays are sorted in the testcases, and the real problem doesn't say nothing about this. I think it would be nice to put at least one problem where the array is not sorted, so it would be clear that the array is not necessarily sorted in the problem
Can anyone please explain in problem D why we are subtracting i from every position value in p? Why calculate (p[i] — i) when i increments from 0 to positions.size()?
spent some time figuring this out, so we have two subtractions, one corresponds how many letters a you have to jump if you are letter b, and the other one corresponds to the letters that you cannot jump from the midpoint which are the same b letters that you do not need to swap because it is already in a block after you swapped the a's, hope it makes sense
what is my mistake in the question 3 code ?
I am doing the same thing as the tutorial said , but mine is failing the 5 test case ,
solution : 340408663
For problem F:
why we can use s=m+1 and m belongs to [0,d] at the same time?
If m=d, s seems can be m rather than m+1.
h=1 d=2
(1)rest,h=2,p=0
(2)move,h=1,p=1
(3)rest,h=2,p=1
(4)move,h=1,p=2
Here m=d=2 and s=2
Emm…….It's really a big challenge for me to understand the English editorial or problem.Is there anyone have some good advice for me to improve my abilities about English reading?
x— the maximum right index such that [i..x] has exactly k distinct; y— the maximum right index such that [i..y−1] has at most k distinct.
i mean shouldn't x be the minimum right index such that [i...x] has exactly k distinct elements?? otherwise x and y-1 will be the same positions always which will yield wrong solution !!!!
At First i was unable to solve the problem Div3-E. Then i did this 2 problems from CSES and got hint.
For anyone trying to solve Problem-E by his own, can try out these problems for hint.
Can someone why this code for problem D is failing in test#2?
How did the randomized solution for G pass? I tried literally the same think just with K = 41 and it got TLE on test case 15, i submitted a bunch of other submissions but everything gave TLE around tc 15.
can anybody explain E to me in easier language?
I can't believe that problem G can be solved by randomized method! I solve it with Segment Tree + Binary search, the code of which is REALLY messy and hard to debug. What an intelligent solution it is!!
It's easy to get rid of the log in the randomized solution for G by simply processing queries offline: 361324634