2015年多校联合训练第四场(Problem Killer)hdu5328

来源:互联网 发布:淘宝 苹果证书 编辑:程序博客网 时间:2024/06/17 02:19

题意:
求最大等差或等比数列的长度
思路:
开始用二分,WA暴了,后来发现我用的等差数列公式有问题
(a[i]+a[j])*(j-i+1)/2,等差数列一定满足这个公式,但满足这个公式的不一定是等差数列,我sb了。。。。。
还有就算等比数列a[i+1]/a[i] == a[i]/a[i-1],也是sb了,这个会引起精度丢失,应该a[i]^2 = a[i-1]*a[i+1];
…….
正解应该是不管等差还是等比数列,如果a,b,c是等差,b,c,d是等差,那么a,b,c,d肯定是等差
然后扫一遍就出来了。。。。。

#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#define LL long longusing namespace std;LL sum[1001005];LL a[1001005];LL n;int main(){    #ifdef xxz   freopen("out.txt","w",stdout);    freopen("1002.in","r",stdin);    #endif // xxz    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        sum[0] = 0;        for(int i = 1; i <= n;++i)        {            scanf("%d",a+i);        }        int AP_L, AP_R,GP_L,GP_R,ans = 1;        AP_L = AP_R = GP_L = GP_R = 1;        for(int i = 2; i < n; ++i)        {            if(a[i] - a[i-1] == a[i+1] - a[i])            {                AP_R = i+1;            }            else AP_L = AP_R = i;            if(a[i]*a[i]  == a[i-1]*a[i+1])            {                GP_R = i+1;            }            else {                GP_R = GP_L = i;            }            ans = max(AP_R - AP_L + 1, ans);            ans = max(GP_R - GP_L + 1, ans);        }        if(n >= 2 ) ans = max(ans,2);        printf("%d\n",ans);    }    return 0;}
0 0