Till I Collapse
Contest: Codeforces Round 406 (Div. 1)
Difficulty rating: 2400
Tags: data structures, divide and conquer

Rick and Morty want to find MR. PBH and they can't do it alone. So they need of Mr. Meeseeks. They Have generated n Mr. Meeseeks, standing in a line numbered from 1 to n. Each of them has his own color. i-th Mr. Meeseeks' color is ai.

Rick and Morty are gathering their army and they want to divide Mr. Meeseeks into some squads. They don't want their squads to be too colorful, so each squad should have Mr. Meeseeks of at most k different colors. Also each squad should be a continuous subarray of Mr. Meeseeks in the line. Meaning that for each 1 ≤ i ≤ e ≤ j ≤ n, if Mr. Meeseeks number i and Mr. Meeseeks number j are in the same squad then Mr. Meeseeks number e should be in that same squad.

Also, each squad needs its own presidio, and building a presidio needs money, so they want the total number of squads to be minimized.

Rick and Morty haven't finalized the exact value of k, so in order to choose it, for each k between 1 and n (inclusive) need to know the minimum number of presidios needed.

Input format:
The first line of input contains a single integer n (1 ≤ n ≤ 105) — number of Mr. Meeseeks.

The second line contains n integers a1, a2, ..., an separated by spaces (1 ≤ ai ≤ n) — colors of Mr. Meeseeks in order they standing in a line.

Output format:
In the first and only line of input print n integers separated by spaces. i-th integer should be the minimum number of presidios needed if the value of k is i.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
For the first sample testcase, some optimal ways of dividing army into squads for each k are:

1. [1], [3], [4], [3, 3]
2. [1], [3, 4, 3, 3]
3. [1, 3, 4, 3, 3]
4. [1, 3, 4, 3, 3]
5. [1, 3, 4, 3, 3]

For the second testcase, some optimal ways of dividing army into squads for each k are:

1. [1], [5], [7], [8], [1], [7], [6], [1]
2. [1, 5], [7, 8], [1, 7], [6, 1]
3. [1, 5, 7], [8], [1, 7, 6, 1]
4. [1, 5, 7, 8], [1, 7, 6, 1]
5. [1, 5, 7, 8, 1, 7, 6, 1]
6. [1, 5, 7, 8, 1, 7, 6, 1]
7. [1, 5, 7, 8, 1, 7, 6, 1]
8. [1, 5, 7, 8, 1, 7, 6, 1]

Example input:
5
1 3 4 3 3

Example output:
4 2 1 1 1

Example input:
8
1 5 7 8 1 7 6 1

Example output:
8 4 3 2 1 1 1 1

Editorial:
Your task is to find the minimum number of parts needed to partition this such that each part contains no more than k different numbers.
For a fixed k, we can greedily find the answer. First, fix a maximal partition with at most k different numbers in it, then a maximal after that and so on. If answer for k is ans(k), we can find this number in $${ \mathcal { O } } ( a n s ( k ) l g ( n ) )$$. The only thing we want is to find maximum r for a fixed l so that [l, r] has at most k distinct numbers.
This can be done with binary search + segment tree, but it's too slow. We can do this using a persistent segment tree in $${\mathcal {O}}(\lg(n))$$: for a fixed l, we define fl(i) to be 1 if l ≤ i and there's no such j that l ≤ j < i and ai = aj otherwise 0. So, if we have a segment tree on every fl, we can use this segment tree to find the first r for an arbitrary k (it's like finding k - th one in this array).
So we find the answer in $${ \mathcal { O } } ( a n s ( k ) l g ( n ) )$$, so the total complexity is $${\mathcal {O}}\left(\sum _{k=1}^{n}ans(k)\lg(n)\right)$$, and because $$ans(k) \leq \frac{n}{k}$$, $$\sum_{k=1}^{n} ans(k) = \mathcal{O}(n lg(n))$$, so:
Total time complexity: $${\mathcal {O}}(n\lg ^{2}(n))$$.