dreamoon_love_AA's blog

By dreamoon_love_AA, history, 2 months ago, In English,
Tutorial is loading...
author's code:
Tutorial is loading...
author's code
Tutorial is loading...
author's code
Tutorial is loading...

UPD: onthefloor provides proof for what I mention here.

author's code
Tutorial is loading...

a super simple solution which is differet to this blog provided by Swistakk.

author's code (from bottom to top with min heap)
isaf27's code(from bottom to top with sorted array)
author's code (from top to bottom)
Tutorial is loading...
author's code
Tutorial is loading...
isaf27's solution
author's solution
 
 
 
 
  • Vote: I like it
  • +116
  • Vote: I do not like it

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

Check out the video editorials

Also, you should add the codes under spoiler dreamoon_love_AA

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

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

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

I had different approach for Div2B, which is probably more intuitive.

Lets calculate two values:

$$$rpref[i]$$$ — the number of different elemtens in ith prefix.

$$$rsuf[i]$$$ — the number of different elements in ith suffix.

So lets fix l1. Our task is to check that sequences from [1 to l1] and from [l1 + 1 to n] are permutations. How to check this?

Sequence from [1 to K] is permutation if and only if sum(1..k) = k * (k + 1) / 2 and rpref[k] = k. We can do same procedure for suffixes.

