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

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

Hello every one

I've solved this problem but I have different questions about this problem:

The link : Frequent Values

The questions:

1 — How can I solve this problem if the values not sorted?

2 — How can I solve this problem online?

3 — How can I solve this problem if the values not sorted and online?

Edit: The link for the first question is here: values not sorted

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

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

First is Mo algorithm ...

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

how did you solve this problem??using segment trees??

  • »
    »
    11 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    It's a RMQ problem, you can build the tree of the frequency of every value, so every query can be done by O(log n) , but you have to handle the corner cases when a[i] != a[j] so the answer is:

    `ans = max(start[a[i]] + cnt[a[i]] - i, j-start[a[j]], QUERY(a[i]+1, a[j]-1))`
    

    So you have to keep track of the counter and the first appearance of every value.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +25 Проголосовать: не нравится

For the third problem (online + arbitrary values):

  1. Let's find the most frequent value as well as number of its occurrences.
  2. Let's suppose that we can get amount of occurrences of number X in segment [L, R] in . Can be done with map from value to list of positions + binary search. Or, what is better, compress all numbers in the array first and then use array instead of map. We will use that we have small numbers only in the future.
  3. Observation: if we know answer for segment [L, R] (say, it's value X), then the most frequent value for segment [L', R'] (where L' ≤ L ≤ R ≤ R') is either X or it lies in .
  4. That is, if we know answer for [L, R] we can find answer for [L', R'] in by combining previous points.

Next idea is sqrt decomposition. For a fixed L we can calculate answers for all possible Rs in O(N) (just keeping current amount of occurrences for each number in an array). Unfortunately, we cannot precalculate answers for all segments (it would be O(N2), to let's do it for K uniformly distributed Ls, it will be O(NK).

Then, we have some queries incoming. Suppose we have query [L, R]. Then we know that we can "shift" L for not more than elements and get a precalculated query. With the power of point 3 we now can answer query in .

The only thing left is to chose K wisely. We can let and have precalc and per query. Or we can minimize , where Q is amount of queries, and it's minimal when (just take a derivative). That way we have for precalculation and per query. When N ≈ Q we have for precalc and per query, which sums up to

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

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