Edvard's blog

By Edvard, history, 9 years ago, translation, In English

Hello, Codeforces!

Educational Codeforces Round 7 will take place on 10 February 2016 at 18:00 MSK for the first and second divisions. You can read about educational rounds here and here.

<This paragraph was modified last time>

The round will be unrated for all users and it will be held with extented ACM ICPC rules. You will have two hours to solve six problems. After that you will have one day to hack any solution you want. You will have access to copy any solution and test it locally.

If you have ideas for some problems or maybe already prepared problems that you can't use in rounds or official competitions, you can write to me.

</This paragraph was modified last time>

Thanks a lot to Aleksa Plavsic allllekssssa who suggested the problems D, E and Ivan Popovich NVAL for the problem F. Also thanks to Mohammad Nematollahi Deemo who suggested the problem that was highly simplified and will be under the letter C.

The round was prepared by me, Edvard Davtyan. Thanks a lot to MikeMirzayanov we invented the problems A, B and C together. Also thanks to Maria Belova Delinur for checking the English statements, Aleksa Plavsic allllekssssa and Ivan Popovich for testing the problems.

We tried to make the problems easy but interesting. I think that the problems is mathematized a little. I hope you will enjoy the problems!

Good luck and have fun!

P.S.: The Codeforces Educational Rounds was recognized by Snarknews as the best project in competitive programming in 2015 (the picture below is the prize).

UPD1: The first phase of the contest is ended. You can hack any other solution.

UPD2: The editorial is ready.

UPD3: The round is over, the results is final.

  • Vote: I like it
  • +313
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
Rev. 3   Vote: I like it +15 Vote: I do not like it

Thanks a lot for the rounds. I like that each problem is meant to illustrate a specific algorithm. I just have one question: Is it possible that you will make educational rounds rated soon? especially that the problems in the last couple rounds are new and not from some previous contests?

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

    Some of the problems were not new.

    Please don't downvote :(

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

      At first it wasn't new, but in the last couple rounds I see the announcement saying that the problems are new. (I think they are new because the announcement started to say the names of people who suggested the problems).

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

        If it's first time you see a problem it doesn't mean it's new or not standard

        and those people who are mentioned in the announcement are just mentioned because they suggested standard problems not original problems

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

          Sorry, but I suggested new and for me not so standard tasks :P

          Normally that doesn't mean someone didn't invent something similar before.

          • »
            »
            »
            »
            »
            »
            9 years ago, # ^ |
            Rev. 2   Vote: I like it -6 Vote: I do not like it

            Ok maybe you did, but it's supposed that people suggest standard/old problems not new as it's mentioned in the announement

            If you have ideas for some problems or maybe already prepared problems that you can't use in rounds or official competitions, you can write to me.

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

              I really don't think the quote you mentioned means that the problems were not new. It says if you prepared problems "that you can't use in rounds".

              Sorry for the questions if you don't like it, it represents my opinion only. I just asked because in MikeMirzayanov's first announcement he said that the series will be unrated (for now).

              Unrated (perhaps for now);

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

                it's not that I don't like it I didn't mean to offense, I just wanted to say that the idea of educational round is to present problems that are standard (not like what you said the problems were new), so it can't be rated because of that.

                and about "that you can't used in rounds" I'm not sure what reasons that a problem can't be used in rounds other than because it's already used somewhere or it's too standard

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

                  Well, I can not agree with you :P

                  I had several rounds in the last half year and I can not organize every 15 days one contest. Also I invented a lot of tasks soon(at least 20), so for me it is much easier option giving it now to Edvard which prepares everything and on that way help to CF ;)

                  I know you didn't talk about me, but I suppose here is many guys which will do the same thing!

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

Thanks Edvard for mentioning me!

I think the task for this round are more 'educational' than any other time :) Also I am sure that everyone will find something interesting and solvable.

About my tasks, I think they aren't standard and I would happy to see such task on some contest where I am participant... Several times they were on line to be used for my regular competition :)

Good luck and any feedback about tasks after round will be valuable :)

»
9 years ago, # |
  Vote: I like it +1 Vote: I do not like it

The Codeforces Educational Rounds was recognized by Snarknews as the best project in competitive programming in 2015 (the picture below is the prize).

Say whaaaat? O.o

Gratz!

»
9 years ago, # |
  Vote: I like it +5 Vote: I do not like it

The Educational rounds have been excellent in featuring algorithms that are otherwise uncommon. Also, they let contestants experiment with the contest environment without fear of rating deduction, so trying new problem solving sequences and the such. I hope that these rounds will continue to be excellent :D

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

Mathematical problems are the best. kudos to Edvard

I think Educational round is at its best. Don't make it rated as it helps us to do/learn fearless coding at a competitive level. Thanks to MikeMirzayanov for polygon n all. Keep it up guys!

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

clash with IIIT hyderabad, Codecraft contest tomorrow.

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

Thanks a lot for the round Edvard.