My submission: https://codeforces.com/contest/1330/submission/75366634.

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

    I thought the same maybe I missed something X0 here

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

    How is it more intuitive?

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

    hey my logic was same as yours but i was unable to find how to correct it :( https://codeforces.com/contest/1330/submission/75427713 please correct my code . thanks in advance

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

      Hey u have declared pre and suf array as integer but the sum might exceed max value of integer.Declare it as long long

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

    I used the same approach but solution is failing on test 3 can you please look at the solution https://codeforces.com/contest/1330/submission/75404652

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

    My idea is similar to the one you use:

    I do not calculate to:

    rpref[i] — the number of different elements in i-th prefix. rsuf[i] — the number of different elements in i-th suffix.

    Because it is also not difficult to see that for a solution to exist no number can be repeated more than twice, so what I do is check that this is true, and then I say that: Sequence from [1 to k] is permutation if and only if sum(1..k) = k * (k + 1) / 2 and max element in the sequence (1...k) = k. We can do same procedure for suffixes.

    My submission with Prefix Sums: 75496699 My submission with Wavelet Tree: 75442213

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

    How to count different elements in fast way? I thought the same but time limit

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

      There is a standard way to do this: Keep a variable called numDistinct; Also use an array or map count[] where you increment count[x] whenever you see x. Then after some increment of count[y], check whether count[y]=1. If so, then this is the first time seeing it, so increment numDistinct.

      The same thing can be done when removing elements. Then you would decrement count[y], and afterward check if it became 0; if so, then decrement numDistinct.

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

      nlogn way to do this is to input everything in a set and check set size, it's O(n) if you're using a hashmap(java and python), otherwise use the method detailed in the other comment

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

    Hi, I had thought of the same approach, but my code is failing on TC8, My submission

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

    I had a similar ideia (prefix and suffix arrays), but in $$$O(nlogn)$$$ using a set, it's pretty easy to understand:

    Let's say you have $$$n$$$ elements in a set and smallest element is $$$1$$$ and biggest element is $$$n$$$, then it's easy to see that the elements in this set is the same of a permutation of size $$$n$$$, since there are $$$n-2$$$ elements between $$$1$$$ and $$$n$$$ and they can't repeat so Pigeonhole principle -> exactly each element from $$$2$$$ to $$$n-2$$$.

    Now how does that help?

    Iterate through prefixes and insert $$$i$$$-th element into set. If the size of the set is equal to the length of the $$$i-th$$$ prefix, first element is $$$1$$$ and last element is $$$i+1$$$ (0-based index), then you have a permutation so you can set $$$prefix[i] = true$$$

    Do the same for suffixes.

    Now you can split sequence into two permutations at the $$$i$$$-th position if and only if $$$pref[i] == true$$$ and $$$suf[i+1] == true$$$.

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

    Heyyy, hope you are doing well...

    I went through your approach in 1330B-Dreamoon Likes permutations. https://codeforces.com/contest/1330/problem/B

    Actually I was trying out a similar approach like.... if I get the prefix sum = i(i+1)/2 and the other part( total sum — prefix )also matches one such sum, I am just checking whether any no.is repeated or not as 4+3+2+1 = 4+3+3. I am checking by the logic that suppose the no.s are 4,2,3,1 then the count of them should be max of them, i.e 4. In case of 4,3,3 the count will be 3. Here is my solution... https://codeforces.com/contest/1330/submission/77888159 Can you just help me out ? @dreamoon_love_AA I'm getting wrong in the 1454th case of sample 2. Can you kindly help ?

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

Nice contest, D1B is beautiful!

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

    I've been trying to make sense of this problem for long. Thanks!

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

    thanks div2C explanation is really helpful.

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

Amazing contest.

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

Somebody explain Div2D in smaller steps?

I understand that we need to choose a[i] in a way that we add at least one bit to b[i-1], and we can remove all lower bits. How does this lead to closed formular for fixed a[0], or sum of "some" a[]s?

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

    Suppose x is some element of our sequence (say at index u). Suppose the i-th bit of x is set . Now if for every other element of the sequence, the i-th bit is 0, the prefix xor array(considering i-th bit only) will be 0 till (u — 1) and 1 after. If there exist some other element (say at index v) with i-th bit set, then the prefix xor array will be 0 till u — 1, 1 from u to v — 1 and 0 after v. This violates the strictly increasing property of prefix xor array. Hence there should be a unique element with a particular bit set. Now since a(i) > a(i — 1) and a(i) and a(i — 1) do not share any common bit, MSB(a(i)) > MSB(a(i — 1)).

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

what the shit u have written for div2 c. how to prove it. bsdk editorial to thik se likha kar sale. paise leta hai to thik se likh

dreamoon_love_AA

»
2 months ago, # |
Rev. 4   Vote: I like it -35 Vote: I do not like it

Good contest with challenging problems!

»
2 months ago, # |
Rev. 4   Vote: I like it -82 Vote: I do not like it

great contest!

»
2 months ago, # |
  Vote: I like it -28 Vote: I do not like it

plz can somebody tell why this gives wrong ans? https://codeforces.com/contest/1330/submission/75438529

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

i can not understand div2 C problem correctly. can anyone help me in this plzz. the editorial is quite fast.

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

    li in i-th operation means in this operation, you can color li numbers of cells, you can start with any position as long as there are no less than li cells after this position and cells will have the color from the latest operation. So the task is to find the starting position for every operation so that you can color all cells and every colors are used at least once.

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

      thank you for your help. now i understand how to solve it. thank u.

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

        The question states that each color must appear at least once.How many colors are there?

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

    You can see the tutorial one more time. I add some detail about this problem.

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

      thank you very much. nice problem set. specially div2 C.

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

      I have some issue in understanding problem Div2-C

      In test case 2 2 1 2

      n = 2 m = 2

      output should be 2 1

      Why is it "-1"?

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

        In your solution, the first color will be covered by the second color. In the end, there is only one color.

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

I loved the contest. Thank you dreamoon_love_AA

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

Can anyone tell what was wrong with this submission? https://codeforces.com/contest/1330/submission/75383926

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

    for input like 1 2 1 2 3 4 ans is 2 4 from both ways by your code and each ans should be unique

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

A problem was the most wonderful problem i have seen.

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

Thanks for the wonderful tutorial,Ienjoy this competition very much ^_^

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

I liked the contest very much, but I wish the pretests can be better next time.

Thank you for the round,and thanks to Denis aramis Shitov.

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

I thick that Div.1B is much more difficult than Div.1C.

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

    Tbh I think it depends on how good you are in counting problems. I found divB easier because I'm not that good in implementing even though I had the algo for div1C

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

Could anyone explain why my 2C is wrong? Thanks! Link: https://codeforces.com/contest/1330/submission/75413150

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

What a great contest!

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

Why "For each i, posi=max(i,n−suffix_sum[i]+1)"?

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

    Same question (about 2C/1A). Seems like magic — can anyone explain the intuition?

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

    One observation is that for any valid solution we can adjust it so that if i<j holds then pos[i]<pos[j] also holds. Thus we can do it greedily. First, we make pos[i]=n-suffix[i]+1 for every i. After doing this, we may find pos[1] < 1. Then we need increase pos[1] to 1 and then increase pos[2] to 2 if pos[2]<2 then increase pos[3] to 3... Iterate this until we find pos[x]>=x.

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

How does my submission for Div2 b 75433741 get TLE, while another code 75420830, which I think has similar complexity to mine, get accepted?

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

Nice Contest!Good problems.

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

Can anyone tell what was wrong with this submission?problem B. checker comment:_wrong answer Integer 0 violates the range [1, 5] (test case 324) https://codeforces.com/contest/1330/submission/75450052

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

can someone please explain div2 A problem. How does this collection in first example 2 and 4 after 5 as v?

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

    After adding 2 and 4 u can get rank 1,2,3,4,5,7,10. but largest v (where we get all ranks from 1 to v ) is 5. therefore answer is 5.

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

Weak pretests for Div2A unfortunately :(

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

In 1330B — Dreamoon Likes Permutations , according to author solution if we give test case like size of array is 5 and array elements — {1,2,3,4,5}. Then ans is 2 0 5 5 0 But it is said the two permutations are of non-zero length. Isn't this a contradiction? Answer shoud be 0. Please correct me where I am wrong.

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

Could someone please explain how we arrived at this formula for Div 2C? For each $$$i, pos_i=max(i,n−suffixsum[i]+1)$$$.

EDIT: Formatting

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

Problem B(Div2) also solvable by using the prefix sum. Here's my submission: 75458331

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

Problem B can be easily solved using rolling hash!

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

Div2-D/Div1-B, I dont't know how to solve it, but i just print the answer for the first 16 number using a program I. And I found regular pattern in it (table I). i-th row in table I is the number of every possible xor-sum of the case n = i.

Observation I: $$$f(x)$$$ represents the number of the case n=x, then $$$f(x) = f(x-1)*2$$$ if x is qual to lowbit(x) where lowbit(x) means x&-x. The lowbit of 0b100100 is 0b100.

Observation II: $$$g(x)=highbit(x)$$$, highbit means the highest-bit in the number. The highbit of 0b10100 is 0b1000 and the highbit of 0b11101 is 0b10000. Then $$$f(x) = f(x-1) + f(g(x)-1)$$$ if x is not equal to lowbit(x). So $$$f(x) = f(g(x)) + (x - g(x))f(g(x)-1)$$$ if x is not eual to lowbit(x).

Then I write the solution by the observation. Solution in contest

Thanks for the fortune make me become candidate master.

Program I
Table I
»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone please tell me what is wrong with my submission for Div.2B? Similar idea with editorial but simpler implementation with 2 sorted vectors?

My Submission: https://codeforces.com/contest/1330/submission/75460697

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

Problem D reminded me of Bookshelves. The reason it reminded me of it is because both problems require us to divide the given problem into 30 sub problems (based on the number of bits) and then solve that sub-problem with a DP. When I saw Bookshelves, I was very surprised at the idea. But introspecting about it was the reason I was able to solve this D.

Here are my solutions, in case anybody wants to refer them

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

https://codeforces.com/contest/1330/submission/75403704 Getting wrong answer on B, can someone help?

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

For Div1B sequence can be found on OEIS. This is the differences between answers for $$$d$$$ and $$$d+1$$$. Source code for python available on OEIS too in bottom of the page. I copy-pasted this code to my program and got accepted during contest. Idea to search on OEIS came to me on second hour, because I believed to author when read the statement that it can't be found on OEIS.

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

    I think what you mean is the sequence of "distinct" differences between answers for $$$d$$$ and $$$d+1$$$. You are right. I didn't found it when I test it during preparing the contest. It shows that I still need to train myself about the ability of searching in OEIS. (>__<)

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

In Div2 D, I think there is a typo in the example of editorial. For example, when n = 6 , it should be d=6 . 'n' is the size of a matrix.

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

I think the expression of DIV2B is ambiguous.

In the statement of the question:"Now Dreamoon concatenates these two permutations into another sequence a of length l1+l2. First l1 elements of a is the permutation p1 and next l2 elements of a is the permutation p2."

This sentence misled me into thinking that a is made up of two "permutation", but the a in the test data may be 1,2,3,4... or 1 1 1..... Obviously, in the test data, a is random, not made up of two permutation.

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

The DIV2B problem was very tricky.

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

Love how you explained the constructive algorithm in 1329A so thoroughly! I didn't find a solution in 2 hours, just as you say, the conditions are way too 'open'. Your explanation gives me new insight on how to approach problems like this.

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

dreamoon_love_AA can you please explain it more for coloring problem.

There exists some i such that li+i−1>n. In this condition, at least one of the first i−1 colors will disapear after i-th operation.

how to prove it.

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

    If there exists some $$$i$$$ such that $$$l_i+i-1 > n \Leftrightarrow i > n-l_i+1$$$, the $$$i-th$$$ operation must be done at position $$$p_i < i$$$, that is because $$$p_i \le n-l_i+1 < i$$$. In this case, it will overlap other colors and make them disappear. Hope that help!

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

      i m not satisfied with the tutorial.

      What if the test case is 6 5 1 4 3 4 2

      As per tutorial it gives -1.But still the solution is possible as 1 2 4 2 2 which is possible.

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

        why's that, the 2nd colour and the 4th colour have the same length and the same p[i] (as you said), then the 4th colour will surely overlap the 2nd one. (or did you have a typo i think?)

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

          try to colour as i have mentioned in the answer. There will be one colour of each m which is the requirement in question.

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

    This means that n — li < i — 1. Now, whenever you colour li blocks, exactly n — li blocks remain uncoloured. If this number is less than i — 1, it means that less than i — 1 blocks remain uncoloured after this operation. But in the previous i — 1 operation, you have put i — 1 colour and even if each of them occupies just 1 block, it will disappear after this operation.

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

what is a1,a2,...an in div 2 D / div 1 B pls explain.....

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

In Div2 problem B (1330B), i don't understand what "p+len1" mean, can anyone help me? (line 16)

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

    p+len1 means reference to "array p at index len1" so a[0] will point to p[len1]

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

In Div2-C, how do we know that after applying "sort" constraint, our problem won't become unsolvable?

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

    No need to sort SEE HERE

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

      In my opinion, the output of your solution is also "sorted".

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

        In a sense yes. But great problem though ,feels good to solve this one even after contest

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

    You won't know before you try it. That's just like We don't know whether we can solve a problem before we think out a solution. When solving problems, just like do DFS on the thought until you arrive at the correct solutions. apply "sort" constraint is just one possible try on this problem. You can also try many other things such as all $$$p_i$$$ are equal or $$$p_i$$$ is from larger to smaller.

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

For those who are facing the wrong answer on test case 2 in 1330B — Dreamoon Likes Permutations try for test cases whose l1 and l2 are equal. For example a=[1,2,2,1] or a=[2,1,2,1]. In this case, the output should be 1 2 2 and not 2 2 2 2 2 or anything similar. Click on this to view my solution.

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

Nice contest . This is how contests should be — 5 problems and 2 hrs. max . Nice , compact and standard problem statements and conducted very well . Although I believe pretests should have been stronger . This is my first comment ever and I wrote it to appreciate your commendable work .

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

Was scrolling through the comments and didn't seem to find someone giving a proof on the correctness of Div 1B/Div 2D (that is, the sequence is valid if and only if $$$h(a_i)<h(a_{i+1})$$$ always holds.

If $$$h(a_i)<h(a_{i+1})$$$ always holds, then $$$a_i<a_{i+1}$$$ for for each $$$i$$$ so the first condition is true. To show that this would imply $$$b_i<b_{i+1}$$$, for each $$$i$$$, $$$h(a_k)<h(a_{i+1})$$$ for all $$$k\le i$$$. It then follows that $$$h(b_k)<h(a_{i+1})$$$ for all $$$k\le i$$$ (by the definition of XOR), and therefore $$$h(b_{i+1})=h(a_{i+1})>h(b_k)$$$ for all $$$k\le i$$$: the first equality is because $$$b_{i+1}=a_{i+1} \otimes b_{i}$$$. It then follows that $$$b_i$$$ is also increasing.

Now we show that $$$h(a_i)<h(a_{i+1})$$$ is essential. Suppose otherwise. Given that $$$a_i$$$ is increasing, there exists $$$k$$$ satisfying $$$h(a_k)=h(a_{k+1})$$$. Choose such a minimum $$$k$$$ (i.e. $$$h(a_i)<h(a_{i+1})$$$ for all $$$i<k$$$); adapting the proof above we see that $$$h(b_k)=h(a_k)=h(a_{k+1})$$$, which means that the $$$j$$$-th digit of $$$b_k$$$ and $$$a_{k+1}$$$ in their binary expansion matches for all $$$j\ge h(b_k)$$$. It follows that $$$b_{k+1}=a_{k+1} \otimes b_k$$$ must have the $$$j$$$-th digit as 0 for all $$$j\ge h(b_k)$$$, which follows that $$$h(b_{k+1})<h(b_k)$$$ and therefore $$$b_{k+1}<b_k$$$, contradiction.

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

    Why do in the final answer , we are subtracting the value obtained by -1, like I understand how we got 24 in the editorial solution but why do we subtract the value obtained by multiplication by 1 to get 23 ? Plz help.

    • »
      »
      »
      7 weeks ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      The empty sequence is counted once in the analysis, but it's not allowed. Hence the $$$-1$$$.

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

1329B - Dreamoon Likes Sequences Can someone please explain how does knowing v=0,1,2 ... solve the problem for all lengths n>=1 ?? Thanks.

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

dreamoon_love_AA May I know when will the construction of Div. 1 C and E be over? I am eagerly waiting for the editorial of Div. 1 C or may I get some hint for Div. 1 C?

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

    I hope I can complete it this weekend. I cannot make sure what time I can complete it just like I don't know what time I can become LGM again.

    P.S. In fact, I'm writting it now. But my writing speed is very slow.

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

    Now Div.1 C is complete.

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

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

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

My code for div 2B was O(n^2). It gave TLE on test case 3. But according to the constraints it should have worked. Please help me out if I am missing something. https://codeforces.com/contest/1330/submission/75401692

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

    Why do you think it can work with the constraints? $$$O(n^2)$$$ shouldn't work.

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

      n=200000 n^2=4*10^10 t=10^4 In 1 second we can do 10^8 operations Constraint is 2 seconds. Hence we can do 10^16 operations. t*n^2=4*10^14. So I thought it should work. I think I am wrong somewhere. Please correct me.

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

        You can do 2*10^8 operations in 2s not 10^16

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

          Oh thanks! I get it now. What a stupid mistake.

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

Anyone explain properly in simple language Div 2C.

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

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

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

In div1.c bottom to top approach, while constructing the solution, it is said if the current value at the iterated node is not in final tree call f(). But how to prove its possible to do the operation at that node without the leaf going below height g?

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

    I had written it. Please see the tutorial carefully.

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

      I didn't understand this part, "Then We iterate node from smallest index to the larger one. We always can determine the weight value of the iterated node, because the weight value only can be the maximum value in its subtree". Didn't we already determine value for each node while finding minimum sum?

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

        When we have chosen the weight values set $$$S$$$ remaining in the final tree, according to the trait of max heap, the value of node $$$1$$$ must be the largest one from $$$S$$$. And the value of node $$$2$$$ must be the maximum value in the subtree of node 2 who belongs to $$$S$$$ except the final value of node $$$1$$$. And so on, we can make sure all values in the final tree will be the same with the lower bound calculated by the algorithm.

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

The more I hear/read about poeple's solutions to Div1C, the more I appreciate my strikingly simple solution. All authors solutions seem to me overly complicated. I call positions from $$$1$$$ to $$$2^g-1$$$ free initially (positions that are at least $$$2^g$$$ are not considered free). I sort all elements in this heap and consider them in increasing order. Whenever I consider some value, I traverse path from its initial node to root. If there is no free element on it, I skip it. If there is one, I check whether both of its sons are not free, and if it the case then I put it there and call it taken instead of free (if exactly one of sons is free then I skip it). And that's it, very short code here: https://ideone.com/1XiSlu

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

    Wow! I appreciate your solution a lot!

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

    I tried to understand the correctness of your simple solution, but couldn't.

    Could you please kindly provide some proof of it? or at least any hint?

    • »
      »
      »
      2 months ago, # ^ |
      Rev. 3   Vote: I like it +16 Vote: I do not like it

      Lemma: A height-$$$g$$$ heap is a valid final heap if and only if for every position $$$r$$$, the element $$$a_r$$$ on it is originally in the subheap rooted at $$$r$$$. Proof: =>: observe that an element can only move to the position of its ancestor <=: observe that if you fix the elements left in the final heap, the shape of the final heap is unique. (largest element should be the root, and right and left subheap both have unique shape by induction hypothesis)

      We will prove by induction that

      1. If we place an element, it is in the correct position.

      2. If we skip an element, it is not in the optimal solution.

      Proof for 1. Suppose we can $$$a_r$$$ on $$$r$$$ but in the optimal solution there's $$$b_r$$$ on $$$r$$$. If $$$b_r>a_r$$$, we can replace $$$b_r$$$ with $$$a_r$$$ in the optimal solution. This is still valid by our lemma, and has a better value (contradiction). If $$$b_r<a_r$$$, that means we skip $$$b_r$$$ before. (contradiction)

      Proof for 2. Suppose we skip $$$a_r$$$ but it's actually on $$$r$$$ in the optimal solution. Now consider all elements under $$$r$$$ in the optimal solution. They are $$$<a_r$$$ so we should have placed them in the correct position. That means all nodes under $$$r$$$ are full at this point so we will not skip $$$a_r$$$ by our rule. (contradiction)

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

Great explanation for Div-B #C.

Can you please provide some more general tips on tackling constructive algorithm problems?

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

div2D min(2(v+1)−1,d)−2v+2.why added 2 here.anyone help please

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

    $$$\min(2(v+1)−1,d)−2v+2$$$ mean the number of different condition that you can choose at most one number from $$$2v$$$ to $$$\min(2(v+1)−1,d)$$$. Let $$$x = 2v$$$, $$$y = \min(2(v+1)−1,d)$$$. Firstly, there are $$$y-x+1$$$ numbers from $$$x$$$ to $$$y$$$. So if you choose exactly one number, there are $$$y-x+1$$$ choices. Secondly, you must add an additional one for the case don't choose any number from the range. That's why.

    UPD: fixed the bug in the fomula

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

      Thanks for explaining this point, but I think that y = min(2(v+1)-1,d) without -2v + 2.

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

      Why do in the final answer , we are subtracting the value obtained by -1, like I understand how we got 24 in the editorial solution but why do we subtract the value obtained by multiplication by 1 to get 23 ? Plz help.

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

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

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

Div2D For example, when d = 6, there are 2 choices for v=0, 3 choices for v=1, 4 choices for v=2.

Please someone give me all the choices for every v. Because I get it like that for v = 0 --> 1 choice : 1(1) for v = 1 --> 2 choices : 2,3(10,11) for v = 2 --> 3 choices : 4,5,6(100,101,110)

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

I was wondering if anyone could let me know what the issue with this code for problem C Div 2 is (and possibly a test case to replicate it). Running on C — Dreamoon Likes Coloring gets the wrong answer on test case 6, but the input is too long to replicate and copy. I have also tried similar types of values to this test case, but they all succeed. I do know that my answer outputs "-1" when it should be possible. I appreciate it a lot! (First time posting.)

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;

public class C {
    public static void main(String[] args) throws IOException {
        Scanner std = new Scanner(System.in);
        int n = std.nextInt();
        int m = std.nextInt();
        if (m==1) {
            if (n == m) {
                System.out.println(1);
            } else {
                System.out.println(-1);
            }
            return;
        }
        BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
        int tmpN = n;
        int[] arr = new int[m];
        for (int i = 0; i < m; i++) {
            arr[i] = std.nextInt();
        }
        int[] postSum = new int[m];
        postSum[m-1] = arr[m-1];
        for (int i = m-2; i>=0; i--) {
            postSum[i] = arr[i] + postSum[i+1];
        }

        int lastBlockStart = n - arr[0] + 1;
        int lastBlockEnd = n;
        log.write(lastBlockStart + " ");
        //inclusive...
        for (int i = 1; i < m - 1; i++) {
            int greedyStart = (lastBlockEnd - 1) - arr[i] + 1; //place at this index if possible
            int greedyStart2 = postSum[i + 1] + 1; //must place at this index in worse case...
            int currBlockStart = Math.min(greedyStart, greedyStart2);
            int currBlockEnd = currBlockStart + arr[i] - 1; //ends here...

            if (currBlockEnd < lastBlockEnd && currBlockEnd + 1 >= lastBlockStart) {
                lastBlockEnd = currBlockEnd;
                lastBlockStart = currBlockStart;
                log.write(currBlockStart + " ");
            } else {
                System.out.println(-1);
                return;
            }

        }
        int currBlockStart = 1; //starts here
        int currBlockEnd = currBlockStart + arr[m - 1] - 1; //ends here

        if (currBlockEnd < lastBlockEnd && currBlockEnd + 1 >= lastBlockStart) {
            log.write("1");
        } else {
            System.out.println(-1);
            return;
        }
        log.flush();

    }
}
»
2 months ago, # |
  Vote: I like it +10 Vote: I do not like it

Wow dreamoon_love_AA, This editorial includes, How to approach the problems, Not just solution. Kudos to You, Thank You!.

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

Can anyone provide proof of that is stated in Div2(Problem D) editorial about h(ai) must less than h(ai+1).

  • »
    »
    7 weeks ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    I had provided the link of the proof written by a good person in the tutorial. Please see the tutorial carefully.

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

Can anyone please tell me what is wrong with this code for div2 B https://codeforces.com/contest/1330/submission/75668322

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

After a long time i learnt something interesting.The problemset was really nice...Thanks Dreamoon_love_AA. Please arrange such contest as many as you can.

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

I have written this code for 1330A - Dreamoon and Ranking Collection:

Your code here...
#include<iostream>
#include<set>
#include<iterator>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--) {
		int n, x, missing, ans;
		cin>>n>>x;
		set<int> s;
		for(int i=0; i<n; i++) {
			int x;	cin>>x;
			s.insert(x);
		}
		set<int> :: iterator itr;
		int index = 0;
		for(itr=s.begin(); itr!=s.end(); itr++) {
			index++;
			missing = *itr - index;
			if(missing > x)	break;
			ans = *itr;
		}
		while(missing < x) {
		    missing++;
		    ans++;
		}
		cout<<ans<<"\n";
	}
	return 0;
}

