51nod 1285 山峰和分段
【题解】
枚举n的各个因数作为段长,O(n)判断每一段内是否有山峰即可。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #define LL long long 6 #define rg register 7 #define N 1000010 8 using namespace std; 9 int n,m,ans,a[N]; 10 bool p[N]; 11 inline int read(){ 12 int k=0,f=1; char c=getchar(); 13 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 14 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 15 return k*f; 16 } 17 inline bool check(int x){ 18 int last=1,now=x; 19 while(now<=n){ 20 bool ok=0; 21 for(rg int i=last;i<=now;i++) ok=ok|p[i]; 22 if(!ok) return 0; 23 now+=x; last+=x; 24 } 25 return 1; 26 } 27 int main(){ 28 n=read(); m=trunc(sqrt(n)); 29 for(rg int i=1;i<=n;i++) a[i]=read(); 30 for(rg int i=2;i<n;i++) if(a[i-1]<a[i]&&a[i]>a[i+1]) p[i]=1; 31 for(rg int i=1;i<=m;i++)if(n%i==0){ 32 if(check(i)) ans=max(ans,n/i); 33 if(check(n/i)) ans=max(ans,i); 34 } 35 printf("%d\n",ans); 36 return 0; 37 }