majk's blog

By majk, history, 2 weeks ago, In English,
Tutorial is loading...
Code (by arsijo)
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code (by winger)
Tutorial is loading...
Code
 
 
 
 
  • Vote: I like it  
  • +154
  • Vote: I do not like it  

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

1091A Tutorial is not available

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

It was fast :D

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

I haven't seen such fast editorial! Cool contest, I enjoyed it!

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

Happy New Year!

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

Problem D: How do you prove that, there are no pair of indices (i, j) such that sum of [i, j] is n(n + 1) / 2 and [i, j] is not a permutation of 1, 2, ..., N ?

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

    Hi, when N = 5 there is a subsequence 1, 5, [4, 3, 2, 2, 1, 3], 4, 5 and there is subsequence of length 6 and sum 15, isn't it?

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

      The length of that subsequence should be equal to N

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

        Thanks, I completely overlooked that.

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

    If (i,j) is just one of n! permutations it is obvious permutation. Lets call (i,j) substring. It has k characters as suffix of some permutation and n-k characters as prefix of NEXT permutation. This k first characters of our substring: a) If they are not in decreasing order we then the next permutation will only differ on last (or less)k characters which implies the fact that n-k first characters of next permutation are same as in this permutation before. So, indeed if this k characters are not in decreasing order we obtain substring (i,j) which is permutation of numbers 1,2,3..n. b) Second option is that first k characters of our substring are the suffix of before permutation and they are all in decreasing order. Then, we cannot actually obtain substring starting with this k characters. Why? This is about the thing that last k characters will differ in before and next permutation by exactly one letter, which implies that their prefixes of length n-k will differ exactly by one letter too. I know how to prove it when k is the longest decreasing suffix. Then it is pretty clear. In other case(k is decreasing suffix but not longest) i think that something similar is true. The proof when k is the longest decreasing suffix comes from algorithm for generating all permutations in lexicographical order. Im sorry for my poor english. Let me know if something is unclear or if im wrong somewhere.

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

      can u please explain this natofp?

      for suppose 1,2,3,4,8,6,7 is a permutation and its next permutation will be 1,2,3,6,4,7,8. here if we consider the last four elements from first permutation and the first three elements from the next permutation it will form sum n(n+1)/2 but there it was said that we cannot form a sum if the suffix is in decreasing order

      Thanks in advance

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

    I think the following is pretty unreasonable to come up with in contest...

    We will show inductively that for any consecutive permutations σ, σ + 1,  if the sum of the first k elements are the same, the first k elements must be permutations of each other. Let σ = (a1, ..., an), σ + 1 = (b1, ..., bn).

    Let k be the length of the longest common prefix of σ, σ + 1,  and let m < n be maximal such that and for now assume k > 0. The key idea is that (ak + 1, ..., an), (bk + 1, ..., b2n) are consecutive permutations. Thus if k < m,  we have ak + 1 + ... + am = bk + 1 + ... + bm which by the inductive hypothesis implies that ak + 1, ..., am are a permutation of bk + 1, ..., bm,  contradicting the maximality of k.

    We now take care of k = 0. Clearly σ = (a1, a2 > a3 > ... > an) and σ + 1 = (a1 + 1, b2 < b3 < ... < bn). Then it is clear that the prefix sums of length  > 2 are greater for σ than σ + 1. For length 2,  equality only holds if the biggest number in (1, ..., n) is one more than the smallest, i.e. n = 2 which is checked by hand.

    With his lemma, we can show that if a contiguous interval of length n has sum n(n + 1) / 2,  it must be a permutation of 1, ..., n. Indeed, let this contiguous interval be lodged between two consecutive permutations (c1, ..., cn), (cn + 1, ..., c2n). Then which by the lemma implies that (cn + 1, cn + 2, ..., cn + k) is a permutation of (c1, ..., ck). Thus as permutations, as desired.

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

      There is few flaws in this proof.

      Thus if k < m,  we have a_(k + 1) + ... + a_m = b_(k + 1) + ... + b_m which by the inductive hypothesis implies that a_(k + 1), ..., a_m, are a permutation of b_(k + 1), ..., b_m.

      This induction implies that in inductive hypothesis, (a1 , ... ,  an) and (b1, ... ,  bn) don't need to form a permutation of consecutive numbers.

      We now take care of k = 0. Clearly σ = (a_1, a_2 > a_3 > ... > a_n) and σ + 1 = (a_1 + 1, b_2 < b_3 < ... < b_n).

      Then in this statement, σ + 1  might not begin with a1 + 1. It can be some other numbers in (a2,  a3 , ... , an) which is greater than a1.

      Besides, I don't think it is clear that the prefix sums of length  > 2 are greater for σ than σ + 1. Maybe it needs some further proof. Sorry for my poor intelligence.

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

        I'm not sure that I understand what you mean by the first point. What I'm using is that if two consecutive permutations have a common prefix of positive length, the remaining parts of the permutations are consecutive permutations of each other. For example 21354 -> 21435.

        I didn't explain the k = 0 case in much detail. The fact that σ + 1 must start with a1 + 1 comes from the fact that σ, σ + 1 are consecutive permutations and we need the first element in the permutation to swap out.

        For your last point, just see that the relative ordering of the ai and bi ensures the inequality... the details are a bit messy so I'll spare you, but I think it's clear (try 35421 vs 41235, for example to see what happens).

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

      Brilliant ideas!!

      One question here, why you say contradicting the maximality of k? By the hypothesis of the induction, they are permutations of each other, however, this does not imply that ak + 1 = bk + 1 and so doesn't contradict.

      Additionally, I think m doesn't have to be the maximal, it can be any value greater than longest common prefix.

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

