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.
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.
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.
分析:枚举k后,每次可以贪心的O(n)扫一遍得到答案,每次贪心的过程可以用二分加速,但是这样下来每趟复杂度是logn^2的,然后我们考虑主席树,每次贪心相当于在一个区间中找到包含k个不同的数的最大的一段前缀,我们可以用主席树求区间不同数的方法把它转化为求区间的第k大树,这样每趟下来复杂度是logn,总复杂度nlogn^2.
#include <bits/stdc++.h>
#define N 100005
#define INF 2147483647
using namespace std;
int n,cnt,a[N],rt[N],Pre[N],ans[N];
struct Node
{
int l,r,ls,rs,sum;
}tr[N*40];
void Build(int &node,int l,int r)
{
node = ++cnt;
tr[node].l = l;
tr[node].r = r;
if(l == r) return;
int mid = (l + r) >> 1;
Build(tr[node].ls,l,mid);
Build(tr[node].rs,mid+1,r);
}
void Insert(int pre,int &node,int pos,int x)
{
node = ++cnt;
tr[node] = tr[pre];
tr[node].sum += x;
if(tr[node].l == tr[node].r) return;
int mid = (tr[node].l + tr[node].r)>>1;
if(pos <= mid) Insert(tr[pre].ls,tr[node].ls,pos,x);
else Insert(tr[pre].rs,tr[node].rs,pos,x);
}
int Find(int node,int kth)
{
if(tr[node].l == tr[node].r) return tr[node].l;
if(kth <= tr[tr[node].ls].sum) return Find(tr[node].ls,kth);
else return Find(tr[node].rs,kth-tr[tr[node].ls].sum);
}
int get(int x,int k)
{
int kth = tr[rt[x]].sum-k;
if(kth <= 0) return 1;
return Find(rt[x],kth) + 1;
}
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
Build(rt[0],1,n);
for(int i = 1;i <= n;i++)
{
if(Pre[a[i]])
{
Insert(rt[i-1],rt[i],Pre[a[i]],-1);
Insert(rt[i],rt[i],i,1);
}
else Insert(rt[i-1],rt[i],i,1);
Pre[a[i]] = i;
}
for(int k = 1;k <= n;k++)
{
int r = n,l;
while(r)
{
ans[k]++;
l = get(r,k);
r = l-1;
}
cout<<ans[k]<<" ";
}
}