__rose's blog

By __rose, history, 8 months ago, translation, In English

2149A - Be Positive

Solution

2149B - Unconventional Pairs

Solution

2149C - MEX rose

Solution

2149D - A and B

Solution

2149E - Hidden Knowledge of the Ancients

Solution

2149F - Nezuko in the Clearing

Solution

2149G - Buratsuta 3

Solution (randomized method)
Code (randomized method)
Solution (Misra--Gries algorithm)
Code (Misra-Gries)
  • Vote: I like it
  • +120
  • Vote: I do not like it

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by __rose (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

This contest thought me not to abuse unordered_map again, nice contest

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You can use unordered_map or gp_hash_table with a custom hash function to avoid collisions: https://mirror.codeforces.com/blog/entry/62393

    #include <chrono>
    #include <cstdint>
    
    struct custom_hash {
        static uint64_t splitmix64(uint64_t x) {
            x += 0x9e3779b97f4a7c15;
            x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
            x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
            return x ^ (x >> 31);
        }
    
        size_t operator()(uint64_t x) const {
            static const uint64_t FIXED_RANDOM =
                std::chrono::steady_clock::now().time_since_epoch().count();
            return splitmix64(x + FIXED_RANDOM);
        }
    };
    
»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

GOT MY FIRST +69

»
8 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

I'm getting a message that says You are not allowed to view the requested page when I try to access the codes

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Thanks for the wonderful round! I successfully reach blue :)

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

why does it show "N/A" when I read others' code?

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

340571620 Can anyone tell me what's wrong with my code for D ??

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    for this test_case : "abaaba" your answer is 1 but the right one is 2.

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    For example, when counting the steps required for a (which is at index i) to move to the front of b (which is at index j), we should add the number of b between them that are before a, rather than using i - j. Because the a infront of this a are already move infront of the b.

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Where is the editorial?

