Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

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

Автор bihariforces, история, 5 недель назад, По-английски

Mode of an array is the most frequent element, mode frequency is frequency of most frequent element.

For example, $$$[1,3,5,5]$$$ has mode $$$5$$$ and it's frequency is $$$2$$$ so mode frequency of this array is $$$2$$$.

For an array of length $$$N$$$ we need to find sum of mode frequency of all subarrays.

Is there any way to do this better than $$$O(N^2)$$$?

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

»
5 недель назад, # |
  Проголосовать: нравится -6 Проголосовать: не нравится

see codechef last contest question 6

»
5 недель назад, # |
  Проголосовать: нравится -14 Проголосовать: не нравится

A very similar problem was asked in Codechef Starters 136. A slight difference was that the string provided was binary, so there were only two elements you needed to keep a track of, but the overall process of implementation will be the same.

  • »
    »
    5 недель назад, # ^ |
      Проголосовать: нравится +24 Проголосовать: не нравится

    Not slight difference, binary is simple with prefix sums, I don't see any way to extend this to even ternary, let alone general array.

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

Since there are at most $$$\sqrt N$$$ different frequencies, so first we collect them, then for each frequency $$$f$$$ scan array with two pointers counting start of $$$f$$$-segment and end, but I can't immediately see is clean implementation of this because there are many cases where segments start and end.

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

    What will we exactly check for each possible frequency

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

      Number of pairs $$$(l,r)$$$ where it's the mode frequency

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

        Then what will be the worst complexity?

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

          The issue I see with this approach is array like 5 5 5 5 has single frequency, but subarrays have 4 of them, if you find a way handle this (maybe scan for values with given freq instead of frequencies itself) it should be $$$O(n\sqrt n)$$$