Блог пользователя I_love_Leyeli_Meredova

Автор I_love_Leyeli_Meredova, история, 3 месяца назад, По-английски

Hello everybody

Problem

Thank you!

  • UPD:

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
3 месяца назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Hi, I'm not at home right now, so I am unable to implement it. I think the intended solution is to use the fact that a[i] is small.

This inspires us to make each node have an array of size 40, to store the number of occurrences of each number. Then merging nodes is just counting the inversion number of the two arrays + inversion number of left array + inversion number of right array.

  • »
    »
    3 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I didn't quite get it.

    • »
      »
      »
      3 месяца назад, # ^ |
      Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

      Hmmm, you can try understanding a node in the segment tree contains the information of all the numbers in the segment. When we want to combine two nodes, lets call them L and R, inversion of current node = inversion of L + inversion of R + inversion of array of (L, R). In order to do that, we can maintain a frequency table in each of the node (size 40). Also notice that it is better that we count that with a prefix sum in 40 operations, instead of doing a 40 * 40 operation (looping through i, j).

      After merging the nodes, we still notice that our answer can't just simply sum all together, because the segment tree considers separated segments. But we can observe that the number of segments is around log N, so we can simply brute force them together to calculate the answer.

      Code link here

      the time complexity for my solution is $$$O(40 * N log N)$$$ which is passable.

»
3 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
Hint 1
Hint2
»
3 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I think the idea is to use merge sort tree. If you don't know about it, then it's a special kind of segment tree with array/map/st/multiset stored in each node representing the count of each element in the range

»
2 месяца назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I figured out how to solve the problem,

but
  • »
    »
    2 месяца назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Use arrays instead of vectors should be easier. But, if you really want to implement it that way, you can use vector(45, 0).

»
2 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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