Can someone explain how to check if the answer is on the left or the right?

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

    UPD: I'm explaining the solution of Problem E...

    In fact, the Theorem we used can be changed to:

    For each

    . So we only need to consider these two situations: point n+1 is in the set or not.

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

When you didn't realize the link in E was useful, so you had to come up with your own graph realization algorithm :(

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

    Problem E is really not so friendly...

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

OMG! SO FAST!

Wish every coder : Happy New Year!

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

that was so fast , thank you for a great contest

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

That was fast insert Pikachu(o_o) meme

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

Here's a tutorial for similar optimizations as in problem H.

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

Alternate solution for B: Generate all points (xi + aj, yi + bj). Print the one that occurs n times.

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

    Can you please elaborate it ??? I find it difficult to understand

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

      since it proves that T always exists, so the equality (xi + aj, yi + bj) = T is always able to be satisfied?
      so we compute all possible (xi + aj, yi + bj), since (xi + aj, yi + bj) = T is always able to be satisfied, and 1 ≤ i ≤ n,so this equality can be satisfied by (at least) n pairs of (i, j),so this solution is correct?

      nah tbh i'm not sure as well :/

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

        Thank You ! :)

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

          But the time complexity is O(N^2log(N^2)), wouldn't that be a little too slow?

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

      T will be one of them (x_i + a_0 , y_i + b_0) (1<=i<=n)

      so , iterate over them and check for n points code

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

    Did the same but don't know why is it WA can you help please? 47738111

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

Wikipedia is useful for E but I didn't found it:(

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

I never saw this coming so fast :p

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

One can find complement of D (n·n! - an) in OEIS: A038156

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

    god OEIS :/

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

    I checked the sequence itself and also some intermediate results, but unfortunately not the complement.

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

      If you had found the complement sequence, would you have put a different problem D?

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

        Probably yes. And this problem would still be useful for a contest without Internet access, e.g. local ACM contest.

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

    Damn i tried finding a$_n$ directly and couldn't find it. had to come up with a recurrence myself by just seeing patterns.

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

      Me too... looks like I need to work on OEIS skills :/

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

        Maybe you should work on your problem solving skills?

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

          Haha, that goes without saying.

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

    Why do we subtract from n.n! ?

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

How didn't my Recursive Backtracking get TLE on problem B?
link here: http://codeforces.com/contest/1091/submission/47737516
I can't prove its time complexity but it seems to be like due to its elapsed time?

