WorldWarV's blog

By WorldWarV, history, 3 weeks ago, In English

Thanks for participating in Codeforces Round 1122 (Div. 3)!

Rate the contest!

2266A - Good Contest

Hint
Solution
Code (C++)
Rate the problem!

2266B - Three Piles

Hint 1
Hint 2
Solution
Code (C++)
Rate the problem!

2266C - AND, OR, Sort!

Hint 1
Hint 2
Solution
Code (C++)
Rate the problem!

2266D - Falling Concrete

Hint 1
Hint 2
Solution
Code (C++)
Rate the problem!

2266E - Prime Destruction

Hint
Solution
Bonus
Code (C++)
Rate the problem!

2266F - MEX Replacement

Hint 1
Hint 2
Hint 3
Solution
Bonus
Bonus Answer
Code (C++)
Rate the problem!

2266G - Modular Tree

Hint 1
Hint 2
Hint 3
Solution
Code (C++)
Rate the problem!

2266H - Deque Malfunction

Hint 1
Hint 2
Hint 3
Hint 4
Solution
Implementation 1: Lazy Segment Tree
Code (Lazy Segment Tree)
Implementation 2: Amortized Set Deletion
Code (Amortized Set Deletion)
Rate the problem!
  • Vote: I like it
  • +98
  • Vote: I do not like it

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

Update the announcement :3

»
3 weeks ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

all the code sections are empty. are you waiting until after the hacking, or is this a mistake?

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

Sigma round, loved so much. But not C >:(

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

For H, instead of a segtree, we can use a BIT for easier implementation: https://codeforces.me/contest/2266/submission/391534225 Finding maximum over (t,m] is the same as finding maximum on (0,m-t]

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

had a ton of fun solving F :D, my bound for the binary search was [0,n+60] which makes it a bit easier to implement than having it bound by mx, and it's pretty easy to prove. was scared I didn't handle the overflow well but I guess it was just 1 if lol

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

    I solved F after the contest but used the bounds [mxValue -> (mxValue + 32)]. Just iterate over this range and get the result. My proof is : if I want the answer to be mx + i then I need all the values in range [0-mxValue] to have frequency atleast (1 << (i - 1)). And as the max frequency is 1e9 so answer will not be greater than mxValue + log2(1e9)

»
3 weeks ago, hide # |
 
Vote: I like it +22 Vote: I do not like it

I can’t remember the last time a Div 3 contest rekt me this hard.
After looking at the editorial, I actually like this contest.

Also, my post contest discussion stream here. ABCEDFG is the order.

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

D and E had me STRESSED. Glad I succeeded in solving them, and props to you WorldWarV for this contest! The problems I managed to solve had very elegant solutions, which I appreciate.

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

I solved C using dp

Submission

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

    Likewise

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

    Can u explain the logic?

    • »
      »
      »
      3 weeks ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it
      1. Notice that s1 can never change (As s[i] = (bitwise | or &) of s[1], s[2], ... s[i]) (Put i = 1 in that equation to prove it)

      2. We want the string to be sorted in non-dec order so we know that if s[i - 1] was 0, then s[i] can be both 0 or 1 (both possibilities) whereas if s[i - 1] was 1, then s[i] must equal 1 (if s[i] = 0, then it becomes unsorted, defeating our purpose)

      3. We will go step by step trying all possibilities through recursion

      Code

      We will try to go through ALL POSSIBILITIES and find the minimum operations out of them.

      Then memoize it (just store the values of the already calculated function calls so that we can save time).

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

        You can just track the zeroes and ones that appear before the current index and after the current index, precalculate the total zeroes in advance.

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

I solved E after the contest. I didn't use DP I brute-forced the solution for each number in the array by factorizing it, and then found the formula for how many operations it will cost if I want to remove factor x from ai

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

    could you please explain your approach I want to know

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

      Let a be some number that we want to reduce such that a <= k

      One operation allows us to remove a prime number from a

      We know that any number is the product of some prime numbers.

      So assume a = p1 * p2 * p3 * p4 * p5

      Let's say that we choose to remove p1,p2,p3 from a because we know that p4 * p5 <= k

      Now all we need to know is the number of operations required to make a and all the numbers it produced equal to p4 * p5

      We start with a which is (p1 * p2 * p3 * p4 * p5)

      We divide it by p1 and now we have p1 instances of (p2*p3*p4*p5) and cost+=1

      Now we divide each one of these new instances by p2 and we end up with p1 * p2 instances and cost+=p1

      Now we also divide these p1 * p2 instances by p3, and we end up with p1 *p2 *p3 instances and cost+=p1*p2

      So in order to remove p1, p2, and p3, it cost us 1 + p1 + p1*p2.

      So I first precomputed these costs for all numbers from 1 to 2e5 and then I tried all valid possible subsets of primes to remove

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

I looked at H a bit differently, i.e. fix the start position and optimizing for the number of prefix maximums and prefix minimums.

Since these two sequences are independent excluding the starting element, we can optimize them individually.

For the prefix maximum case, if we want to add a value at position $$$j$$$ to the sequence ending at position $$$i$$$, we need to ensure that all values in $$$[b_i + 1, b_j - 1]$$$ is in $$$b[j + 1, n]$$$.

That way we can propagate from $$$j$$$ to $$$i$$$ by considering the minimum $$$b_i$$$ to be the first value not appearing in $$$b[j, n]$$$ that were less than $$$b_j$$$.

The same method can be used for the prefix minimum case.

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

Problems with easier implementations but harder ideas are so orz, one of my favorite contests so far

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

Nice round! Problem E was definitely easier than D if you know prime factors

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

Dealing with bigness of numbers in F was such a headache for me :(

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

A-B very easy

C standard prefix

D This felt very hard, All i could think of was sort with A[i]-i but couldn't do anything beyond it

E Easy dp with sieve

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

    I didn't want to look at the editorial for D, after maybe 5 hours of brainstorming, I looked at your comment, and that was enough.

    A[i]-i is the invariant that I didn't think of

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

This was my first contest, I want to know when the rating update happens??

»
3 weeks ago, hide # |
← Rev. 3  
Vote: I like it +5 Vote: I do not like it

C can also be modelled using easy dp

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

Great Contest.Bob and Alice fought again BTW

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

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

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

Well, solved till 5th question

Confortable till 4th though Thinking spf and reccursiveness was the coolest part

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

hell C

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

I solved C using dp :) 391473244

»
3 weeks ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

E was a good problem:)

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