I think that the round will be interesting and cool!

»
9 years ago, # |
  Vote: I like it +51 Vote: I do not like it

Codeforces is an amazing community! Like none other. This place is supposed to be competitive, but instead, its such a supportive community. People are not just sharing their knowledge, they're doing so without any immediate tangible incentive in return! May God bless you MikeMirzayanov, may god bless all problem setters, editorialists, people who share their solutions in blogs, coordinators, and everybody involved in the process. Sometimes, people willingly encourage others who feel defeated and demotivated. I think the money the setters earn is nothing compared to what they give, and a lot many people share their approaches without any payment whatsoever. :) I feel humbled by you all, and I feel happy everytime I come here. I wish to become great programmers like you, and also great human beings like you. God bless codeforces!

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You will have two hours to solve six problems. After that you will have one day to hack any solution you want. You will have access to copy any solution and test it locally.

Does this also mean that you can hack a solution to a problem even though your submitted solution to that very problem didn't pass the pretests? Thanks!

»
9 years ago, # |
  Vote: I like it +5 Vote: I do not like it

F is really nice! Thanks!

I understand the ideas, but I don't know how to implement them in a beautiful way.

Looking forward to magical implementations.XD

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

    Indeed. I didn't know it was possible to calculate this sum effectively... Until I solved this problem today.

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

    f(n) = 1^k + 2^k + ... + n^k is a polynomial of degree (k + 1). So, using Lagrange's interpolation formula, it's possible to compute f(n) in O(klog(k)).

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

      Why is the sum a polynomial of degree (k + 1)?

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

        See the editorial. It's easy to prove by induction.

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
          Rev. 6   Vote: I like it 0 Vote: I do not like it

          Thank you, now it's clear the induction step. I can make this more formally, for those interested. Denote . We want to prove that degree of Pk(n) is k + 1.

          Base case of induction after k:

          By the induction we know that deg(Pk - 1(n)) = k. Taking the derivative of P(1)k(n) = k·Pk - 1(n). This implies that deg(P(1)k(n)) = k

          Therefore deg(Pk(n)) is exactly k + 1.