Well seems it works much slower than
but it took only 0.5s ,how?

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

    The Verdict is Accepted, isn't it?

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

      so i'm confused :/
      i thought it should get a TLE but it didn't
      this makes me puzzled

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

    Your solution's time complexity is about . In test 47 and 49, it equals 5 × 108.

    Thanks to Codeforces' amazing servers, it can pass the system test.

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

Wait, where did it say that sqrt in problem G returns a sqrt randomly?

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

    You are choosing x randomly so in the n = pq case, the system doesn't know which of the four possible values of x yield a square of x2. The square root returned is random in the sense that it only depends on and not x.

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

I can't understand C. Can Anyone explain?

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

    it's easy to understand that, m = DivisorCountOf(n);

    Now, how to get f_1, f_2 , f_3, ......f_m ?

    What will be the series for some k , n (for all k , n%k == 0)

    1. k = 1, n = 6 , series a: 1 2 3 4 5 6
    2. k = 2, n = 6 , series a: 1 3 5
    3. k = 3, n = 6 , series a: 1 4
    4. k = 6 , n = 6 ,series a: 1
    5. series len = n/k

    6. a_1 = 1
    7. a_len = n-(k-1); /// cause , (a_len + k)%n = 1

    so , series sum : len*(a_1 + a_len)/2

    Here is the Code

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

When is the tutorial for A coming ? ( ͡° ͜ʖ ͡°)

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

Can anyone explain Problem C solution with example ?

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

    using some examples, u can see that for values of k which are factors of n, the ball reaches in a single round(it's obvious). but, for other values of k for a given n, u will get the same result as obtained by one of the factors of n.

    for ex n=6 k=1 gives [1 2 3 4 5 6 1], k=2[1 3 5 1], k=4[1 5 3 1], k=5[1 6 5 4 3 2 1]; 1=5 and 2 = 3;

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

Statement of problem B was very long and hard to understand hope to fix problems like that next time :)

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

Both My Solutions for B and C failed on main tests, but I can't seem to find my error.

Edit: I realized my Mistake with B

Problem B: https://codeforces.com/contest/1091/submission/47751047 Problem C: https://codeforces.com/contest/1091/submission/47762703

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

    I think it is because your condition in selecting minimum and maximum point

    if(a.x > max.x) { max = new Point(a.x, a.y); }

    maybe the a.x = max.x but a.y > max.y you don't handle this condition in your code

    the same thing in the min condition Java

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

      Thanks, I forgot to consider that. Do you know what could possibly be wrong with my solution for C?

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

        i think the problem in "find" function make sure that it is the right way to calculate fun number. here is how i calculated fun number

        // d -> divisor
        // n -> input number
                    
        ll x=n/d;
        ll fun=x*(x+1)/2;
        fun*=i;
        fun-=n;
        fun+=x;
        st.insert(fun);
        
»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone explain the recurrence in D?

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

    as far as i understood :

    Well i thought about this while i was trying to solve as well it is true each part have same contribution , because when we switch the first digit ( 1 3 4 2 --> 2 1 3 4 ) the last digit of our last permutation is the same as the first digit of our next permutation during the switch , so concatenation of (1 3 4 2 + 2 1 3 4) , gives no sub string that has all 1 2 3 4. That's why they have equal contributions as the last digit will come twice always

    I am not sure how they counted for a single part i had a different approach which is giving some answer right and some wrong..

    but the recurrence is there as all the n! length string have same contribution , all starting with 1 , contribute the same as all starting with 2 , 3 and so on

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

    d(n) = (d(n - 1) + (n - 1)! - 1) × n

    • »
      »
      »
      9 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      can you explain why the sum of from k to n-1 n!/K! works i understand that for the recurrence relation ship it's required n*n! to find all possible permutations but why i am subtracting it from n!/k! every time using different k thanks in advance?

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

SO FAST!

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

Can anyone please explain D?

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

