When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

Um_nik's blog

By Um_nik, history, 6 years ago, translation, In English

I want to apologize once more for queue problems. It has also aggravated some tight ML/TL issues which probably would be not so big otherwise. I hope you enjoyed the problems nevertheless.

Tutorial is loading...

38799778

Tutorial is loading...

38799800 — logs
38799811 — case analisys

Tutorial is loading...

38799824

Tutorial is loading...

38799831

Tutorial is loading...

38799840

I hope that Petr is not mad at me for my joke.

Tutorial is loading...

38799850
38799853 — completely different solution with complexity O(6n / 2)

Tutorial is loading...

38799873

Tutorial is loading...

38799881

I hope that PrinceOfPersia is not mad at me for my joke.

Tutorial is loading...

38799887

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

| Write comment?
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it
 It is clear that we will run DFS exactly once for each connected component of original graph.

Can u explain this?

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

    Let's look at strongly connected components of new graph. Each connected component of old graph lies inside one strongly connected component of new graph, and you can't go from one such strongly connected component to another. So we will run DFS from each that strongly connected component once.

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

Hihi Quang Dien brought me here.

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

    Wow!! Do you mean DoctorRYA of VietNam???

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

In Div1 B, isn´t it easier just to transform one permutation into the other and count the number of steps it takes?

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

    That's another way to count parity

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

      But, isn't it possible to swap the same pair more than once?

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

        Yes, we can swap the same pair more than once. But notice that it must take an even number of swaps to preserve the order, hence the parity and the answer will not be changed.

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

          Ah, So, NicolasCassia should compare the parity of the number of steps with the parity of 3n or 7n+1.

          Thank you very much :)

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

            If from a given permutation the original can be reached with an even number of steps, why can not it also be with an odd amount?

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

              I understand this truth, but don't know how to explain it other than like following:

              After any number of distinct swaps, if I want to return to the previous case (before those swaps) I need to do the same number of swaps I have done before (and this can be understood by testing). So, the number you need to return to a previous case starting from this case is even.

              So, if a permutation P1 can be reached from a permutatuon P0 with n number of steps, and you did s number of additional steps (anywhere between P0 and P1) and still end in P1, then this s is even. So, n + s has the same parity as n. Thus, if n was even, then any number of steps between P0 and P1 will be even, and vice versa.

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

                Can you explain significance of 3n and 7n+1?? I am unable connect the dots here.

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

                  The setter want to obtain two number which are surely different in the parity.

                  Firstly, we should know the following two rules (or observations):

                  odd * even = even,

                  odd * odd = odd,

                  odd + 1 = even,

                  even + 1 = odd.

                  Then, let's discuss the two case of n:

                  If n is even, then 3n is even and 7n + 1 is odd.

                  If n is odd, then 3n is odd and 7n + 1 is even.

                  And this difference in parity is necessary to solve the problem.

                  Hope I understood your question and answered it.

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

                  Yeah, i got that. I am referring to the coded solution above. (ans^=1 and why "ans=1" implies Um_nik... "ans=0" implies Petr).

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

                  Oh, I understood you now, and really don't know what's happening in the code you referred to XD. Sorry for that.

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

      any prove?? for odd swaps will definitely give odd parity and even will even

»
6 years ago, # |
  Vote: I like it +7 Vote: I do not like it

38770498

I can't understand this solution. Can someone explain this? Thanks.

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

    Because it's a wrong solution, this is counter-test. True answer is "Um_nik", but this solution returns "Petr".

    The authors of the problem did not assume a probabilistic solution, so there are no tests against them.

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

      You are right. But this solution can get Accepted. Emmm... Your test case is randomly generated data ? Maybe this solution based in some randomly theory?

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

        Well, yes, this solution uses that tests are randomly generated and it is correct for big n. However for n = 1000 + eps it has not very good probability of being correct. But I had very few (4 to be precise) tests with small n and it happened to be correct on all 4.

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

        This test is specifically directed against a probabilistic solution.

        For example and fun, I can generated tests when this solution will be Accepted:

        srand(123456);
        cout << (rand() % 1 ? "Petr" : "Um_nik");
        

        But this will not be a complete set of tests for this problem.

        You can use this method of solution if you do not need to solve the problem completely (on some olympiads give a few points per each test).

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

