B.Arithmetic Progression
“In mathematics, an arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, … is an arithmetic progression with common difference of 2.”
- Wikipedia
This is a quite simple problem, give you the sequence, you are supposed to find the length of the longest consecutive subsequence which is an arithmetic progression.
Input
There are several test cases. For each case there are two lines. The first line contains an integer number N (1 <= N <= 100000) indicating the number of the sequence. The following line describes the N integer numbers indicating the sequence, each number will fit in a 32bit signed integer.
Output
For each case, please output the answer in one line.
Sample Input
6
1 4 7 9 11 14
Sample Output
3
比赛时这题没解出,由于题目没有看清楚数据可以有100000组的,我没看清楚数组就只开到10001。一直wa了9次,最后看到测试数据才知道有100000.o(︶︿︶)o 唉
#include <stdio.h>
#include <string.h>
int main()
{
long long stu[100001]={0},t,q,k,max=0;
int i,j;
// freopen("ap.in","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%lld",&t))
{
memset(stu,0,sizeof(stu));
max=0;
for(i=1;i<=t;i++)
scanf("%lld",&stu[i]);
if(t==1||t==2)
{
printf("%lld\n",t);
}
else
{
for(i=1;i<t-1;i=j-1)
{
k=2;q=stu[i+1]-stu[i];
for(j=i+1;j<t;j++)
{
if(stu[j+1]==stu[i]+(j+1-i)*q)
{
k++;
if(k>max)
max=k;
}
else
{
j++;
if(k>max)
max=k;
break;
}
}
}
printf("%lld\n",max);
}
}
return 0;
}