Thanks for a fast editorial!

  • »
    »
    2 weeks ago, # ^ |
    Rev. 3   Vote: I like it -23 Vote: I do not like it

    Did I say something wrong? If anyone has any problem with anyone's comment why not just skip it? I think all the persons here are programmers with a developed mentality. Only a freak or mentally disordered person can do such kind of stupidity. Sorry, if I am saying something wrong.

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

walnutwaldo20 told me that on D, if you split the sequence evenly into n consecutive parts (each part has length n!) then:

  • each part contributes the same amount to the final answer
  • no valid subarray can overlap with 2 different parts

But we don't know why this is true. Does anyone know the proof?

edit: nvm ignore this

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

    Well i thought about this while i was trying to solve as well it is true each part have same contribution , because when we switch the first digit ( 1 3 4 2 --> 2 1 3 4 ) the last digit of our last permutation is the same as the first digit of our next permutation during the switch , so concatenation of (1 3 4 2 + 2 1 3 4) , gives no sub string that has all 1 2 3 4. That's why they have equal contributions as the last digit will come twice always

    Just explaining in case someone else also didn't understand this

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

For problem B, I simply created an array of tuples for all possible answers that we might get. To do so, I simply took the coordinates of the first pillar and created all its combinations with the clues. So, it gave me total n possible answers. Then I created a map of tuples of the coordinates of the clues. Now, while iterating over each possible answer in the array that we created initially, I checked if there exists a coordinate in our map such that we would get the same T for which we are currently checking in our possible answers array, If we have all the required coordinates, then we have have found our answer or else we continue checking for the next possible answer.

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

The constraints on problem E seem incredibly tight.

Many O(N * log^2(N)) didn't pass (which is unsurprising, given 500K elements and 2 seconds). I thought the limit was so tight especially so that such solutions wouldn't pass (since there is a linearithmic solution), but the editorial does mention a O(N * log^2(N)) solution, so it seems like it was intended to pass after all?.

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

3 10 10 50 WGL

In problem F how is the ans 220 for this ? I think it should be 240? Am i missing something? pls help

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

    Swimming for 120 minutes, gaining 40 stamina, then 50 minutes for grass getting another 10 stamina and 50 minutes for lava. 120 + 50 + 50 = 220min

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

Example of my pattern

I need help with why my solution is wrong for D So basicly my approach is dividing it into 2 parts , 1 is the obvious n! arrangements of 1 to n , then concatenations of 1 to 2nd and 2nd to 3rd and so on So what i found out was , when we check from 1st to 2nd we get exactly n-2 , from 2nd to 3rd we get n-3 this alternates all the way till the first digit is changed i.e ( 1 4 3 2 — > 2 1 3 4) so as each different digit startings ( 1 2 3 4 .... , 2 1 3 4 ... ) contribute the same amount so i found out the result for 1 2 3 4 and multiplied it by n for patterns starting with 1 , i did ((n-1)!)/2 * (n-2) + (((n-1)!/2) -1)* (n-3) , the -1 in 2nd case because last transition for digit change would have no substring that has all 1 to n Can anyone help me out

http://codeforces.com/contest/1091/submission/47759220

its giving correct answer for some cases and for some it is giving a wrong answer

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

    i also got the same logic but not understanding it is not applied on 10!;??have u got the probelms in this approach...??

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

Can I get a better explanation of D?

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

I have solved 'D' by finding patterns.

Divided into some points:

  1. Each legal sub array of length n must have numbers from 1 to n at most once.
  2. So, each permutation counts as one of the legal sub array.
  3. Now extra legal sub arrays come after concatenating permutations. Two adjacent permutations definitely have ELSA(Extra Legal Sub Array) from n-2 to 0. The number of ELSA varies according to the first place from the left side where two adjacent permutations don't have same digit.

