跳到正文

题解

题解:AT_abc032_c [ABC032C] 列

ZZA000HAH约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年5月28日
收录
2026年2月16日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v1当前

本题给出一个序列,从中选出一个最长的满足条件的子序列,由于 n≤105n \le 10^5,则 n2≤1010n^2 \le 10^{10},如果双循环依次遍历可能会超时,所以考虑双指针算法。

需要判断一个特殊情况,如果给出的序列中有 00 存在,那么满足条件的最长子序列长度便为 nn。

使用双指针 l,rl,r 维护区间 [l,r][l,r],ss 为区间的积,时间复杂度约为 O(n)\mathcal{O}(n)。

需要注意的是,在左端点 ll 要往右移动之前,需要先从 ss 中除去 ll 下标对应的序列中的数值,但当且仅当 l≤rl \le r 时才进行此操作(可参见样例 44)。

Code。

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int a[N];
int main()
{
	int n,k,x,ans=0;
	scanf("%d%d",&n,&k);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		if (a[i]==0)
		{
			printf("%d\n",n);
			return 0;
		}
	}
	int l=0,r=0,s=1;
	while (l<n)
	{
		while (r<n)
		{
			if (s>k) break;
			s*=a[r],r++;
		}
		ans=max(ans,r-l-1);
		if (l<=r) s/=a[l];
		l++;
	}
	printf("%d\n",ans);
	return 0;
}

End。

原文