D Getting TLE in D could anyone Help?

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

    for each good store vertices which produce it(in a vector).now run BFS for each good(reinitialzing the dist/vis array) and keep pushing back into a vector(answer vector) the dist(which is obviously minimum due to unweighted graph) from the vertices producing the current good.After that sort for each vertex its ans vector and sum up the first s elements and print it.

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

      I have used same logic u said but i am getting tle don't Know why

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

        maybe because you are using index of size n whereas it only needs to be of size k

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

 Look here: 38736740 And what I should do with that?

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

    Maybe use long double instead of double! Link

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

    Try to change from double to long double.

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

    What?!?!

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

      Funny staff with mingw + windows. In a local machine with gcc+linux does not show this artifact.

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

        It's not MinGW or Windows specifically, it's floating point arithmetic in general.

        Basically, the compiler is free to calculate one expression with more precision than needed by double (say, in 80-bit FPU registers) and the other one with 64 bits precision. These two results will be different when compared in 80 bits, hence non-zero output.

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

      This could be an explanation

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

can someone give python code for binary fft

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

For Div2B, during the contest I came up with this approach:

For any two positive integer numbers a and b:

ab = ba if (a = b), else

ab < ba if (a = 1), else

ab > ba if (a < b).

But got WA on pretest 5.

Today, I checked for the test I failed in and saw it was (2 4), then treated this test, but got WA on this test (2 3), then treated this and got AC.

Is really this last approach is correct for all a and b, or it just passed the tests?

BTW, it was easy to come up with this last approach because (2 4) and (2 3) are easy to think about.

38781062

.............

UPD: Sorry, I have just seen that what I wrote above had been discussed in the editorial. I apologize for my fault.

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

    I got AC with similar solution: brute forced everything where a && b is small, otherwise using your logic.

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

I did exactly what the tutorial says in div2C, and during the contest as well, but it gets TLE on test 11. Tried searching for similar solutions in python 2 but no one AC, can someone tell me what I got wrong?

38782761

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

    Submit it on PyPy interpreter

»
6 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Why are some problem names written in russian instead of english in english version of editorial ??

»
6 years ago, # |
  Vote: I like it +30 Vote: I do not like it

I would like to just note that problems B from Div2 and D from Div1 have a lot in common. Solution to Div2B basically tells you how optimal sum looks like in Div1D ;). I don't know if it is intentional, but if not then it is funny coincidence.

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

    Yeah, I noticed it too. But it wasn't intentional :) Ddiv1 was in my problem bank for long time and easier problems were made up just for this contest.

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

;

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

Why did Div1B set a lower bound on n?

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

    For joke to work. It is usual condition for such problems.

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

This is my solution for Div2 E 38777827

I am counting the number of swaps needed to obtain the permutation in O(nlogn)

how to calculate the number of cycles in permutation in O(n).

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

    Just use a regular array instead of map, the numbers in the permutation <= 1e6.

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

May someone please tell, why did my short solution got TLE to the problem Div1-B? Submission link. Is it because of distance function?

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

    Since set does not have a random access iterator distance function takes O (n). Thats what makes it slow. For more see this

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

Can someone explain how i get AC by usingthis solution on problem Div2-D

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

For div1D, 3^i always looks like 100000....000 (with i 0s) in base 3. So, why don't we just convert n (or n/2, or n/4) to base 3 (In O(LlogL)) and compare it with 3^i (in base 3)?

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