(Ex-1: n=3, 1234 & 1243; 2nd place from right or 3rd from left. Ex-2 : n=3, 1243 & 1324; 3rd place from right or 2nd from left.)

  1. Here comes my pattern finding. For 2nd place(from right) ELSA is n-2, for 3rd place(from right) ELSA is n-3, for 4th place(from right) ELSA is n-4, goes on unless ELSA is 1 at the (n-1)-th place.

  2. Now I have to find how many adjacent pairs of permutation have X-th digit(from right) variant.

  3. Then, (Number of X-th digit variant) * (n-X);
  4. Here we found total ELSA after concatenation, now add the number of permutation.

Finding amount of each X-th digit variant: (By example, for n = 4): 1. 1-th variant or, number of permutation : 4 * 3 * 2 * 1 = 24 2. 2-th variant : 4 * 3 * (2-1) * 1 = 4 * 3 * 1 * 1 = 12 (ELSA = 2) 3. 3-th variant : 4 * (3-1) * 1 * 1 = 4 * 2 * 1 * 1 = 8 (ELSA = 1) 4. 4-th variant or n-variant never need because it's corresponding ELSA = 0. Now, 24 + (12*2) + (8*1) = 56.

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

Problem E can also be solved without using the property that the answer lies in a contiguous interval (yeah, guess who didn't realize this during the competition).

Following the Erdos-Gallai theorem, assume we want to check whether d could be the answer. Let p be the position d would be in the degree array. For every position i ≤ p, d would appear on the left hand side of the inequality, thus posing a lower bound on d; for every position i > p, d would appear on the right hand side, thus posing an upper bound.

The bounds at each position can be preprocessed using prefix sums (for right hand side we could do a binary search instead of value counts), and we can also perform another scan to obtain the "prefix lower bounds" and "suffix upper bounds" (i.e. intersection of prefix/suffix bounds). To check whether d could be the answer, just check the prefix and suffix bounds, and also whether the inequality satisfies at position p itself. Checking one value takes time (just have to do a binary search), so the whole algorithm is .

One caveat that can be ignored: we still need to take the min function on the right hand side into account: if i < d then we should ignore the bound. This means that when checking the answer, instead of using the "prefix" bound, we should use the bound of an interval, which is the maximum value in the interval. This is an static RMQ problem and can be solved in O(1) using sparse tables. However, it seems that the lower bounds are non-decreasing (bounds at higher indices are always tighter (larger)), so this is unnecessary.

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

    In the contest I solved it only realizing it had to be a contiguous interval between [ai, ai + 1] where the ai's are the integers in the input in sorted order.

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

    Well, I just want to mention that this solution can be hard to implement. During the contest I also came up with a similar idea, but it took me more than 1 hour to code.

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

      Yes, you're right. I had a bunch of off-by-one errors before I could get it to work.

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

      Indeed, it's quite a bother to implement. The version I got in the contest is actually O(n) after sorting: 47753124. The prec0 and prec1 arrays correspond to the "sum of mins" part in the formula which can be precomputed by two pointers, and the lower/upper bounds are accumulated once and for all in opposite directions.

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    How do you mean its O(logN) per value? For every value, you need to check all preffixes, not only one, isnt it O(n) per value? Looks like you are checking only prefix till new value but that does not make sense, becase that is not only prefix that can make inequality wrong.

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

      To check whether degree d could be an answer, we need to first find the position p of d in the sorted array of degrees. Then, we need to check whether all n inequalities hold. These can be categorized by their positions i into three types: i < p, i > p, and i = p. As I mentioned in the previous post, each type could be checked within time with a bit of preprocessing. It's a bit hard to explain so I won't go into details here, feel free to check out my submission: 47769662.

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

    can you please explain this part of the code from the editorial of problem E -

    int is_score(int value) {
        vector<int> C(N+1,0);
        for (int i = 0; i < N; ++i) ++C[A[i]];
        ++C[value];
    
        int less = 0;
        long long left = 0, right = 0;
        for (int k = 0, i = 0; k <= N; k++) {
            int val = (i == k && (i == N || A[i] < value)) ? value : A[i++];
            left += val;
            --C[val];
            right -= min(val, k);
            less += C[k];
            right += N-k-less;
            if (left > right + (long long)(k+1)*k) {
                return (i == k) ? TOO_BIG : TOO_SMALL;
            }
        }
        return OK;
    }
    
»
2 weeks ago, # |
  Vote: I like it -10 Vote: I do not like it

I am still a Beginner ^ _ ^ need help in A please

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

    iterate from 100 to 1 for finding valid number of flower code:: scanf("%d %d %d",&a,&b,&c); for(int i=105;i>=0;i--) { if(i<=a+1&&i<=b&&i+1<=c){ cout<<3*i;break; } }

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

Let me share my approach on D!

We can observe (I have yet to figure out how!) that valid subarrays are the ones that contain all the n distinct numbers (from 1 to n). So, for example, for n = 5: [4, 2, 3, 5, 1] is a valid one, but [4, 2, 2, 3, 1] is not!

So, now let's write down the concatenation of the permutation for n = 4 (hoping to find a useful pattern):

1 2 3 4 (p1)

1 2 4 3 (p2)

1 3 2 4

1 3 4 2

1 4 2 3

1 4 3 2

2 1 3 4

.... (pn!)

Obviously, for each p, all the subarrays that start at the beginning are valid (n! so far). (S0 = n!)

What about the the ones that are beginning at the middle of p? (for example, on p1, the [2, 3, 4, 1] subarray?)

As you can see, we need the part of the subarray that belongs to the next p (its prefix) must be equal to the prefix of the current p. So, for instance, on p1 if we take (again) the [2, 3, 4, 1] subarray, we observe that the part which overlaps with p2 (ie. the 1) occurs on p_1, too! So, by applying this reasoning, we can conlude the following:

If the subarray begins from 2nd number of a p (it ends on the 1st number of the next p), it is invalid only when the 1st number is changed (see the last p on the example) (ie when 1st number on permutation become 1 (the first number), 2, 3, 4, ..., n). The valid subarrays for this case are S1 = n! (total permutations) — n

If the subarray ends on the 2nd number of the next p, it is invalid only when the 2nd number changes (ie. when the prefix becomes _ -> [1, 1], [1, 1] -> [1, 2], ..., [1, 4], [2, 1]) and this happens n * (n — 1) times, so S2 = n! — n * (n — 1)

...

By continuing like this, we get Sk = n! - n·(n - 1)·...·(n - k + 1)

Hence, we need to calculate the following expression:

ans = S0 + S1 + ... + Sn = n! + (n! - n) + (n! - n·(n - 1)) + ... + (n! - n!) = n·n! - (n + n·(n - 1) + ...)

How can we calculate the second part?

We can use DP, where:

DP[n] = n

DP[n - 1] = DP[n]·(n - 1)

...

DP[k] = DP[k + 1]·k (until k = 2 is enough)

And we sum them! :)

