Codeforces Round #406 (Div. 1) C. Till I Collapse(可持久化線段樹)

RJ28發表於2017-04-03

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

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

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]<<" ";
	}
} 


相關文章