「杂题乱刷」CF786C

题目链接(luogu)

题目链接(cf)

水 2400。

首先我们容易看出,答案具有单调性,然后无法使用数据结构进行优化,这时我们可以直接根号分治,发现总是有一段连续的区间数是相同的,因此我们直接根号分治套二分即可 AC。

参考代码:

点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
*/
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define forll(i,a,b,c) for(register long long i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(register long long i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
ll t;
ll n,sq,a[100010],b[100010],k,last;
ll f(ll x)
{
	ll ans=0,sum=0;
	last=1;
	forl(i,1,n)
	{
		b[a[i]]++;
		if(b[a[i]]==1)
			sum++;
		if(sum>x)
		{
			ans++,sum=1;
			forl(j,last,i-1)
				b[a[j]]=0;
			b[a[i]]=1,last=i;
		}
	}
	forl(i,last-1,n)
		b[a[i]]=0;
	ans+=bool(sum);
	return ans;
/*	ll Sum=0,ans=0;
	forl(i,1,n)
	{
		b[++k]=a[i];
		Sum+=++sum[a[i]]==1;
		if(Sum>=x)
		{
			Sum=0,ans++;
			while(k)
				sum[b[k]]=0,k--;
		//	sum[a[i]]=1;
		}
	}
	return ans;*/
}
void solve()
{
	cin>>n;
	sq=pow(sqrt(n),1.2);
	forl(i,1,n)
		cin>>a[i];
	forl(i,1,n)
	{
		if(i<=sq)
			cout<<f(i)<<' ';
		else
		{
			ll L=i,R=n;
			ll need=f(L);
			while(L<=R)
			{
				ll Mid=(L+R)>>1;
				if(f(Mid)<need)
					R=Mid-1;
				else
					L=Mid+1;
			}
			forl(j,i,L-1)
				cout<<need<<' ';
			i=L-1;
		}
	}
}
int main()
{
	IOS;
	t=1;
//	cin>>t;
	while(t--)
		solve();
    /******************/
	/*while(L<q[i].l) */
	/*    del(a[L++]);*/
	/*while(L>q[i].l) */
	/*    add(a[--L]);*/
	/*while(R<q[i].r) */
	/*	  add(a[++R]);*/
	/*while(R>q[i].r) */
	/*    del(a[R--]);*/
    /******************/
	QwQ;
}
posted @ 2024-04-12 18:43  wangmarui  阅读(3)  评论(0编辑  收藏  举报