Link:http://acm.hdu.edu.cn/showproblem.php?pid=5328
Problem Killer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 153 Accepted Submission(s): 61
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
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
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
Source
分析:该题求的是区间中连续的数组元素所能构成等差数列或等比数列的最长长度是多少。主要解题思想是用f1[i]、f2[i]表示以a[i]结尾的最长等差数列、最长等比数列的长度,然后 利用等差数列、等比数列的性质,可得到若能构成等差数列或等比数列,则相应的有递推公式f1[i]=f1[i-1]+1或f2[i]=f2[i-1]+1。详见代码注释。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#define LL long long
#define MAXN 1000010
using namespace std;
LL a[MAXN],f1[MAXN],f2[MAXN];//f1[i]、f2[i]表示以a[i]结尾的最长等差数列、最长等比数列的长度
int main()
{
int t,n,i,j;
LL ans;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
if(n<=2)
{
ans=n;
}
else
{
ans=2;
for(i=1;i<=n;i++)
{
f1[i]=2;
f2[i]=2;
}
for(i=3;i<=n;i++)
{
if(a[i-1]*2==a[i-2]+a[i])//等差数列性质
{
f1[i]=f1[i-1]+1;
}
if(a[i]*a[i-2]==a[i-1]*a[i-1])// 等比数列性质
{
f2[i]=f2[i-1]+1;
}
ans=max(ans,max(f1[i],f2[i]));
}
}
printf("%lld\n",ans);
}
return 0;
}