【NOIP2014模拟10.26】数字对

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Larry1118/article/details/86645256

一个很容易想到的O(n)做法。
由于区间肯定不会重叠,我们可以从左到右开始找区间,每次左端点变成上个右端点+1
上标:

#include<cstdio>
#define N 500010
using namespace std;
int n,a[N],c[N],l,r,ans=0,s=0,tot=0;

inline int read()
{
	int x=0; char c=getchar();
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x;
}
int main()
{
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=n;i=r+1)
	{
		l=r=i;
		while (l>1 && a[l-1]%a[i]==0) l--;
		while (r<n && a[r+1]%a[i]==0) r++;
		s=r-l;
		if (s>ans) ans=s,c[tot=1]=l;
		else if (s==ans) c[++tot]=l;
	}
	printf("%d %d\n",tot,ans);
	for (int i=1;i<=tot;i++) printf("%d ",c[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Larry1118/article/details/86645256