»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Oh, no... Math.. (((

»
9 years ago, # |
  Vote: I like it +8 Vote: I do not like it

E is really nice!

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

    Thanks for nice words. I tried to contribute cool tasks. At least I hope problems D and E were interesting for solving :) Both tasks are easy for implementation and I will try to invent something similar in future !

»
9 years ago, # |
  Vote: I like it +24 Vote: I do not like it

I want to sincerely thank the problemsetters. Especially the one who set D . I don't remember the last time I saw such a beautiful problem. How brilliant someone has to be to come up with such an elegant problem that can be written in problem statement in such a simple manner !! I am simply awed . Kudos!! :)

»
9 years ago, # |
  Vote: I like it +13 Vote: I do not like it

Idea for D : You want to make d1 = n - 1, d2 = n - 2, ..., dn - 1 = 1 so that the sum is 0. The rest is just experimenting to find the solution.

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

    I knew this idea and yet I couldn't solve :(

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

      One of the key ideas is that d_{n} multiplied by zero, so you can make of it anything you want.

      The pattern for solution is:

      Separate all in three groups: where d_i must be some even constant, where d_i must be some odd constant, and where i == n (you don't care about d_n at all).

      Solution the task for first group it is something like (a b c c b a), you can see that it has exact d_i's as you need (1, 3, 5...).

      To solve for second + third group put n in between of this sequence and append n after (e.g. a b c N c b a N), is has also exact d_i's as you need. (2, 4, 6...)

      (Example ans for n == 4) 1 3 3 1 2 4 2 4

»
9 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Educational Rounds are very helpful in learning new concepts and revising old concepts.

Thanks for Educational Rounds. Keep it unrated.

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to hack other people? Can i lock the submission now, if I didn't do it earlier?

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

How to solve C?

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

    one way is to split array in sqrt(n) blocks. for each block, you keep an set and do binary search on it

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

    You can do some pre-processing.
    For each position, store the position of the next number that is not equal to it.
    Just traverse the array from right to left
    if array[i + 1] != ar[i] , then nextUnequal[i] = i + 1
    else nextUnequal[i] = nextUnequal[i + 1]
    Now when you have a query(L,R,X) check A[L] if it's not equal to X, return L otherwise look at nextUnequal[L] if that's <= R, return that else return -1

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

      It takes intelligence to see the simplicity in something complex. I was so sure this is segment tree problem, and later thought of sqrt-decomposition solution lol.

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

    For each index of the array keep the index of the next element which is not equal to current.
    Then the answer for query can be obtained like this:
    1. if (a[l] != x) print l
    2. else if (nxt[l] > r) print -1
    3. else print nxt[l]

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

    For each position in array remember the last position of a number different that what you have at that position(denote it d[i]), then for each query check if tab[r] != x — r is the answer, if tab[r] == x and d[r] >= l — answer is d[r], else -1

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

    What i did was to find maximum number in the interval.

    Then i also found minimum number in interval.

    If both are the same and they are equal to the number we dont want it to be equal to , we output -1.

    Else we look which of min and max are different from it and output one of them.

    But there are better solutions.

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

    I did it a long way by having 2 segment tree, one which reports the minimum and other which reports maximum in the range [L, R]. If both of them are equal and equal to x then print -1 else find the position of one not equal to x. Since the numbers range is [1,10**6], store indexes of each number in a vector and then binary search on them to find position. Complexity: O((n+m)logn)

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

    can be solved using DP in O(N+M) time. The idea is to calculate the maximum length of subarray for each position such that the subarray contains the same number as the one in that position. You can do this in O(N) time

    Then for each query go to array[l] . If this is not equal to x, tada! else jump to the position denoted by array[l] and the length of the precalculated subarray for position l. If this is within range, return the position, else -1

»
9 years ago, # |
  Vote: I like it +5 Vote: I do not like it

How to solve E?

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

    Consider all children of 1, let them be v. We can calculate answers for each v separately, taking the maximum answer.

    Now, for each v, find all leaves that belong to v, and record their distance. Sort them in decreasing order, and the answer is max(d[i]+i) for 0-based distance array. The reason is that we can think in reverse way: the ants move from root to leaves, and we always want the furthest ant to move first.

    Btw, this question has appeared (exact problem) some year ago in a chinese contest.

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

      I am not doing chinese contests, but It looks as right decision to put this task on educationl round :)

»
9 years ago, # |
  Vote: I like it +12 Vote: I do not like it

How to solve F?

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hi- distance cantst got hold down.

»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

How to determine the index of min element in algorithm segment tree? Is it right? My code for problem C

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

    check 15945814 tree1 stores index of maximum element in a range and tree2 stores index of minimum.

  • »
    »
    9 years ago, # ^ |
    Rev. 3   Vote: I like it +4 Vote: I do not like it

    I solved C with partial sums. Say we have to answer a query (X, Y, C): if sum of numbers in tha range [X, Y] is divisible by (Y-X+1) and sum in range [X, Y] divided by (Y-X+1) equals C then we have no element in range [X, Y] which is different from C. If we have a different element, we can binary search for the position of that different element.

    Here is my solution

    LE: Nevermind, I missed a case

    • »
      »
      »
      9 years ago, # ^ |
      Rev. 2   Vote: I like it +12 Vote: I do not like it

      Hacked :P

      Look at this sample :

      5 1

      3 2 4 5 5

      1 3 3

      your answer is -1, that isnt correct ;)

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

        Yeah, while I was taking a shower I realised that my solution fails that test and that I shoould hack myself, but it seems like you already hacked my solution. :p

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

          I solved it just like you did and I hacked myself too :p

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Are there at all any test cases for which an answer might fail (after passing the pretests) for the problems A and B? It's been almost 5 hours now and I have tried everything but in the end it seems that the pretests cover up almost everything for both the problems!

PS: Don't confuse this with me asking for helps to hack other's solutions. All I merely am asking is a yes or no to the question. Thanks! :)

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

    There are many hacked solutions for A and B task, so possible some tricky testcases exist ;)

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Wow, I use to teach how to calculate interpolating polynomials, but in a straight-forward way...

Couldn't believe they could be used in competitive programming until now!

»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Problem C have really tight time limits. I wrote O(N) solution in Java and it exceeded the time limit on test 20. It only passed when I moved the solution to C++ with scanf/printf.

I used BufferedReader and StringTokenizer in my java solution, so it's not a problem with slow Scanner class.

  • »
    »
    9 years ago, # ^ |
    Rev. 3   Vote: I like it +3 Vote: I do not like it

    There was no time limits problem in your solution. Actually you have used System class to print the output which is quite slow. Also you have used println which is slower especially when it is used directly by System class(writing on console sending bytes, flushing the screen etc). To make it faster create an instance of PrintStream class and use that instance to write to the console. print("\n") uses '\n' as linefeed character and println() terminate the string with new line character depending on the platform. Best to use is '%n' with printf() in java.

    15949731 15949638 15949630

»
9 years ago, # |
  Vote: I like it +16 Vote: I do not like it

Can someones guess, what hack test could be in B? halyavin have already successfully done 116 hacks:D

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

I completely overkilled C and got TLE as a return gift.

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

    My approach-

    Build a segment tree in nlogn where each node has a vector of elements of its subtree in sorted order. 2 adjacent segments will have sorted elements and so merging can be done in O(n) leading to a total build complexity of nlogn. Queries will find the range in a segment which has 'x' and then return an index outside this range. Complexity of each search is therefore log^2(n).

    I realized after contest that all we need is minimum and maximum of a range to find any position which does not have x . Stupid me :/

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

Have the solution been rejudged already? With the full test cases and the successful hacks?

EDIT: Spoke too soon

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This is the first round that I can solve problem D, although I was not in the field.