Now you have nn problems, the ii-th problem's difficulty is represented by an integer aiai (1≤ai≤1091≤ai≤109).
For some strange reason, you must choose some integer ll and rr (1≤l≤r≤n1≤l≤r≤n), and solve the problems between the ll-th and the rr-th, and these problems' difficulties must form an AP (Arithmetic Progression) or a GP (Geometric Progression).
So how many problems can you solve at most?
You can find the definitions of AP and GP by the following links:
https://en.wikipedia.org/wiki/Arithmetic_progression
https://en.wikipedia.org/wiki/Geometric_progression
For each test case, the first line contains a single integer nn, the second line contains nn integers a1,a2,⋯,ana1,a2,⋯,an.
T≤104,∑n≤106T≤104,∑n≤106 OutputFor each test case, output one line with a single integer, representing the answer. Sample Input
2 5 1 2 3 4 6 10 1 1 1 1 1 1 2 3 4 5Sample Output
4
6
題意爲 : 找一段數中最長的等比等差數列,輸出其中最長的一個長度。
暴力 注意 6 5 4 1 1 1 最後的判斷,與等比數列採用double存等比。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int INF=0X3f3f3f3f;
const int N=1e6+5;
int n,p[N];
int a[N];
double c[N];
int maxa,maxc;
void find()
{
int i;
int tempa=2,tempc=2;
for(i=1;i<n-1;i++)
{
if(a[i]==a[i-1])
{
tempa++;
}
else
{
maxa=maxa>tempa?maxa:tempa;
tempa=2;
}
if(c[i]==c[i-1]) tempc++;
else
{
maxc=maxc>tempc?maxc:tempc;
tempc=2;
}
}
maxa=maxa>tempa?maxa:tempa;
maxc=maxc>tempc?maxc:tempc;
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
maxa=0;
maxc=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
if(n<=2)
{
printf("%d\n",n);
continue;
}
for(i=1;i<n;i++)
{
a[i-1]=p[i]-p[i-1];
c[i-1]=(double)p[i]/p[i-1];
}
find();
maxa=maxa>maxc?maxa:maxc;
printf("%d\n",maxa);
}
return 0;
}