这道题就是给你一列数组。求里边等差或者等比最长的序列长度。比较简单,但是卡数据。就是等比有可能公比是非整数。需要考虑到这一点。然后输入的时候,依次算出公比和公差,然后累加判断就可以了。
Problem Killer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1035 Accepted Submission(s): 397
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
Author
XJZX
Source
题目传送门:HDU5328 Problem Killer
代码如下:
#include <cstdio>
#include <iostream>
#define N 1000005
int a[N];
int b[N];
float c[N];
int main()
{
int T,i,j,n,ans1,ans2,ans;
scanf("%d",&T);
while(T--)
{
ans1=1;
ans2=1;
ans=1;
scanf("%d",&n);
scanf("%d",&a[0]);
for(i=1;i<n;i++)
{
scanf("%d",&a[i]);
b[i-1]=a[i]-a[i-1];
c[i-1]=(float)a[i]/(float)a[i-1];
}
if(n==1)
{
printf("1\n");
continue;
}
for(i=1;i<n-1;i++)
{
if(b[i]==b[i-1])
{
ans1++;
if(ans1>ans)
ans=ans1;
}
else
ans1=1;
if(c[i]==c[i-1])
{
ans2++;
if(ans2>ans)
ans=ans2;
}
else
ans2=1;
}
printf("%d\n",ans+1);
}
return 0;
}