38793607 Getting TLE in problem D from Div. 2. Not sure what I'm doing wrong here, could anyone help?

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

    In the bfs Don't push a node unless their cost will be updated and update the cost

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

      Thanks! I made this change and got AC. But I don't understand one thing:

      Earlier I was pushing the nodes irrespective of whether the cost would be updated; I was performing this check at the time of popping and stopping the BFS from a popped node if the cost is not to be updated. So basically in the BFS in my earlier submission, all the nodes were being operated on 2 times instead of 1 time (first time when it's pushed onto queue and second time when it's popped and I check for cost update). So this would be an optimization, right? The time complexity is the same? Please correct me if I'm wrong. Thanks again!

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

        Actually a node will be pushed as many times as it's degree here ! Moreover, you might run into a cycle which will take you into an infinite loop !

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

How to solve Div2 E with mergesort ?

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

    Merge sort here is used to calculate the number of inversions of a permutation to find the permutation's parity.

    Suppose your permutation is 4 3 2 1.

    Divide it into two parts. 4 3 and 2 1.

    Now, what are the number of inversions created due to 2? (number of pairs where a[i] > a[j] and i < j and a[j] = 2)

    It's the number of elements larger than 2 in 4 3.

    You can easily find that in the merge process of merge sort. When you are inserting 2 during a merge, the number elements left in the other half of the array is the number of elements larger than 2, isn't it?

    So, for 1, when you have divided original array into 2 parts, you are adding 2 to number of inversions.

    When you further divide 2 1 into 2 halves i.e. 2 and 1, you are adding another 1 to number of inversions.

    This is just a basic gist, you can find details by googling about finding number of inversions using merge sort.

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

I had used the logarithmic approach as discussed in the editorial but I am getting the wrong answer for x=y (where x and y are very big) but it is working fine on my local system. I am not able to figure out the exact reason why it is behaving differently on CF judge and my local system. Can someone please help me out to understand it?

Thanks in advance.

my solution

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

Links to authors submissions are added.

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

    Not available. Looks like something with the rights

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

    If I remember correctly, you should submit by hitting an airplane submit button next to a problem in the problemset list:

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

      Looks like the important part is "the problemset list". If I submit it in the contest, it is not visible because I'm manager of the contest. Anyway, thanks.

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

    Well, the authors submissions links are not working properly. Please fix the problem, thanks. [UPD] I'm sorry for the unnecessary post too. I'll think before I post:)

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

    [UPDATE] — Its working now. Sorry for the unnecessary post.

    Your solution link is not working.

    It keeps on saying,"You are not allowed to view the requested content"?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it -48 Vote: I do not like it

    Should be fixed now.

    Last two comments are not helping.

»
6 years ago, # |
  Vote: I like it +10 Vote: I do not like it

For Div.1 E,can somebody elaborate the fenwick tree on euler tour tree or introduce some tutorial on it? How does it calculate the sum of on path to root in O(logn) time? Thanks a lot!

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

    keep l[x] = starting time of x in euler tour, r[x] = ending time

    If you want to set value of node x = val, then with fenwick you modify l[x] to val, and r[x] to -val.

    Now say you have 2 nodes x, y and x is ancestor of y

    The range of query will be l[x], l[y]. Every node on path from x to y will appear Exactly once on this path. And nodes that aren't on path appear twice, so they cancel out by the way we set values in fenwik tree.

    So basically sum from root to x is query on (1, l[x])

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

    Also we can do that without fenwick tree at all. Just do dfs and keep sums to the root in the global array. In this case for each query we need to calculate sum sumx + sumy - sumlca(x, y) - sumparent(lca(x, y)). That will be solution.

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

can anyone provide links to more problems like Div1-B,Div2-E.. Thanks :)

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

Can any one help me with solving Three displays problem using dynamic programming(DP)?

The states for the DP I visualised are current display label(1..N) and number of displays(1..3). For each display label I minimise and choose the best cost of one display, the best cost of two displays and the best cost of three displays.

I'm getting Wrong Answer. The case I'm missing is where I don't get the best cost of three display at any display labels. So now I'm kind of feeling stuck on how to re use the sub-problem's solution here? Can anyone help me with formulating the DP ?

