[枚举] 多校联合第四场 hdu328 problem killer

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): 620 Accepted Submission(s): 259

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

Author
XJZX

给一串数列,它的子序列可能是等差或等比数列,求其中最长的等比或等差序列项的长度。

没说的,根据等差或者等比数列的定义扫一遍就好了。
有两个被坑的地方:
1、定义每一项的差,使用0x3f3f3f3f还不够!要开到尽量大。。
2、计算等比数列的比值时,eps要开到合适的位置!试了一下,好像1e-6是可以接受的。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
int a[1000500];
int T,n,m;
int cha[1000100];
double bi[1000100];
int main()
{
 //   freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i =1; i<=n; i++)
            scanf("%d",&a[i]);
        if (n==1 || n==2) {printf("%d\n",n);continue;}
        for(int i = 1;i<n;i++)
        {
            cha[i] = a[i+1]-a[i];
            bi[i] = (double)a[i+1]/a[i];
        }
        cha[n] = 0x6f6f6f6f;
        bi[n] = 0x6f6f6f6f;
        int ans = 1;
        for(int i = 1; i<n;i++)
        {
            int j = i;
            while( i < n && cha[i+1]==cha[i] ) i++;
            ans = max(ans,i-j+2);
        }
        for(int i = 1; i<n;i++)
        {
            int j = i;
            while( i < n && fabs(bi[i+1]-bi[i])<0.0000001) i++;
            ans = max(ans,i-j+2);
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值