Problem Killer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1162 Accepted Submission(s): 449
Problem Description
You are a "Problem Killer", you want to solve many problems.
Now you have n problems, the i-th problem's difficulty is represented by an integer ai (1≤ai≤109).
For some strange reason, you must choose some integer l and r (1≤l≤r≤n), and solve the problems between the l-th and the r-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
Input
The first line contains a single integer T, indicating the number of cases.
For each test case, the first line contains a single integer n, the second line contains n integers a1,a2,⋯,an.
T≤104,∑n≤106
Output
For 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 5
Sample Output
4
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1162 Accepted Submission(s): 449
Problem Description
You are a "Problem Killer", you want to solve many problems.
Now you have n problems, the i-th problem's difficulty is represented by an integer ai (1≤ai≤109).
For some strange reason, you must choose some integer l and r (1≤l≤r≤n), and solve the problems between the l-th and the r-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
Input
The first line contains a single integer T, indicating the number of cases.
For each test case, the first line contains a single integer n, the second line contains n integers a1,a2,⋯,an.
T≤104,∑n≤106
Output
For 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 5
Sample Output
4
6
//题意: 求的是形成连续的等差或者等比数列的个数 最大值为多少
#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;
int a[1000010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,cnt2,cnt1,ma=0;
scanf("%d",&n);
cnt2=cnt1=2;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(i>1)
{
if(a[i]-a[i-1]==a[i-1]-a[i-2])
cnt1++;
else
cnt1=2;
if(fabs(1.0*a[i]/a[i-1]-1.0*a[i-1]/a[i-2])<10e-7) //注意形成分数的等比
cnt2++;
else
cnt2=2;
}
int t=max(cnt1,cnt2);
if(t>ma)
ma=t;
}
if(n<3)
{
printf("%d\n",n);
}
else
printf("%d\n",ma);
}
return 0;
}