My submission link contains my DP idea.

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

    I solved it with dp. It might help if you look at my solution. I think it is easier to understand. http://codeforces.com/contest/987/submission/38737422

    Hope I helped. Have a nice day!

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

      Can you please tell the logic to solve using dp?or some material to study from these kind of problems...

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

        Kakashi_150 Say i is the index of first display board, j is the index of second display board and k is the index of third display board of our answer, where i<j<k, We keep track of the best cost of two display boards cost(i)+cost(j) at dp(j) if font_size(j)>font_size(i). Then for each k we try to pair it with dp(j)[which holds the best cost of two boards already] if font_size(k)>font_size(k).

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

      Thanks a lot for the idea.

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

      Now if we increase the constraint, say length of the array is 10^5.

      Can we arrive at an O(N) dp for the problem ? That was what I as trying to do in my initial submissions for this problem.

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

please explain O(6^(n/2)) approach for AND graph. thanks

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

I am getting TLE for Div2D in Java even though I used the same algo as the editorial. Someone suggested that using ArrayList in graphs in Java is inefficient. Does anyone has links to some articles that explains the alternative well?

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

    Read graph to ArrayList[], then copy its contents to int[][]. Use int[] for queue in bfs. It should be enough.

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

Dear Um_nik,

Please explain your second solution to F with complexity O(6^(n/2))

»
6 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it
  1. In problem C And Graph, why do we have to consider the second class of edge (x,2)-->(x|(1<<k),2)?
  1. In case of third class of edge (~x,2)-->(x,1) , the AND of (2^n-1-x)&x is zero , so we can keep this edge, but x&(x|(1<<k)) is non zero so why keep this edge,though this edge might be useful in leading to some y such that x&y=0, how does it actually work?
  1. Also, many submissions I saw used transition from x--> x^(1<<k) if kth bit is set, where x^(1<<k) is the subset of x if I'm not wrong(I read it in SOS DP). What is the difference between going from a. x--> x^1<<k if kth bit is set and b. x--> x|1<<k if kth bit is not set, can someone please explain me the whole thing correctly im too much confused. Thanks;)
  • »
    »
    6 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    Here is my editorial to problem F. I think it's easier to understand. Let me know if you have any doubts. :)

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

      First, Thanks for an excellent editorial, very nice I understood what I was missing , thankyou so much;)

      I used the same approach but I am getting runtime error- stack overflow, I use java.

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

        Hey, thanks a lot :) ... I don't know why it's getting RTE. Try removing the variable arr you'll reduce the amount of memory consumed that way and try again.

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

      Thanks! This is a very good explanation :)

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

      can someone explain me why am i getting WA in test case 14 (F. AND Graph Div 2)?

      i am using dsu to solve the problem

      http://codeforces.com/contest/987/submission/38892597

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

        Didn't understand your logic ... Please explain it clearly

        You can read the blog link I posted to understand my approach.

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

          a < b and if a&b = 0

          then i will change the vale of ane[b]=ane[a] it means b is connected with a so ane[b] != b

          if c < b and b&c = 0 so if c is not connected with a graph but b is connected with a so i will change ane[c]=ane[b]

          and in the end if(ane[c]==c) that means c is not connected with any prw graph so i will add 1 in my ans...

          Ex.

          4 3 1 3 4

          so we see than in first loop (for i=1 and j=3,4)

          1&3 != 0 (no cnange ) ane[4]=1 | ans=1

          in sec loop (for i=3 and j=4)

          now 3&4 == 0

          but ane[4]=1 so i will change ane[3]=ane[4]

          in sort i am just checking if a or b is prw. connected or not

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

            So, does each vertex have only one edge in your approach ?

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

              yes we can say that

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

                i think there can be 2 edges... assume some p and q and r S.T p<q<r among many possibilities we can say that p&q=0 and q&r=0 will be true and thus there are 2edges for q.

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

                it also means that the upper bound will only N/2 as each vertex has only one edge.

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

                *******DELETED******

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

      Damn dude how did you make such a hard problem into a piece of cake? :D. Nice solution and very easy to understand.

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

        Thanks a lot :)

        Please read my other posts to see if you like any of them too :)

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

      Hello ghoshsai5000 First of all your editorials are superb.Thanks a lot. I have a problem with question E :Petr and permutations, i have solved it differently and it is quite short code. Can you look at it once and tell me where i am wrong? Here is the link: (https://codeforces.com/contest/987/submission/39357325)

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

        Thank you for your compliments :)

        You are not counting the number of inversions correctly. Give me some time to give a counter example to show why that way of counting is wrong. :)

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

          Please tell me bro....

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

            Take 2341

            So you would do

            1. 3241
            2. 3214

            But you need three swaps !

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

              Can you tell me what we have to do after counting inversions and why? I have counted inversions using mergesort but it is showing wrong answer on test case 3.....link

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

                The idea is to count the number of swaps you need to arrive at the current permutation. Check the parity of the number of inversions.

                I know how to do it with segment trees and cycle decomposition, but I don't know how to do it with merge sort. I think there's some mistake in that part.

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

                The parity checking is correct. The parity of number of inversions should be equal to parity of number of operations.

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