Cant find
»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by __rose (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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])

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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.

  • »
    »
    7 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    maybe "x——the minimum right index such that [i...x] has exactly k distinct ?"

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by __rose (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by __rose (previous revision, new revision, compare).

»
8 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

The submission link in D redirects to that of E.

The submission link in E doesn't open.

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like 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

code
»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

still cant access code for E

»
8 months ago, hide # |
Rev. 3  
Vote: I like it +60 Vote: I do not like it

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$$$.

  • If $$$g \lt B$$$, then we do a bruteforce on the interval $$$[l, r]$$$ to calculate the answers. Obviously, the time complexity for this part is $$$O(B)$$$.
  • If $$$g \ge B$$$, observing that the answers can only come from the numbers occur not less then $$$B$$$ times, we can iterate these numbers, and check if one can be included in the answers with a preprocessed counting array. The time complexity is $$$O(\frac{n}{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. ((

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by __rose (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by __rose (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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;"

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can anybody tell me whats wrong with my submission :

https://mirror.codeforces.com/contest/2149/submission/340460656

»
8 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Can someone please tell me what's wrong with my code for E? 340636181 It passes all tests exceptthe last one

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can we solve G with sweep line + Misra-Gries + sparse table?

»
8 months ago, hide # |
Rev. 2  
Vote: I like it +8 Vote: I do not like it

G can also be solved using extended boyer moore's algorithm with segment tree in O(NlogN).

Hint 1
Hint 2
Hint 3
»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

__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.

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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.

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hii can anyone help me here, Why is my solution to Problem C giving TLE on 7th test case Submission ID: 340421354

t = int(input())
for _ in range(t):
    n, k = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    if k == 0:
        print(a.count(0))
    else:
        curr = a.count(k)
        seta = set()
        for i in a:
            if i < k:
                seta.add(i)
        required = k
        available = len(seta)
        print(max(curr, required - available))

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

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone explain this in question F editorial ?


For fixed m, to minimize the total cost we make the run lengths as equal as possible. Let a = ⌊d/s⌋, r=d mod s. Then r runs have length a+1 and s−r runs have length a

I didn't understand the logic.

  • »
    »
    8 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +4 Vote: I do not like it

    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.

    • »
      »
      »
      8 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      why are we considering r runs as a+1 length and (s-r) runs as a length ? It could be other way around also ?

      • »
        »
        »
        »
        8 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        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:

        $$$d = s \frac{d}{s} = s \left\lfloor \frac{d}{s} \right\rfloor + (d \mod s)$$$
        $$$ = (s - (d \mod s) + (d \mod s)) \left\lfloor \frac{d}{s} \right\rfloor + (d \mod s) $$$
        $$$= (s - (d \mod s)) \left\lfloor \frac{d}{s} \right\rfloor + (d \mod s) \left\lfloor \frac{d}{s} \right\rfloor + (d \mod s) $$$
        $$$= (s - (d \mod s)) \left\lfloor \frac{d}{s} \right\rfloor + (d \mod s) \left(\left\lfloor \frac{d}{s} \right\rfloor + 1 \right) $$$
        $$$= (s - r) \left\lfloor \frac{d}{s} \right\rfloor + r \left(\left\lfloor \frac{d}{s} \right\rfloor + 1 \right) $$$
        $$$= (s - r) a + r \left(a+ 1 \right) $$$
    • »
      »
      »
      8 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      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...

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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。

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    8 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it
    Comment 1
    Comment 2
    Solution 1
    Solution 2
  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it
  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it +6 Vote: I do not like it

    Yes, there is a way to use MO's Algorithm without randomization to solve this problem, checkout my submission: 340804466

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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?

»
8 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Editorial of F says —

Then r runs have length a+1 and s−r runs have length a

How to arrive at this conclusion ?

  • »
    »
    8 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    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.

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can someone tell me how to hack a unordered objects ... theory behind it

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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_MAX will 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 _/_.

»
8 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

A simpler and more readable implementation of Tutorial for E.

ans =0;
// left border
ll x;
// right borders
ll y =0;
ll z=0;	

loop(x, 0, n, 1) {
	while (y <= n && m1.size() < k) {
		if (y < n) m1[a[y]]++;
		++y;
	}

	while (z <= n && m2.size() <= k) {
		if (z  < n) m2[a[z]]++;
		++z;
	}

	ans += std::max(0LL, std::min(z - 2, x + r - 1) - std::max(x + l -1, y - 1) + 1);
	// [x..y) is the SMALLEST window with exactly k distinct characters  
	// [x..z - 1) is the LARGEST window with exactly k distinct characters
	// all the valid windows with x as left end and all the indices [y, y + 1, .., z - 1] as right end

	// advance the left pointer  
	if (--m1[a[x]] == 0) m1.erase(a[x]);
	if (--m2[a[x]] == 0) m2.erase(a[x]);
}
»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For 2149D - A and B I implemented it with O(n) solution.340468951

»
8 months ago, hide # |
 
Vote: I like it +2 Vote: I do not like it

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.

»
8 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

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?

  • »
    »
    8 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    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.

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    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:

    1. Pick the run with the longest length $$$l_i$$$ and the shortest length $$$l_j$$$.
    2. Make the longest run one step shorter ($$$l_i := l_i - 1$$$), and the shortest run one step longer ($$$l_j := l_j + 1$$$).
    3. If the total cost of the path decreased, repeat the process from step 1, stop otherwise.

    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-- and h++, 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).

»
8 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

What is the purpose of making so tight requirements for G - Buratsuta 3 problem (at least in randomized solution)? You forced to remap values of original inputs, because using map is 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 :)

Image
»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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!

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

inequalityforces

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

It is still showing N/A for me for all the codes.

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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 ?

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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!

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can somebody explain why it is SUM(pi — L + i) rather than SUM(pi-L)

»
8 months ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

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

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
8 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

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()?

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
8 months ago, hide # |
Rev. 9  
Vote: I like it 0 Vote: I do not like it

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

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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?

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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 !!!!

»
7 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

At First i was unable to solve the problem Div3-E. Then i did this 2 problems from CSES and got hint.

  1. Distinct Values Subarrays (Code)
  2. Distinct Values Subarrays Exactly K(Code)

For anyone trying to solve Problem-E by his own, can try out these problems for hint.

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
#include<bits/stdc++.h>
#define ll long long int 
#define vi vector<ll>
#define vvi vector<vi> 
#define vvvi vector<vvi> 
#define pb push_back
using namespace std;

void solve()
{
   int n;
   cin>>n;
   string s;
   cin>>s;
   
   vector<int> a_pos, b_pos;
   for(int i=0;i<n;i++){
       if(s[i]=='a'){
           a_pos.push_back(i);
       }
       else{
           b_pos.push_back(i);
       }
   }
   int a_len = a_pos.size(), b_len = b_pos.size();
   
   if(a_len==0 or b_len==0){
       cout<<0<<endl;
       return;
   }
   int a_cost = 0, b_cost = 0;
   int a_med = a_pos[a_len/2], b_med = b_pos[b_len/2];
   
   for(int pos:a_pos){
       a_cost += abs(pos-a_med);
   }
   a_cost -= (a_len-1);
   
   for(int pos:b_pos){
       b_cost += abs(pos-b_med);
   }
   b_cost -= (b_len-1);
   
   cout<< min(a_cost,b_cost)<<endl;
}

int main()
{
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); 

int t=1;

cin>>t;

while(t--)solve();


return 0;
}    
   
 

Can someone why this code for problem D is failing in test#2?

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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.

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can anybody explain E to me in easier language?

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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!!

»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

It's easy to get rid of the log in the randomized solution for G by simply processing queries offline: 361324634