Codeforce Educational Codeforces Round 55 (Rated for Div. 2) E - Increasing Frequency(尺取法)

考虑 C C C和非 C C C的贡献.
对于每一个数字 x ≠ C x\neq C x̸=C,其 [ l , r ] [l,r] [l,r]贡献是: [ l , r ] [l,r] [l,r]中所有的 C C C − 1 -1 1, x x x 1 1 1.用尺取法对每一个数字的贡献序列暴力求解。
容易知道如果合并所有的 1 1 1 − 1 -1 1,生成的贡献序列的总长度 ⩽ 2 n \leqslant 2n 2n,从而算法复杂度为 O ( n ) O(n) O(n).

#include <cstdio>
#include <cstring>
using namespace std;
int prec[500005];
int head[500005],nx[500005];
bool vis[500005];
int ck[500005],ct=0;
int b[500005],bt=0;
int mk(int x){
	if(!x)return 0;
	int t=mk(nx[x]);
	if(prec[nx[x]]-prec[x])b[++t]=(prec[nx[x]]-prec[x]);
	if(b[t]<=0)b[++t]=1;else b[t]++;
	return t;
}
int main(){
	int n,c,a;
	scanf("%d%d",&n,&c);
	vis[c]=1;
	for(int i=1;i<=n;i++){
		scanf("%d",&a);
		nx[i]=head[a];
		head[a]=i;
		if(!vis[a]){
			vis[a]=1;
			ck[++ct]=a;
		}
		prec[i]=prec[i-1]+(a==c);
	}
	int res=0;
	int sum,mx;
	for(int i=1;i<=ct;i++){
		int siz=mk(head[ck[i]]);
		sum=0;mx=0;
		//for(int j=1;j<=siz;j++)printf("%d ",b[j]);
		//	puts("");
		for(int j=1;j<=siz;j++){
			sum+=b[j];
			if(sum<0)sum=0;
			if(mx<sum)mx=sum;
		}
		if(res<mx)res=mx;
	}
	printf("%d\n",res+prec[n]);
}
/*
10 2
6 6 6 2 2 6 6 6 6 5
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值