【NOIP2017提高模拟9.7】NYG的动态数点
Description
Input
Output
Sample Input
输入1:
5
4 6 9 3 6
输入2:
30
15 15 3 30 9 30 27 11 5 15 20 110 25 20 30 15 30 15 25 5 10 20 7 7 16 2 7 7 28 7
Sample Output
输出1:
1 3
2
输出2:
1 13
9
Data Constraint
Hint
题解
水题
从左到右枚举除数,再向左和向右拓展
剪枝:扩展过的点就不用再做了
时间复杂度 近似O(N)
code
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#define R register
using namespace std;
int n,cnt,ans,a[500101],l[500101];
inline void read(R int &x){
x=0;char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
}
int main(){
read(n);
for(R int i=1;i<=n;++i)read(a[i]);
for(R int i=1;i<=n;++i){
R int x=i,y=i;
while(a[x-1]%a[i]==0&&x>1&&a[x-1]!=a[i])--x;
while(a[y+1]%a[i]==0&&y<n)++y;
if(y-x>=ans)l[++cnt]=x;
if(y-x>ans)ans=y-x,l[cnt=1]=x;
i=y;
}
printf("%d %d\n",cnt,ans);
sort(l+1,l+1+cnt);
for(R int i=1;i<=cnt;++i)printf("%d ",l[i]);
}