Gives wrong answer on one test case of sample test case two. Can anyone pls help about how this could be wrong.

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

for Div1-C i have an idea to solve it. for each node find the number of node we have to delete in its subtree from left side, since the number of node need to delete from right and left side will be equal to get perfect binary tree after all operation, lets denote it by num[i]. and maintain left[i] and right[i] to store number of nodes we have deleted in left & right subtree of node i till now respectively. now if i call required function from pos 1 then there are 4 possiblity.

if(v[l]>v[r] && left[i]<num[i]) then it will goto left child

if(v[l]>v[r] && left[i]==num[i]) then it has to go to right child in this case i will update the pos from where i am calling function to get perfect binary tree finally. similarly other two condition where v[r]>v[l].

once it get to leaf node, i will break it and call required function from pos that we get from above function. As i am calling required function from top most possible node every time then i should get minimum possible total sum and and i will also get binary tree finally. But i am getting WA and i couldn't find such test case. Here is my submission. Can anyone tell me what is wrong in my solution

Update: thanks dreamoon_love_AA i forgot to update left[i] & right[i] for all ancestor of node from where i was calling required function. If some one want to look, here it is

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

https://codeforces.com/contest/1330/submission/75732591

i Dont't know that is wrong logic in my code can some one help me with div2 B problem will be much appreciated thanks

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

