Problem Killer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3311 Accepted Submission(s): 1102
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
题意:给你一个序列,让你求最长的连续的等比数列或等差数列。
思路:直接暴力,注意不能开两个数组记录符合条件的序列,会TLE,用两个变量来回倒就可以。
代码:
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=1e6+10;
int n,m;
int a[maxn];
int ans,tmp,cnt;
int main()
{
int T,cas=1;
while(scanf("%d",&T)!=EOF)
{
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
int x,y=-1,z;
ans=1;
x=a[1];
tmp=1;
for(int i=2;i<=n;i++)
{
if(y==-1){y=x;x=a[i];tmp++;ans=max(ans,tmp);}
else if(((double)a[i]/(double)x)==((double)x/(double)y)){y=x;x=a[i];tmp++;ans=max(ans,tmp);}
else {tmp=2;y=x;x=a[i];ans=max(ans,tmp);}
}
x=a[1];y=-1;
tmp=1;
for(int i=2;i<=n;i++)
{
if(y==-1){y=x;x=a[i];tmp++;ans=max(ans,tmp);}
else if(a[i]-x==x-y){y=x;x=a[i];tmp++;ans=max(ans,tmp);}
else {tmp=2;y=x;x=a[i];ans=max(ans,tmp);}
}
printf("%d\n",ans);
}
}
return 0;
}