Igor_Parfenov's blog

By Igor_Parfenov, history, 17 months ago, translation, In English

2040A - Game of Division

Editorial
Solution C++
Solution Python

2040B - Paint a Strip

Editorial
Solution C++
Solution Python
Notes

2040C - Ordered Permutations

Editorial
Solution C++
Solution Python

2040D - Non Prime Tree

Editorial
Solution 1
Solution 2 (zap4eg)
Notes

2040E - Control of Randomness

Editorial
Solution
Solution (Notes)
Notes

2040F - Number of Cubes

Editorial
Solution Phi
Solution DP
  • Vote: I like it
  • +104
  • Vote: I do not like it

| Write comment?
»
17 months ago, hide # |
 
Vote: I like it +49 Vote: I do not like it

can someone hack my submission for D, it works purely on hopes and dreams. 295631816

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

    who are we to ruin ones hopes and dreams?

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

    My submission for D was literally hopes and dreams (I just chose random valid numbers until it was no longer possible.)

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

      Same

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

      Hey, can you tell me how did this even come to your mind to generate random numbers and then brute forcing all the available remaining numbers, did you not think that it may give TLE? Just curious to know why so many people applied such a random approach of generating random numbers.

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

        I was running out of time and couldn't think of a sophisticated way of doing it, and the number of composite numbers >> the number of prime numbers so I'd figured I'd probably be able to allocate a number within a few tries. As for why random numbers instead of just iterating starting from the next number, I thought that there was a decent chance that I'd end up with a bunch of allocated numbers close together that I would have to iterate over and waste time, so random numbers were probably safer than that option. Plus, I always have a soft spot for cool approaches and randomization definitely qualifies. :) In hindsight it was probably not necessary but it was fun nonetheless.

        Sorry for rambling, but that pretty much exactly represents my thought process within the contest.

        Edit: it turns out randomization was necessary for my approach. I submitted a solution without randomization, just using the fallback I had of iterating over all unused elements in order (using a set so it wasn't totally inefficient), and I TLE'd.

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

fast editorial

»
17 months ago, hide # |
Rev. 2  
Vote: I like it -72 Vote: I do not like it

Problems like C add nothing to the knowledge of the participants. They just test IQ.

Also, it is exactly same as this problem: https://mirror.codeforces.com/contest/513/problem/B2

Edit: I understand that many of you had fun solving the problem. But there are several participants who just recognized the pattern for smaller n's and hoped it work for larger values of n as well. Isn't it unfair to the people who really come up with and proved their solution?

I mean the testers create randomized cases just to ensure that makeshift solutions do not pass and here anyone who recognized the "pattern" for n <= 6 could get an AC!

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

    Is this the reason for so many ACs on C?

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

    the fact that it already exists is cringe and they should've checked for that. But I struggled on it for an hour, and when I managed to solve it, I think it really did add to my knowledge, and I think it's a great problem. I believe I learned a lot more from it than from D, where I literally just guessed the solution without proving it actually works

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

      That is because you are one of the few people who solved it like it should be. Many participants just observed the pattern for small n's and hoped that it will work for bigger values of n as well. Something similar happened for D also.

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

        Well, not "hoped", but it's much easier to prove a pattern than spot a pattern without seeing the permutationa

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

    Nahh man,I think apart from the fact that it was an existing problem I felt that it was a nice constructive algorithm problem which didn't rely on prior facts or knowledge.

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

      Agree 100%, really annoying that it was a previously done problem, since I had a lot of fun solving it (and it's probably the hardest problem I've ever solved, so I'm happy about that)

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

    Me, the author, and the testers didn't know about this problem, and it didn't come up in my searches :( (maybe because the formula is an image...)

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

      I feel that getting the permutation even after knowing the fact (which can be easily verified by a brute force), the construction isn't trivial. The editorial seems incomplete without it.

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

    welp that's the average div. 2 C unfortunately

  • »
    »
    17 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it +10 Vote: I do not like it

    i think C is a pretty cute problem Tbf, aside from the fact that it existed before the contest

    it felt much nicer solving C since i was able to see what was happening and wasn't making any guesses

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

    They just test IQ

    If so, all constructive problems on arrays are testing IQ.

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

    I think recognizing the pattern is a good thing. It takes skills to be able to recognize the pattern and take advantage of the pattern to solve the problem. Dynamic programming is also about recognizing the pattern.

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

oh wow, super fast editorial before system testing, thank you.

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

Thanks for the really interesting problemset. It's satisfying to see that even E can be implemented so simply without need for advanced data structures.

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

E can be solved offline in O(Q log^3 N) by using parallel binary search and HLD.

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

everyone failed E because they misread the constraints

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

Bruh I had no idea how to solve B.

Then I remembered, "If nothing makes your solution work, put it in a binary search" — me

So, I tried binary searching on how many of 1st type operations would be used.

And I got wa. But I will try to solve it using binary search

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

Here is the 2e5 version of problem E for python. It passed the test, and run within 1s with randomly generated cases on my local machine.

295640041

»
17 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
»
17 months ago, hide # |
Rev. 4  
Vote: I like it +15 Vote: I do not like it

My solution of D with the proof:

  1. Find the center of the tree, do dfs that starts from it.
  2. Sort all vertices by (depth, dfs_visit_time) descending order as $$$v_1, v_2, \cdots, v_n$$$.
  3. Assignment $$$a_{v_i} = 2 \times (i + 1)$$$ for $$$v_1, v_2, \cdots, v_{n-1}$$$,and $$$a_{v_n} = 2$$$.
  4. If there is a conflict that $$$v_1 (a_{v_1} = 4)$$$ is a neighbor of $$$v_n (a_{v_n} = 2)$$$,assignment $$$a_{v_1} = 1$$$.

Proof:

Lemma: Since we do dfs from the center, every layer has at least two vertices if the tree has two centers. Otherwise only the layer of the center has only one vertex.

$$$v_i$$$ must be not the neighbor of $$$v_{i+1}$$$ if they have same depth. In the case they have different depth, they can't be adjacent after the sort because the lemma (except $$$v_n$$$ when it's a center). So $$$v_1, v_2, \cdots, v_{n-1}$$$ must be a legal solution.

So we just need to care with $$$v_n (a_{v_n} = 2)$$$. It can only conflict with $$$v_1 (a_{v_1} = 4)$$$. If they are adjacdent, we just assignment $$$a_{v_1} = 1$$$ because $$$v_1$$$ is always a leaf and not about other vertices ($$$v_2, v_3, \cdots, v_{n-1}$$$).

Code: 295631636

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

B was nice!

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

ok i am dumb i forgot about last 1 after "1"

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

    Place 1s at indexes 1 and 4

    Fill the indexes 1 through 4

    Place 1 at index 10

    Fill the indexes 1 through 10 (This solves your second question)

    Place a 1 at index 20

    Fill the indexes through 1 through 20

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

    3 from 10: initial: 0000000000 after 1: 1000000000 after 2: 1001000000 query type 2: 1111000000 after 3: 1111000001 query type 2: 1111111111

    20 does the same steps up to there but adds one more initial (3 operations done): 11111111110000000000 after 4: 11111111110000000001 query type 2: 11111111111111111111

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

    Consider this string: 1001000001 Where the flipped bit are those set using first operation. Now, you can flip the first two bits in between (because there are a total of 4 bits, 2 of which are 1, so the constraint ceil(4/2) >= 2 is satisfied). Then the string becomes: 1111000001 Now, just choose the first and the last bit; l=1, r=10, the constraint ceil(10/2) = 5 >= 5 is satisfied, so we can flip all other bits. Just do it iteratively and you have the solution.

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

E can be solved in O(n^2 + q). Link

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

dreams are illusion or future

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

Cannt problem E be solved with $$$Q \le 10^5$$$ also.

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

For problem D my solution simply chooses values randomly for each node of the tree in DFS order, repeating until the difference with the node's parent isn't prime.

This works because when $$$n$$$ is large enough, $$$n \, » \, \frac{n}{\ln n}$$$ (or even $$$\frac{2n}{\ln n}$$$), which means there's always a significant fraction of the remaining numbers that would result in a non-prime difference. During the contest I added some logic to potentially restart from scratch and re-randomize if needed for small $$$n$$$, but it seems like you don't even need that: 295643399

Another benefit of this solution is it will work for smaller bounds such as $$$1.5n$$$ instead of $$$2n$$$.

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

nice problems, finally reached pupil!

neat solution to problem C... I found the greedy strategy for problem C but couldn't find a way to implement it other than calculating 2^(n-2), which would not work with the constraints. sad that I could not solve it even after finding the correct logic ;(

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

Hey ladies and gentlemen, this is my code for verifying how the dynamics of the C's solution work https://www.ideone.com/fQB1zU

and following is the submission link of the AC verdict for the corresponding Que' https://mirror.codeforces.com/contest/2040/submission/295645589

hope you understand it, if not, ping me back freely!

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

Alternative solution for problem E:

Let us solve the problem where $$$p=0$$$ for all queries. Consider two functions $$$A_v$$$ and $$$B_v$$$ where $$$A_v$$$ is the expected number of moves to reach $$$1$$$ if we are currently in $$$v$$$ and $$$i$$$ is odd and $$$B_v$$$ is the same as $$$A_v$$$ but $$$i$$$ is even. $$$A_1=B_1=0$$$, since we are already at $$$1$$$ so we require zero moves.

If $$$i$$$ is odd, then we move to the parent with probability $$$1$$$, hence $$$A_v=B_{\texttt{pr}}+1$$$, where $$$\texttt{pr}$$$ is the parent of $$$v$$$.

If $$$i$$$ is even, then we move to any neighbor vertex with the same probability, hence $$$B_v=\sum\limits_{u \in N(v)}\frac{A_u}{deg[v]}+1$$$. Let us simplify this formula as $$$B_v=\frac{A_{\texttt{pr}}}{deg[v]}+\sum\limits_{u \in \texttt{children}(v)}\frac{A_u}{deg[v]} + 1$$$. Notice, that $$$A_u=B_{\texttt{v}}+1$$$ from the $$$A_v$$$ recurrence, hence $$$B_v=\frac{A_{\texttt{pr}}}{deg[v]}+\sum\limits_{u \in \texttt{children}(v)}\frac{B_v+1}{deg[v]} + 1$$$ The sum does not depand on $$$u$$$ anymore, so we have $$$B_v=\frac{A_{\texttt{pr}}}{deg[v]}+\frac{deg[v]-1}{deg[v]}\cdot (B_v+1) + 1$$$.

Solving this equation for $$$B_v$$$ we have a formula $$$B_v=A_{\texttt{pr}}+2\cdot \texttt{deg}[v]-1$$$.

We have formulas for $$$A_v$$$ and $$$B_v$$$ that depend only on the parent of $$$v$$$, so can run a simple dfs to compute the values for all vertices. The answer for the query is $$$A_v$$$, since we start from the odd $$$i$$$.

But in the original problem we have some amount of coins. The solution doesn't change at all, we just add another parameter to our functions that responds to the number of coins we have, $$$A_{v,p}$$$ and $$$B_{v,p}$$$. The transitions are the same, but we can also spend a coin when updating $$$B_{v, p}$$$, so $$$B_{v, p}$$$ may be $$$A_{\texttt{pr}, p-1}+1$$$.

Precomputing $$$A_{v,p}$$$ and $$$B_{v,p}$$$ for all $$${v,p}$$$ takes us $$$O(n^2)$$$ and answering the queries $$$O(1)$$$.

Implementation: 295642233

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

C was tragic for me, I actually found the "left or right " construction, I just couldn't get it to work properly, i need to work on constructives

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

Damn, in D I thought that 1 is considered to be a prime number...

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

I am failing to understand E, if anyone could help me understand it in easier way. Thanks before hand

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

problem E: I thought to build a vector of expected values $$$d$$$, where $$$d_N$$$ is the expected number of moves from a node with $$$N$$$ neighbors to go up (towards vertex 1) of exactly one step, so the parent, when the current counter parity is even. So:

$$$d_N = \frac{1}{N} + \frac{N-1}{N} (2 + d_N)$$$

the first term is the case in which the robot is lucky and goes up, that takes 1 step with probability 1/N; the second one is the probability of going down to one of the $N-1$ below standing children, than going up (2 moves) and the expected value $$$d_N$$$ itself. So, at the end the expression for $$$d_N$$$ is:

$$$d_N = N\frac{2N-1}{N-1}$$$

But it is not correct. Can someone explain me why please?

UPD: the first equation is correct, the final computation is not. As harshaa005 sais:

$$$d_N = 2N - 1$$$
  • »
    »
    17 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +1 Vote: I do not like it
    $$$ d_N = \frac{1}{N} + \left(2 + d_N\right) \cdot \frac{N - 1}{N} $$$
    $$$ d_N \cdot N = 1 + \left(2 + d_N\right)(N - 1) $$$
    $$$ d_N \cdot N = 1 + 2(N - 1) + d_N \cdot (N - 1) $$$
    $$$ d_N \cdot (N - N + 1) = 2(N - 1) + 1 $$$
    $$$ d_N = 2N - 1 $$$

    Your initial equal is correct. Somehow you must have done some mistake in calculation. I have used the same equation in my submission.

    295662501

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

On C I calculated the S(p) up to n = 8 to see the pattern. I did realize that the smaller numbers always ended up in one of the ends of the array and that they had a sort of logarithmic behaviour, like, the permutation changed based on powers of 2.

Though I had no idea how to enumerate them, and to realize afterwards that you can just decompose K into bits and just decide the position based on that is just crazy. Awesome problem, even if it costed me my Specialist.

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

    literally same, i found the pattern, not by proof, but in a sort way where i was like, the more big stuff on one side the better, this is probably right and we cant make both sides big for each number, and in a notes document i wrote: "case on dropping to the left or the right, left makes smaller, right makes bigger." however, when I went to check it against samples for "4 6" i considered binary 6 instead of binary 5, so it wasnt right, I gave up on the idea, bricked my mental, and failed the contest

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

For Problem C I printed set of all permutations and tried to recognize a pattern. But Had a very difficult time imaplementing it, and also totally overlook that n is large and I cant use 1<<(n-1). That was my first experience handling such tricky constraints... Really learned so much in this single problem!!!!

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

    Wow, I was just speaking about this point, but I think I handled it in a different way and yes I didn't have time to finish my work in the contest coz of this trick and another trick

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

I solved C in a different way, I just generated all of the permutations with the score of the maximum and the score of the maximum is the score of this permutation that you put the highest one in the middle then on its left the next maximum the on its right the next maximum and so on, so something like this will happen if n = 7

1 4 6 7 5 3 2

then this gave me this result when used n = 5

(1, 2, 3, 4, 5) (1, 2, 3, 5, 4) (1, 2, 4, 5, 3) (1, 2, 5, 4, 3) (1, 3, 4, 5, 2) (1, 3, 5, 4, 2) (1, 4, 5, 3, 2) (1, 5, 4, 3, 2) (2, 3, 4, 5, 1) (2, 3, 5, 4, 1) (2, 4, 5, 3, 1) (2, 5, 4, 3, 1) (3, 4, 5, 2, 1) (3, 5, 4, 2, 1) (4, 5, 3, 2, 1) (5, 4, 3, 2, 1)

and from here I saw they are being repeated by powers of 2 until they reach n

look at the first item in the first 8 places it is 1 and then the others will be 2 in the next 4 perms

and so on, so I coded this pattern

then any number less than n will be printed by just looping from n to 1 and add any not-added-yet numbers

My Submission

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

For problem E I can't figure out what is wrong with my approach. I see the editorial did something different, but I cam up with geometric distribution to solve it, and it gets till test case 4 and fails on the 138th query.

submission

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

    You have a problem with dp. When you have several paths to the state, you set the value from the last path. Instead, assign the minimum value.

    Change dp[u] = dp[v] + x to dp[u] = min(dp[u], dp[v] + x)

    Also, you don't need modulo here, as the answer is always $$$\le$$$ n.

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

      That worked, I was so close with my approach, instead of treating each movement as odd-even step, I just tracked the parity in the dp states, and the possible transitions based on the the current parity.

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

In the Editorial of D:

$$$\text{We will write the values } n\cdot 2,n\cdot 2−1,n\cdot 2−2,\cdots \text{ to the vertices with odd depth in breadth-first order.}$$$

Maybe it is a mistake and should it be:

$$$\text{We will write the values } n\cdot 2,n\cdot 2−2,n\cdot 2−4,\cdots \text{ to the vertices with odd depth in breadth-first order.}$$$

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

Hi, could I check why in the first if block for C we must check for n <= 60? I'm unsure of the intuition behind it.

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

    because for n>60 ,2^n will always be greater than k, so ans will always exist. So there is no need to compute 2^n for n>60 and also it will give integer overflow even in long long int.

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

In B if n=11 and i make arr1=1 and then i make arr5=1 so for now two first type operations are needed and then i apply second type of operation(5/2<=2) and then applying in arr11=1 cant i get it in 3 operations only why the answer is 4 plz someone tell

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

    $$$\lceil$$$ and $$$\rceil$$$ represent ceil operator, so $$$\lceil {5 \over 2} \rceil$$$ is $$$3$$$, not $$$2$$$.

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

Here is how I solved problem C. Initially, I made some observations:- How will i get the max S(p)? -> By making the contributions of smaller numbers the least possible. How will i do this? -> starting with the current smallest no(which initially is 1) i can prove that it has to be placed either on the extreme left or to the extreme right of the current empty array. This can subsequently be done for the next current smallest values, all follow the same thing.

So getting the kth permutation:- When will the ans not exist? -> when k > no of perm which gives the max value (2 ^ n-1); As k<=1e12, this is only when n-1<40.

Now for each number in the permutation, we have two possibilities either place it in the first or last of the empty array. When right? -> temp = (1LL<<(n — cursmallest — 1)) if(k>temp) then cursmallest will be at the last and simultaneously update k = k — temp;

Elsewhere cursmallest goes to the left position.

Implementation 295706641

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

The Editorial of problem D contains a typo in my opinion. The second solution should have "n.2, (n-1).2, (n-2).2,..." instead of "n.2, n.2-1, n.2-2,...". Please verify this once Igor_Parfenov

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

can someone explain C in more detail? i recognized the pattern but don't know how to generate the kth one. these are some first permutations for n = 5

permutations for n = 5

I might've missed some here...

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

    Here are all the permutations that work for n = 5

    (1, 2, 3, 4, 5)
    (1, 2, 3, 5, 4)
    (1, 2, 4, 5, 3)
    (1, 2, 5, 4, 3)
    (1, 3, 4, 5, 2)
    (1, 3, 5, 4, 2)
    (1, 4, 5, 3, 2)
    (1, 5, 4, 3, 2)
    (2, 3, 4, 5, 1)
    (2, 3, 5, 4, 1)
    (2, 4, 5, 3, 1)
    (2, 5, 4, 3, 1)
    (3, 4, 5, 2, 1)
    (3, 5, 4, 2, 1)
    (4, 5, 3, 2, 1)
    (5, 4, 3, 2, 1)
    

    Here are a couple of observations that I made, bolding the ones that were fairly important

    • A reversed permuation has the same score as the original

    • An element can only appear in the final equation at most (max_element-n+1) times, e.g if the permutation was of length 5, 5 could appear at most once, 4 could appear at most 2 times, etc. Therefore, in order to ensure that the sum is maximized, an element must appear the maximum amount of times that it is able to.

    • the first element from the back will be fixed for 1 permutation, the second element from the back will be fixed for 1 turn, third element 2 turns, fourth element 4 turns, and fifth element 8 turns (with fixed meaning up to that point, it's in the same position that it would be if the permutation was sorted)

    • An element will be a prefix or a suffix of the elements up to that point, e.g in the case where n = 5, 4 could either appear before or after 5, so the possible values of that subarray are 4 5 or 5 4, then once we get to 3, it can be 3 4 5, 3 5 4, 4 5 3, 5 4 3

    • Whether the nth element from the back is at the start or end of the elements up to that point depends on if the modulo value of k by 2^n is greater or less than 2^(n-1) (if it's less than 2^(n-1), it should be added at the front, otherwise at the back.

    I hope this helped (sorry if it's hard to understand, I know I'm not the best at explaining)

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

      really didn't understand the 5th point. i couldn't make the 4th observation but i had all the work done before that

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

        I'll use the example of n = 5 and k = 9

        Let's start with an empty list (I used a deque in my code, but a list will work to show how it works)

        (I'll mention that I subtract 1 from k to perform the calculations, so k is equal to 8 in my calculations)

        Now let's iterate from 5 to 1 and add the elements to the list

        Since 5 is the first element from the back, we will check if k % 2^0 < 2^-1 (8 % 1 < 0.5). Since it's less than 0.5, we add it to the front, so the list is now [5]

        Now let's go to 4, and check if k % 2^1 < 2^0 (8 % 2 < 1). Since this is true, we add it to the front, and the list is now [4, 5]

        For 3, k % 2^2 < 2^1 (8 % 4 < 2) is true, so we add it to the front, and the list is now [3, 4, 5]

        For 2, k % 2^3 < 2^2 (8 % 8 < 4) is true, so we add it to the front, and the list is now [2, 3, 4, 5]

        For 1, k % 2^4 < 2^3 (8 % 16 < 8) is false, so we add it to the back, and the list is now [2, 3, 4, 5, 1]

        Our final list is [2, 3, 4, 5, 1]

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

Problem 2 — Can someone explain me the 4 cuts for n=20? My understanding says we require atleast 5 operations of type A performed on i=1,4,8,16,20.

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

fun fact: problem E can be solved without modulus operation.

Submission: 295789260

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

I learned something new from problem F

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

can somebody explain c in detail

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

    Consider how many times each element appears in formula. For example, for n=9 it's [9, 16, 21, 24, 25, 24, 21, 16, 9]. This means that smallest number should be placed at the edges, to affect less segments. After you placed $$$x$$$, it will change number of segments $$$x+1$$$ will affect, so this can be viewed as placing numbers starting from $$$x+1$$$ in empty array of length $$$n-1$$$.

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

For problem B The tutorial/system gives answer = 3 for n = 5

But, Let's say I want to put 1 at the index 1 and 4. Means the intermediate array looks like [1,0,0,1,0]. Here because of the operation 2 the array will become [1,1,1,1,0], correct? and at last you choose R = 5 and L = 1. With this your entire array will become [1,1,1,1,1]

So total of 1st operations used is equal to 2, right? Instead of 3.

Can someone correct me where I am missing.

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

    In order to apply operation of type 2, both a[l] and a[r] should be equal to 1. In your case, a[5] is 0, and hence you cannot apply the operation of type 2.

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

      if you directly put a[1]=1 and a[5]=1 using first operation, i can apply second operation directly to get [1,1,1,1,1] (since 1+1=2=[(5-1+1)/2], and i assume [.] to be floor. shouldn't the answer be 2 then?

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

        nvm it is ceil, question did not specify it

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

          Apologies, I checked this notification after 5 months. I have completely forgotten about the question at this point of time. xD

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

Late note: problem E can be solved in $$$\Theta(n + q)$$$ using the fact that $$$\sum d_u = \Theta(n)$$$.

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

How do you even think of C? I can understand the editorial and follow along, but the idea is just so unexpected.

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

For D, I solved it by finding the diameter of the tree. Say we got a diameter from node u to node v with $$$m$$$ vertices on it, then we just assign the values 1, 2, 3, ..., m to the vertices on the tree. After that, we loop through the diameter's vertices, explore all branches, and assign the remaining values (i.e., from $$$m + 1$$$ up to $$$n$$$) to vertices on those branches. The assignment is simple: at each vertex, we find the first remaining value that satisfies our condition and assign it to it.

Solution implementation: 325626482