zoukankan      html  css  js  c++  java
  • HDU 5328 Problem Killer(2015多校联合)


    题目链接

    戳我

    题目大意

    一个序列 (a), 求这个序列的 连续子序列 中最长的等差序列或者等比序列

    样例解释

    2 // T
    5 //n
    1 2 3 4 6 //n个数, 1,2,3,4是最长等差序列,长度为4
    10 //n
    1 1 1 1 1 1 2 3 4 5 //前6个1满足等差(比)序列,且长度最长是6

    解题思路

    暴力查询即可
    此题坑点: 等比序列 的 公比 可能时 分数.

    代码

    //Author LJH
    //www.cnblogs.com/tenlee
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #define clc(a, b) memset(a, b, sizeof(a))
    using namespace std;
    
    const int inf = 0x3f;
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e6+5;
    const double eps = 1e-6;
    
    int n;
    double a[maxn];
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d", &n);
            for(int i = 0; i < n; i++) 
            {
                scanf("%lf", &a[i]);
            }
            double ap = a[1] - a[0];
            double gp = a[1] / a[0];
            int len1 = 1, len2 = 1;
            int malen1 = 1, malen2 = 1;
            for(int i = 1; i < n; i++)
            {
                if(abs(a[i] - a[i-1] - ap) < eps) len1++;
                else
                {
                    ap = a[i] - a[i-1];
                    len1 = 2;
                }
                malen1 = (malen1 > len1 ? malen1 : len1);
    
                if(abs(a[i] / a[i-1] - gp) < eps) len2++;
                else
                {
                    gp = a[i] / a[i-1];
                    len2 = 2;
                }
                malen2 = (malen2 > len2 ? malen2 : len2);
            }
            printf("%d
    ", malen1 > malen2 ? malen1 : malen2);
        }
        return 0;
    }
    
  • 相关阅读:
    docker一些基本操作
    Error requesting socket: exit status 255(一个很不错的解决办法)【转】
    十五周至十八周的任务进度
    7月24号day16总结
    7月23号day15总结
    7月22号day14总结
    7月21号day13总结
    7月20号day12总结
    7月19日day11总结
    7月18号day10总结
  • 原文地址:https://www.cnblogs.com/tenlee/p/4693019.html
Copyright © 2011-2022 走看看