What is the O(s(m + n)) solution to D ?

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

Inspite of the controversy surrounding this contest over the delayed queue length and the tight memory limits, I actually enjoyed this contest quite a lot ! I loved the problems and 3 of them inspired me to write blog posts.

Here is the repository of all my solutions to problems of this contest, along with explanations.

All my blog posts have some more exposition. Please read them and get back to me with any doubts or corrections. :)

I have written a blog post about B here.

I have written a blog post about E here.

I have written a blog post about F here.

(All the problems were quite beautiful :) )

Note — I have not written a blog post about C, but the repository of all my solutions also contains an O(n log n) solutions to C with segment trees. It's quite easy to understand if you know how to count the number of inversions using a segment tree. It's the same idea. First we sort elements by font size, and keep track of every elements cost and position. Then, one by one, we insert the elements in ascending order of font size into a minimum tree for cost. When we insert element x, all elements with font size smaller than x have already been inserted, so to know the smallest cost, we simply query the tree from (1, position(x) — 1).

We do the same thing for the right too, except now we insert the elements, one by one, from the largest font size. This time when we insert x, we have already put all elements with a font size greater than font size(x). So to get the minimum cost, we query the tree from (position(x) + 1, n).

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

Can someone explain update complexity in 1E(Prince's Problem).

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

(in div1B)

Why the generated tests have to be generated randomly!?

I mean hacks were disabled and we're guaranteed that input is random, even though the official solution does not rely on randomness of the input!

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

    or else the joke wouldn't be complete

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

Can I get a hint for solving Div2-D in O(s(m+n))?

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

    Hi~

    Also I don't know the optimum solution, but here is my thought. Hope it help

    When we run BFS for one specific town, we don't need to record all information of k goods. Once get s goods, we get the answer. This is to say, no need to use the sort stuff.

»
6 years ago, # |
  Vote: I like it +19 Vote: I do not like it

Author's solution for div1E contained a typo (which made it incorrect, of course). There were no tests against this bug in my testset, so even two other solutions didn't help to find it. Now author's solution should be fixed and some stronger tests added. I apologize for this mistake and want to thank Death_Scythe who found the bug.

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

In problem Div2 E, I don't quite get the part of parity of 3n or parity of 7n + 1. Can someone help me with this?

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

    When n is even, 3n is even and 7n+1 is odd. When n is odd, 3n is odd and 7n+1 is even. Thus, if you know the parity of n and parity of inversions in the array, you can directly arrive at the answer.

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

Can Div2 C be solved in O(nlog(n)) time. If yes, how?

»
6 years ago, # |
Rev. 3   Vote: I like it -10 Vote: I do not like it

Troll solution for a troll problem Div1B
Link to AC soln

int n, p = 0;
cin >> n;
for (int i = 1; i <= n; ++i) {
    scan(x);
    if (x == i)p++;
  }
  if (p >= 3 or n <= 5) cout << "Petr";
  else cout << "Um_nik";

(Expected number of fixed points are very different in both approaches)

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

Can we solve div2 C in O(N log N) ????