等差和等比各扫一遍即可。
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 using namespace std; 7 8 const int N = 1000000; 9 const double eps = 1e-8; 10 int a[N]; 11 12 int main () 13 { 14 int t; 15 scanf("%d", &t); 16 while ( t-- ) 17 { 18 int n; 19 scanf("%d", &n); 20 for ( int i = 0; i < n; i++ ) scanf("%d", a + i); 21 if ( n == 1 || n == 2 ) 22 { 23 printf("%d ", n); 24 continue; 25 } 26 int ans = 2, i = 0; 27 while ( i < n - 2 ) 28 { 29 int d = a[i] - a[i + 1]; 30 int j = i + 1; 31 while ( j < n - 1 && a[j] - a[j + 1] == d ) 32 { 33 j++; 34 } 35 ans = max( ans, j - i + 1 ); 36 i = j; 37 } 38 i = 0; 39 while ( i < n - 2 ) 40 { 41 double d = a[i] * 1.0 / a[i + 1]; 42 int j = i + 1; 43 while ( j < n - 1 && fabs( a[j] * 1.0 / a[j + 1] - d ) < eps ) 44 { 45 j++; 46 } 47 ans = max( ans, j - i + 1 ); 48 i = j; 49 } 50 printf("%d ", ans); 51 } 52 return 0; 53 }