If i were to be honest, the difficulty curve of the problems were kinda inconsistent, for example i would personally say that D was harder than E but that might just be my skill issue. (Also the tutorial for d is somewhat vague too) Overall it was a great contest tho!

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

Felt more like a div 2 than a div 3, of course problem D and E would be easier than div 2, but first 3 weren't really easier.

»
3 weeks ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Here is my $$$O(n^\frac{4}{3} \log{n})$$$ solution for Bonus E.

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

    Shouldn't this be $$$O(n\log^2(n))$$$ since the total number of divisors from 1 to n is $$$O(n\log(n))$$$?

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

      I went with a very crude approximation, by taking ~$$$O(n^{1/3})$$$ factors for a number comparable to $$$n$$$. And since the multiset has $$$n$$$ elements; We'll get total $$$O(n^{4/3})$$$ nodes, and each node costs $$$O(\log{n})$$$ to activate or deactivate.

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

      My bad, $$$O(n \log^2{(n)})$$$ is much more accurate bound for it.

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

D got AC during contest but shows TLE now :(

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

Very good contest

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

Can anyone explain the solution of problem G in easy terms and words?

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

My rough solution for Bonus E.

Spoiler
»
3 weeks ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Bonus for problem F

Idea come from bitwise,assume that input range x $$$\in [0,n)$$$ and every number with frequencey $$$10^9$$$,i define $$$n$$$ as position $$$0$$$,$$$n+1$$$ as position $$$1$$$ and so on,$$$n+i$$$ equal to position $$$i$$$,now i observe something interesting if you regard the reverse binary representation as a decimal number then the decimal number is the number of times for the x $$$\in [0,n)$$$ need to deduce to form,for example,if look like "011" the reverse is "110" which is $$$6$$$,so you need $$$6$$$ times to get number $$$n+1$$$ and $$$n+2$$$ the proof is simple,if we let any number $$$x\lt n$$$ reduce $$$1$$$ then the number of position $$$0$$$ will increase $$$1$$$,this act like a binary adding operation.

So let the maximum number be $$$n+k-1$$$ then $$$1+2^1+2^2+..+2^k=2\cdot 10^{14}$$$,thus $$$k\approx 48$$$,why $$$2\cdot 10^{14}$$$?because every number greater than $$$n+50$$$ is useless and we can make them into $$$0$$$ so this is a approximation,in real situation it can't reach

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

Explanation for G AC code

What you need?:$$$\text{Bezout indentity,dfs,gcd}$$$

  1. First we reduce the problem,i don't know how to construct the answer then i let $$$y_i$$$ is the maximum number $$$x_i$$$ can reach,then $$$ans=\sum y_i$$$ .

  2. Now,i observe something interesting,$$$y_i$$$ is independent,why?Let $$$u$$$ be the parent of $$$v$$$,then i can make $$$x_u$$$ to $$$y_u$$$ first,then after that no matter how i change $$$x_v$$$,it won't affect $$$x_u=y_u$$$

  3. from step $$$2$$$,the problem reduce to how to maximum $$$x_i$$$ to $$$y_i$$$,now i suddenly observe something,let set $$$A=\{s_v\}$$$ which $$$s_v=\sum x_v$$$ for some moment,obviously $$$|A|$$$ (size of the set) is finite,then the $$$(x_i=a_i + c_0\cdot b_i + c_1\cdot s_1 + ... )\mod b_i$$$,if you see this form you must be sensitive,because this is related to the general bezout identity,define $$$g=\gcd(b_i,s_v)$$$ then you can see that no matter i reduce how many times of $$$b_i$$$ or increase how many times of $$$s_v$$$ ,in the kernel ,always increase/decrease some $$$g$$$ and bezout identity state that i always exist such a set of coefficient such that $$$(c_0\cdot b_i + c_1\cdot s_1 + ... )=g\mod b_i$$$,for example if $$$s_1=10\cdot g$$$ and $$$b=20\cdot g$$$ then if i increase $$$1$$$ time $$$s_1$$$ and decrease $$$1$$$ time $$$b$$$ then result is $$$+10\cdot g - 20\cdot g=-10\cdot g$$$,in modular i can use add arithmetic to simulate minus arithmetic,for example if $$$b=20\cdot g$$$ and current $$$x_i$$$ be $$$constant+10\cdot g$$$,if i want minus $$$3\cdot g$$$,i can directly add $$$17\cdot g$$$

  4. Now everything is almost clear,$$$x_i=a_i + k\cdot g$$$ and $$$k$$$ is some constant,i observe that if $$$a_i\ge k$$$ then $$$a_i$$$ also produce some of the $$$k$$$,thus let $$$r_i = (a_i \mod g)$$$ then $$$x_i = (r_i+c\cdot g)\mod b_i$$$ and $$$c$$$ is some constant,how to maximize this equation?Remember that $$$b_i$$$ is a multiple of $$$g$$$ thus let $$$b'=\frac{b}{g}$$$ then apparently $$$c\ge b'$$$ is useless it will cycle back,so we can ensure that $$$0\le c\lt b$$$ then i greedily pick the largest one and i found that $$$r_i+(b'-1)\cdot g=r_i+b-g\lt b_i$$$,so we can conclude that for every vertex $$$i$$$ the maximum $$$x_i$$$ it can reach is $$$r_i+b-g_i$$$ and $$$r_i=a_i \mod b_i$$$ and $$$g_i=\gcd(b_i,s_v)$$$

  5. How to construct answer?Actually we don't need $$$s_v$$$,we need $$$g_v$$$,Define $$$u$$$ as the parent of $$$v$$$ then $$$s=\sum a_v$$$,let me remind you what $$$g_i$$$ meaning for,$$$g_i$$$ is the change of every operation in $$$x_i$$$,in other word $$$x_i$$$ always increase/decrease $$$g_i$$$,so we can say that for $$$s_v$$$ of $$$u$$$ , we have $$$s_v=s+\sum c_v\cdot g_v$$$ and $$$c_v$$$ is some constant,tell me what's this form?Bezout identity right?So let $$$g_i=\gcd(b,s,g_v)$$$ then no matter i increase how many times of $$$b,s,g_v$$$ the kernel is i always increase/decrease $$$x_i$$$ some $$$g_i$$$

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

More clean Max-segment tree solution for H AC code

You can learn from author : Al.Cash

»
3 weeks ago, hide # |
← Rev. 3  
Vote: I like it +5 Vote: I do not like it

I took a different perspective on H: it can be seen as a longest increasing/decreasing subsequence variant. Looking just at the low value side: observe that for a given value $$$b_i$$$, we can only ever avoid a malfunction if $$$1,2,\cdots, b_i-1$$$ are all present in $$$b$$$ at positions $$$ \gt i$$$; filter out all items for which this doesn't hold. On the remaining items, observe that a decreasing subsequence of length $$$k$$$ with first chosen value $$$v$$$ corresponds to insertions on the left side going from $$$v$$$ to $$$1$$$: for any un-selected element we have it malfunction at its last possible time, and our filtration rule guarantees we will be able to do this. The standard LIS approach going backwards over $$$b$$$ gives the full tradeoff frontier between $$$v$$$ and $$$k$$$; combine this with the mirrored high-value solution to find an optimal cutoff starting value. Note that this approach also makes it easy to explicitly construct the optimal series of choices.

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

D was really witty question!!

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

I have a doubt in problem E where in the given testcase where

12 3 12 10 9 8 7 6 5 4 3 2 1 12

here I think we can do it 12 operations because if we select our final multiset elements to be {1, 2, 4} then 12 : p=3 x/p=4 -> 1 ops 12 : p=3 x/p=4 -> 1 ops 10 : p=5 x/p=2 -> 1 ops 9 : p=3 x/p=3 each p=x/p=3 x/p=1 -> 4 ops 8 : p=2 x/p=4 -> 1 ops 7 : p=7 x/p=1 -> 1 ops 6 : p=3 x/p=2 -> 1 ops 5 : p=5 x/p=1 -> 1 ops 3 : p=3 x/p=1 -> 1 ops

so the minimum operation is 12 but in the test case it is mentioned as 15. please correct me if I am doing wrong somewhere

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

    You need to use prime divisor,for example your change of $$$12$$$ is $$$12\rightarrow 4 \rightarrow 1$$$ but you can't make $$$4$$$ to $$$1$$$ in one step because $$$4$$$ is not prime divisor

»
13 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

learnt something new from D and E after a long enough hiatus from CF, thanks

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

G is clever, love it :D