I hope that this helps!

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

For problem E: I found the Erdős–Gallai theorem after googling with something like "valid degree sequence". After some investigation into the theorem, I figured out that a solution could be devised using it. However, there are so many details (at least for me) involved in the adaption of the theorem to this problem that I couldn't make it within the last one and a half hour during the contest :(. For those who may be interested in this approach, see my submission 47772075 for detail. The complexity is .

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

How does one show that the given solution for F is optimal?

»
2 weeks ago, # |
  Vote: I like it +26 Vote: I do not like it
We use the Erdos-Gallai theorem linked in the statement to determine whether a sequence is graphic.

I just thought the link was to explain the statement during the contest...

Cannot imagine there exist some solutions...

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

In the problem E,how to make Havel–Hakimi algorithm faster?

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

    calculate the prefix sum of di at first so you can calculate in O(1)

    enumerate k from n to 1,define cnti as the amount of dj(k ≤ j ≤ n) which equals to i,then you can calculate in O(1)

    (every time k is decreased,,and for dk,add it to sum and cntdk if dk ≤ k)

    then you can do it in O(n)

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

      Thank you.But what your said is Erdős–Gallai theorem.In the soluton above,majk mentioned that "Alternatively, we can perform a similar binary search using Havel-Hakimi algorithm, using a segment tree or a multiset and tracking whether the an + 1 was already processed or not to find out whether the answer is too small or too big. This yields O(nlog2n) algorithm." .But I don't know the detail about it.

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

        emmm... I don't know how to use a segment tree or a mulitset to make it faster,but I know how to use splay to do it.

        In Havel–Hakimi algorithm,you need to find the maximum ,delete it and decrease the biggest k numbers.

        take di as the key value,the first and second operation will be simple.

        for the third operator,splay the (k+1)th number to root(if the kth and (k+1)th are equal and on the same node,split the node),mark on the root's left son,delete the root and insert it back(after decreasing the biggest k numbers,the root might be less than its left son(assume we put the bigger node on the left),so we reinsert it to make it in the right place).

        note that the third operation is very complex.

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

          That's right ! Thanks a lot ! Happy new year!

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

