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

本文介绍了一个关于瑞克与莫蒂如何利用Meeseeks进行任务分配的问题,通过采用主席树等数据结构优化算法,实现了一个nlogn^2复杂度的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值