awoo's blog

By awoo, history, 5 months ago, translation, In English

2169A - Alice and Bob

Idea: Roms

Tutorial
Solution (Roms)

2169B - Drifting Away

Idea: BledDest

Tutorial
Solution (awoo)

2169C - Range Operation

Idea: BledDest

Tutorial
Solution (Neon)

2169D1 - Removal of a Sequence (Easy Version)

Idea: FelixArg

Tutorial
Solution (FelixArg)

2169D2 - Removal of a Sequence (Hard Version)

Idea: FelixArg

Tutorial
Solution (FelixArg)

2169E - Points Selection

Idea: BledDest

Tutorial
Solution (BledDest)

2169F - Subsequence Problem

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +26
  • Vote: I do not like it

»
5 months ago, hide # |
 
Vote: I like it +19 Vote: I do not like it

Why is the system testing stuck in 95%? It has been like that for a whole day bro :/

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

What rating was the C problem? 1200?

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

For D1, can someone explain why the answer will be max p?

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

    As I see it we should find the minimum P for which after applying X operations we are left with K or more elements and that P will serve as the answer. In the case of several consecutive sequence lengths that are designated to end with K values, we want the minimal one since this is the only one that will have its max value (which is the sequence's length) preserved along all operations. Larger sequence lengths which result in the same K mean that they add elements to the sequence which get deleted during one of the operations.

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

      Can you please elaborate on why the max value (for minimum p) will remain preserved across operations?

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

        Each P represents the series of integers from 1 to P. Define f(P) to be the number of items remaining after applying X operations on a sequence of length P.

        If f(P) == f(P - 1) + 1, that means that by increasing the sequence by one we were able to preserve one more element. Since whether an element gets deleted or not is determined solely by how many elements come before it and not after it, we can deduce that each element that will be deleted in the sequence of length P-1 will be deleted in the sequence of length P, thus the newly preserved element has to be the newly added one — P.

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

          This case will hold true for consecutive numbers only right? (i.e there are at max 2 numbers such that they go to a position k' after one operation and should be consecutive).Using f(P) defined by you: If p-f(p)==k' and p-m-f(p-m)==k', this can be true only if m==1 since f(p)==m+f(p-m) i.e floor(p/y) == m+floor((p-m)/y) which only holds for m==1 when x>=2. My question is why does min(p) never become 2nd consecutive number in one of the x operations?

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

      hey i just submitted this soln and it got accepted but i couldn't figure out like fundamentally why it got accepted , u know like i just thaught it might work , but how do i approach , how do i relate it with the editorial though?


      ll x, y, k; cin>>x>>y>>k; ll nx = x; ll num = pow(10,12); ll dk = k; if(x>=1 && y==1){ cout<<-1<<endl; return; } while(nx--){ k += ( k + (k + ( k + ( k + ( k + (k + (k + ( k + (k + (k + (k + (k + k/y) / y) / y ) /y ) / y ) /y ) / y) / y ) /y ) / y ) /y ) / y) / y; if(k>num){ cout<<-1<<endl; return; } } k = dk; if(k<y){ cout<<k<<endl; return; } while(x--){ // cout<<k<<endl; k += ( k + (k + ( k + ( k + ( k + (k + (k + ( k + (k + (k + (k + (k + k/y) / y) / y ) /y ) / y ) /y ) / y) / y ) /y ) / y ) /y ) / y) / y; } cout<<k<<endl;
  • »
    »
    5 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I dont understand this,too.

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

    answer is min p, Let the final sequence after all operations is S = [a₁,a₂,a₃,...,a_k,...] where a_k = k-th surviving number Let f(p) be the number of elements that remain after doing all x removals on the prefix 1, 2,.., p so f(a_k) = k and f(a_k — 1) = k — 1 and f(a_k+1)=k+1 so for any number (r) between a_k and a_k+1, f(r)=k, but that number r is not in the final remaining sequence so that means that number would have already been removed in the process, so sequence till r results in exactly k numbers but r is not that kth number.

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

add DP tag to C, as proven here: https://mirror.codeforces.com/contest/2169/submission/348989596

My idea is to calculate everything up to I. I use a simple prefix sum for this.

So one array represents when we have ALREADY used the operation before at some point, and the best we can get from this.

The other represents us using the operation on this index. Here our subarray will either start from the current index OR some the start of the subarray of i — 1.

I'd be happy to explain if there's something complicated

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

"If we find such a maximum p that after all of Polycarp's actions exactly k elements remain, then this p will be the answer."

Isn't it minimum? awoo

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

I don't understand how the time complexity for computing in groups is sqrt(A) for D2. Could someone maybe give me a hint?

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

wow ,great explanation

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

I disagree with sample 2 of problem A:

6 500

200 200 300 500 600 600

b>=501 gives score of 600+600=1200, whereas b=333 gives 200+200+300=700. Clearly the former is optimal.

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

    we are not adding the difference but we are calculating total points only, if Alice is more close to a given number then he gets points otherwise bob

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

    I made the same mistake during the contest, actually the score is defined as the number of balls won rather than the sum of the numbers written on them. So for this case it's 3>2 (1 for each ball).

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

      Could you explain why this is the intended interpretation of the problem statement?

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

        As per the explanation given for the first testcase — if Bob chooses 35, he gets 5 points for marbles 30,40,50,60,70. Although I agree that problem statement could have been clearer.

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

Can anyone , give me the intuition behind the problem D2 ?

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

    Let

    $$$p$$$

    be the smallest number such that

    $$$[1,2,\dots,p]$$$

    reduces to a list of length

    $$$k$$$

    after $x$ operations. The first key observation is $$$p$$$ itself is in the final list of length k we obtain (if it wasnt, it wouldnt be the minimal such $$$p$$$!)

    Now, let $$$L_{i}$$$ denote the length of the list after $$$i$$$ operations were applied. Trivially,

    $$$L_{0} = p$$$, $$$L_{x} = k$$$.

    The key observation now is that we can quite trivially obtain $$$L_{n-1}$$$ given $$$L_{n}$$$.

    Almost Any group of $$$y-1$$$ elements in $$$L_{n}$$$ was obtained from deleting a single element from a group of size $$$y$$$ in $$$L_{n-1}$$$. The ONLY exception is a grouping of size $$$y-1$$$ that contains the last element $$$p$$$. Clearly, such a group was not obtained by removing an element, as this element would appear after $$$p$$$ in the original list, but $$$p$$$ is the maximal element in the original list.

    Thus, only groups of size $$$y-1$$$ not including the last element contribute to the size. This gives

    $$$L_{n-1} = L_{n} + \left\lfloor\frac{L_{n}-1}{y-1}\right\rfloor$$$

    Once this formula is obtained it is simply a matter of computing ti efficiently. (Naive approach is O(n), too slow. But the "grouping" trick from editorial will allow for O(sqrt(n)))

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

FelixArg for D2, do you have a link to more resources regarding the floor function and re-arranging to solve for $$$p$$$? I understand why we're dividing by $$$y - 1$$$ but couldn't follow why the numerator is $$$p' - 1$$$ instead of $$$p'$$$.

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

    hope he did, i have been stuck on this for hours...

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

    $$$x=1,y=3$$$, the seq is:

    $$$1,2,X,3,4,X,5,6$$$ ($$$X$$$ means has been deleted)

    there is only one $$$X$$$ before 4, which is $$$\frac{4-1}{3-1}=1$$$, not $$$\frac{4}{3-1}=2$$$

    that's why $$$p'-1$$$ instead of $$$p'$$$

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

      Ah I was mixing up indices with the values. Your example cleared it up for me, thank you very much!

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

        Try to understand like this,we have one element removed for consecutive $$$k$$$ elements,in the function,we use the $$$k - 1$$$ as the divisor,which means $$$k - 1$$$ consecutive elements for one removed elements,but if we use $$$p'$$$ but not $$$p'- 1$$$ as the numerator,when $$$p \mod k \equiv k - 1$$$ ,we have a remainder $$$k-1$$$,but actually this $$$k-1$$$ elements can't contribute to the answer,as it only has $$$k-1$$$ elements,not enough for $$$k$$$ consecutive elements.

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

$$$A$$$ is like the situation where you and your friend are driving somewhere and you both estimate how long it will take you to get there and whoever gets closer wins, and you say something like $$$30$$$ minutes then your friend always says either $$$29$$$ or $$$31$$$ minutes. Very cool problem.

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

FelixArg Is the formula in D2 correct? Since, If we take $$$p=y$$$, then $$$p'=y-1$$$. Thus

$$$p'+\left\lfloor\frac{p'-1}{y-1}\right\rfloor=y-1\ne p$$$
  • »
    »
    5 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Can really $$$p \mod y \equiv 0$$$? If so,the p-th elemnt will be removed at once

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

      I don't get your point. can you please shed some light at how to solve such a equation with floor function? It worked for me as long as I turn the p = p' + floor((p' — 1) / (y — 1)) and got accepted but I am still confusing how we get the equation from p — floor(p / y) = p' to the previous equation.

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

Problem C can be solved using Dynamic Programming:

When we perform an operation on the range $$$[l,r]$$$, the total sum changes by $$$\Delta=(l+r)(r-l+1)-\sum_{i=l}^{r}a_i$$$. So for each value of $$$l$$$, we only need to find $$$r$$$, such that $$$\Delta$$$ is maximised. Now, the problem becomes a simple Dynamic Programming problem.

Let us define $$$pref_x$$$ as the sum of the first $$$x$$$ elements in the array $$$a$$$. We can rewrite $$$\Delta=(-l^2-l+r^2+r)+(-pref_r+pref_{l-1})$$$. As $$$l$$$ is fixed, now we want to maximise $$$r^2+r-pref_r$$$.

Let us define $$$suf_x$$$ as $$$max_{i=x}^{n}i^2+i-pref_i$$$. Then the answer is just simple $$$max_{i=1}^{n}-i^2-i+pref_{i-1}+suf_i$$$.

$$$\newline$$$

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

    Thank you for your solution it was the simplest solution I understood I recently started cf.

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

    Very intuitive solution, I was able to understand it, thanks a lot! Just a small finding (may be wrong typo), delta should be

    $$$ \Delta = (-l^2 + l + r^2 + r) + (- pref_r + pref_{l-1}) $$$
»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

D2 is actually solvable in $$$O(\sqrt{A} \cdot \log{A})$$$: 349377750

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

Genfunc approach to F:

Think of lexicographic smallest "valid" sub-sequence. Split final sequence into $$$k+1$$$ parts:

  • Part $$$i$$$ for $$$i \leq k$$$ corresponds to the block containing numbers from $$$l_i$$$
  • The last part is the free block that may contain any numbers

We can enumerate each block independently, and then multiply their EGFs to get the EGF for the full sequence, as it will correspond to concatenating them.

For the last part, the EGF is simply $$$\frac{1}{1-mx}$$$ as we can append zero or more integers from $$$[1, m]$$$.

For the $$$l_i$$$ block, we can pick the order in which the elements appear in any of $$$l_i!$$$ ways. Then,

  • Until first element pops up, we can only use $$$m-l_i$$$ "free" numbers
  • Until second element pops up, we can use $$$m-l_i$$$ "free" numbers or the first element
  • ...
  • Until last element pops up, we can use any of $$$m-1$$$ other numbers

This can also be perceived independently as concatenation of $$$l_i$$$ blocks, the $$$j$$$-th ending in the first occurrence of $$$a_{ij}$$$. Therefore, we get the EGF for the $$$i$$$-th block:

$$$ l_i! \prod\limits_{j=m-l_i}^{m-1} \frac{x}{1-jx} $$$

In other words, the answer to the whole problem is

$$$ [x^n] \frac{1}{1-mx} \prod\limits_{i=1}^k l_i! \prod\limits_{j=m-l_i}^{m-1} \frac{x}{1-jx} $$$

This can be evaluated in $$$O(n \log^2 n)$$$ with divide and conquer, or in $$$O(n \log n)$$$ with log and exp.

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

C's solution is beautiful

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

anyone knows how to arrive at the equations for C ? and also is there any proof or smtng for the alternate approach of forming array b.like we can get 2i — a[i] only if l==r , but then if we do that for the entire array a , and find the max sum subarray , how is it guaranteed to the right answer , like how is it related to (r — l + 1 ) * (l + r) ?

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

how in D1 the function is monotonic, can someone provide a proof for that..

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

    This is a speculative guess, but I think that's because $$$p-\lfloor{\frac{p}{x}}\rfloor\leq p+1-\lfloor{\frac{p+1}{x}}\rfloor$$$ (on RHS we have +1, while $$$\lfloor{\frac{p+1}{x}}\rfloor$$$ can grow by at most 1 comparing with $$$\lfloor{\frac{p}{x}}\rfloor$$$)

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

There is a non-dp solution to E where we collect the best candidates for the perimeter by integrating the cost within the coordinates and then brute force them to choose the best combination ,as the sample set of candidates we made is small the brute force does not give TLE , got the idea myself but as i am not good enough in implementation yet had to use AI. https://mirror.codeforces.com/contest/2169/submission/354519911 Can the idea be optimised?

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

My C++ solution for problem B

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

Have a different solution to D1 :

As x is small, thought of iterating over x and finding the kth element after each iteration

For x = 0, res = k

For x = 1, and onward I iteratively check

how many elements smaller that k have to be removed which will be -> (res / y) and add it to res += res / y;

Now the new res might have been increased to a new range where more elements might have been deleted,

so now I keep calculating (res / y) — (prevRes / y), and updating res by adding this term and prevRes is just the older res without this term, I keep doing this till res / y == prevRes / y.

This guarantees that we have added the count of elements that have been deleted before res in the current iteration, and have recursively added count of all the elements that have been introduced newly after adding the deleted no. count

And k + (all these deletions for each iteration) will be the kth no. after x iterations.

Example : x = 2, y = 3, k = 5

Initialize res = 5 for x = 0

x = 1 => res += (res / y); res = 5 + 1 = 6

res / y = 2 and prevRes / y = 1, so res += 1 -> 7

res / y = 2 and prevRes / y = 2, done with x = 1

x = 2 => res += (res / y); res = 7 + 2 = 9

res / y = 3 and prevRes / y = 2, res += 1 -> 10

res / y = 3 and prevRes / y = 3. End Result = 10

Submission : https://mirror.codeforces.com/contest/2169/submission/355288758

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

    I need help with figuring out the exact time complexity for this solution, to me it makes sense that it is x * number of times I'm adding those differences (res / y — prevRes / y) in each iteration until it's 0.

    I could see that they reach 0 fast but not sure of the exact tc, is it logarithmic? How can I calculate

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

    Thanks a lot! Thought of similar approach, but was not able to develop properly.

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

I think that although the formula given in the editorial for D2 is correct, the explanation slightly wrong or inadequate. The equation

$$$ p' = p - \left \lfloor \frac{p}{y} \right \rfloor $$$

does not have a unique solution for $$$p$$$ in terms of $$$p'$$$. For example, if $$$y = 3$$$ and $$$p' = 10$$$, then $$$p=14$$$ and $$$p=15$$$ both satisfy the equation. Instead of saying that we go from $$$p'$$$ to any $$$p$$$ which satisfies the equation, we should instead say that the algorithm goes from $$$p'$$$ to the unique value of $$$p$$$ such that if the operation was applied on $$$1, \dots, 10^{12}$$$ then $$$p$$$ would end up on position $$$p'$$$. This is equivalent to adding the stipulation that $$$p$$$ is not a multiple of $$$y$$$.

This value of $$$p$$$ is indeed equal to

$$$ p' + \left \lfloor \frac{p' - 1}{y - 1} \right \rfloor $$$

because the numbers $$$1, \dots, p$$$ must have had exactly

$$$ \left \lfloor \frac{p}{y} \right \rfloor = \left \lfloor \frac{p' - 1}{y - 1} \right \rfloor $$$

groups of size $$$y$$$ (remember that we stipulated that $$$p$$$ is not the last element of a group). You can also show this algebraically.

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

First solution of the 2169C is wrong. How about the case 1 3 7 9 10?

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

in problem B what is s[i]=='>' && s[i+1]=='*' answer should ne -1 but not accroding to code they have given

»
2 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

we subtract the number $$$\lfloor p/y \rfloor$$$ each time, and since $$$y$$$ is fixed, it follows that there are at most $$$\sqrt p$$$ different values of the subtracted numbers

I am a bit confused by this. Let $$$p = p_1 = 20, y = 3$$$. Then

$$$ p_2 = p_1 - \lfloor p_1 / y \rfloor = 20 - \lfloor 20 / 3 \rfloor = 20 - 6 = 14 \newline p_3 = p_2 - \lfloor p_2 / y \rfloor = 14 - \lfloor 14 / 3 \rfloor = 14 - 4 = 10 \newline p_4 = p_3 - \lfloor p_3 / y \rfloor = 10 - \lfloor 10 / 3 \rfloor = 10 - 3 = 7 \newline p_5 = p_4 - \lfloor p_4 / y \rfloor = 7 - \lfloor 7 / 3 \rfloor = 7 - 2 = 5 \newline p_6 = p_5 - \lfloor p_5 / y \rfloor = 5 - \lfloor 5 / 3 \rfloor = 5 - 1 = 4 \newline p_7 = p_6 - \lfloor p_6 / y \rfloor = 4 - \lfloor 4 / 3 \rfloor = 4 - 1 = 3 \newline p_8 = p_7 - \lfloor p_7 / y \rfloor = 3 - \lfloor 3 / 3 \rfloor = 3 - 1 = 2 \newline p_9 = p_8 - \lfloor p_8 / y \rfloor = 2 - \lfloor 2 / 3 \rfloor = 2 - 0 = 2 $$$

The unique values of $$$ \lfloor p_k / y \rfloor $$$ are $$$ 6, 4, 3, 2, 1, 0 $$$. There are 6 unique values in total. But $$$ \lceil \sqrt p \rceil = \lceil \sqrt 20 \rceil = 5 \lt 6 $$$.