Problem E is terrible for someone who didn't care about the link.

It's really surprising that part of the solution is offered in the description.

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

I passed H without bitset.

Maintain a list of vectors, vector[i] contains all j's which grundy[j] = i. When calculating grundy[x], find y from vector[0], vector[1], ... orderly. While finding in vector[i], if x-y is available, break, and if no break happens in vector[j], then grundy[x] = i and finish.

I can't prove the time complexity, but it performs well.

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

Alternatively, we can perform a similar binary search using Havel-Hakimi algorithm, using a segment tree or a multiset and tracking whether the an+1 was already processed or not to find out whether the answer is too small or too big. This yields O(nlog^2 n) algorithm. So,can a solution Accepted this problem with O(n log^2 n)?n=500000 and n log ^2n=1.8e8.Also multiset is very slow.... P.S. 5e5 is enough to make python programmer TLE in input.

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

Shouldn't the sum in the explanation of D should be instead of

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

I cannot figure out how to make the Havel-Hakimi algorithm faster. How can I sort the degree sequences after each update operation if I use segment tree?

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

What is the analytical proof of the solution to problem B? I mean strict mathematical, not observation

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

If we do not work out the factor list first, but use the following codeif n mod k = 0 then continue, can it pass the tests in problem C?

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

Can someone explain why maximum Grundy number on H is 100? I was hoping the editorial would explain it but it didn't.

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

I made some videos with more detailed explanations for some of the problems. I think my solution to F is a bit simpler than the editorial; the idea is to start with flying over everything and proceed greedily from there.

A: https://www.youtube.com/watch?v=Ue7gAIwhSoo

B: https://www.youtube.com/watch?v=sY1WOkz8K28

C: https://www.youtube.com/watch?v=CFP_lctIss4

F: https://www.youtube.com/watch?v=oj6bcK405lc

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

How to do binary search over a boolean array as said in editorial for E? Array is something like [ N, N, N, Y, Y, Y, N, N]. Plz suggest a general approach.

I know how to binary search range in sorted sequence but this sequence does not appear to be sorted.

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

    Once you determine that the answer is no, you can figure out if you are too high or too low based on where you fail. For example, for the degree sequence [4 3 2 1 ?], if we try setting ? as 3, and then do erdos gallai, the first 2 inequalities work (4<=0+4) & (4+3<=2 +(2+2+1)), but the third does not (4+3+3>6+(2+1)). Now, looking at just this inequality, the edge with length 3 increases the left side, but not the right. So increasing 3 will not work, and we know the answer is hence less than 3.

    If we try to make the answer 1, then the second inequality won't be satisfied, as 7>2+(2+1+1). If we decrease the answer further, the inequality still won't be satisfied, since it will be decreasing the right side of the inequality only (instead we will have 7>2+(2+1+0)). So the answer is greater than 1. Hence we only have to check 2 and that works.

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

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

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

For those don't know Lexicographically Next Permutation algorithm.

Algorithm link

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

Problem D: I read the tutorial and i interpret that we are trying to find the answer by subtracting all the false subarrays.

For each permutation seperately , n! should be added to answer

And for second part in the tutorial They are taking number of total subarrays as n.n! . I can't understand this Can anybody please further explain this? Thanks in advance:)

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