I do not understand the editorial for Div2C. Can someone explain it better?

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

    There are many tutorials you can search from google or in Codeforces comments(For example, here). Please see them first. I'm busy on weekdays. I will add it in the weekend.

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

Somebody please enlighten me with the Problem C insight that is slightly more intuitive!

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

Still couldn't understand div2C after reading all the explanations...

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

@dreamoon_love_AA Can you please tell where I am getting wrong in 1330B - Dreamoon Likes Permutations ?

My approach is I get the prefix sum = i(i+1)/2 and the other part( total sum — prefix )also matches one such sum, I am just checking whether any no.is repeated or not as 4+3+2+1 = 4+3+3. I am checking by the logic that suppose the no.s are 4,2,3,1 then the count of them should be max of them, i.e 4. In case of 4,3,3 the count will be 3. Here is my solution...

77888159

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

I've been trying div2 (B. Dreamoon Likes Permutations) but I'm not really sure why am I getting T.L.E could you please help me to find out ? 78243837 ... I find the maximum inside input array and check the two possible casses (len1 = ma, len2 = n- ma ... len1 = n-ma, len2 = ma)... I use a function that is intended to check if there is a permutation in the subarray bounded by the different lengths in O(n)

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

    You declare "vi calc(MAXN + 1);" too many time.

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

      Thanks a lot for giving me a hand with this

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

      I guess I need to study more about memory handling. Thanks

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

        I think the thing you must know is declaring a vector with length $$$N$$$ need time which is proportional to $$$N$$$.

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

My approach for div 2 B is little diff. i stored elements of the prefix and suffix in 2set and checked if size of set is equal to size of suffix . this will ensure that the elements are unique and also the max element should be equal to the length. though sometimes it gave segmentation fault when i tried to remove elements from the suffix set.

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

Why suffix sum is used in div 2/C. Can anyone help me with the approach ?

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

I'm trying to solve this problem B. Dreamoon Likes Permutations, and I'm getting WA on text 2 (wrong answer Participant output partition 3 3 at least two time on test 324).

Here's my code

anyone can help ?