In question E , i think testcases are not so strong. Because in following testcase , solution of ecnerwala fails. That testcase is :

9

4 3 2 2 2 2 1 0 0

Actual answer should be [0,2,4,6] , but according to his solution , output is [0,2,4,6,8] . Link to his solution is https://codeforces.com/contest/1091/submission/47743506 .

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

    Oops, looks like you're right, I had an off-by-one that wasn't caught. It's fixed here 47800829 if anyone cares.

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

How to prove the greedy solution for F? In editorial there is an algorithm and reasons for it, but I still dont understand how to prove such solutions, when one changes previous steps of the strategy.

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

    First, you need to fly to pass lava. Then we greedily choose to gain as much stamina as we can. So we walk on grass and swim in water. There are two situations. 1. If we still run out of stamina at some place. Then we need to do some extra work to gain stamina. So we need to go backward and forward at some place before. 2. If through this method we can spare some stamina in the end, then in fact we can spend them before. The solution discuss this situation detailedly.

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

In problem C : Can someone explain What is the intuition or proof behind using only divisors as K values which give unique sums?

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

    yes i also dont understand the same thing. can anyone explain this ?

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

    for each k , series : 1 , 1+k , 1+2k , 1+3k , ...... , 1 + x*k ;

    where , (1 + x*k)%n == 1

    so , (x*k)%n == 0

    convince yourself , that (x*k) == n , not 2n,3n,4n.....

    that means k is a divisor of n

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

      why did you rejected 2n,3n,4n... ??

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

        Think the circle as 0,1,2,...n-1. You wanna back to 0 not 1.

        Now series : k,2k,3k,....,x*k

        you stop tour after finding first x such that (x*k)%n == 0 ;

        Now, you tell me how a number forms with it divisors? say 12 ::
        3 , 2*3 , 3*3 , 4*3 (which is 12 itself)

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

    The fun value only depends on gcd(k,n), so you only need to iterate over possible gcds, and all gcds divide n (that's part of the definition of gcd!)

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

In D, Why do we subtract from n*n!?

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

    There are n! permutations , from each of them we can get at most n segments (1 from it , n-1 from its_suffix + next's_prefix) with sum of segment == n*(n+1)/2

    For our last permutation we don't have

    ( suffix_i + prefix_(i+1) == n*(n+1)/2 )

    so why we counting n from the last one?

    Look , last one has bad suffixes of length 1,2,3...n-1 ; All of which are includes in subtraction process ...-

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

Hey , I'm a newbie and just beginning CP. In Problem C , why the formula ,N*(i-1)/2 + i and N*(N/i-1)/2 + N/i , Can someone please explain this formula ? And it's significance in the problem ? What is the need for this formula , why not basic AP sum where d=i ? How do people crack such formula's within 2:30 hrs ?

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

I found the mistake. I was running "O(n) Erdős–Gallai graphicness check" every time i was printing the answers. That was O(n2) :D

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

Can't understand why i need to consider only k that divides n. Can anyone explain that moment in other words than in editorial pls?

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

    The fun value only depends on gcd(k,n), so you only need to iterate over possible gcds, and all gcds divide n (that's part of the definition of gcd!)

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

Problem E:How can you prove that "on the integers of same parity, the answer always forms a continuous interval"

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

    I do not have a simple mathematical proof, but you can definitely see this fact from the statement of Erdos-Gallai.

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

    We assume that x is the smallest possible answer for an + 1, assuming that there is an edge u - v in the graph, then we can change this edge to u - k, k - v without changing the degree of other points, obviously we have found that the degree of k has increased by 2, and this can be done for all other edges, so the possible answer for x is continuous and has the same parity.

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

      You also have assumed that neither of u-k and v-k was already formed

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

In problem G:

"The primes are of form 4x + 3 for a simple reason – we can find the square root of x modulo p by simply calculating ."

Shouldn't it be ?

»
13 days ago, # |
Rev. 3   Vote: I like it -8 Vote: I do not like it

毒瘤啊

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

Problem H:How can you proof that the Grundy number is less than 100?