zoukankan      html  css  js  c++  java
  • 【尺取】HDU Problem Killer

    acm.hdu.edu.cn/showproblem.php?pid=5328

    【题意】

    给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多少?

    【思路】

    尺取,用来解决这样的问题:需要在给的一组数据中找到不大于某一个上限的“最优连续子序列”

    分别用双指针找最长的等差数列和等比数列,找最大值就可以了。

    注意a 或者 a b既是等差数列,又是等比数列。

    【Accepted】

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cmath>
     4 #include <vector>
     5 #include <algorithm>
     6 #include <set>
     7 #include <map>
     8 #include <queue>
     9 #include <deque>
    10 #include <stack>
    11 #include <string>
    12 #include <bitset>
    13 #include <ctime>
    14 #include<algorithm>
    15 #include<cstring>
    16 using namespace std;
    17 typedef long long ll;
    18 int n;
    19 const int maxn=1e6+2;
    20 int a[maxn];
    21 int ans;
    22 int AP()
    23 {
    24     int l=0;
    25     int r=1;
    26     int add=a[r]-a[l];
    27     int res=2;
    28     while(l<n && r<n)
    29     {
    30         while(r<n && a[r]-a[r-1]==add)
    31         {
    32             r++;
    33         }
    34         res=max(res,r-l);
    35         l=r-1;
    36         add=a[r]-a[l];
    37     }
    38     return res;
    39 }
    40 int GP()
    41 {
    42     int l=0;
    43     int r=1;
    44     double q=(double)a[r]/(double)a[l];
    45     int res=0;
    46     while(l<n && r<n)
    47     {
    48         while(r<n && (double)a[r]/(double)a[r-1]==q)
    49         {
    50             r++;
    51         }
    52         res=max(res,r-l);
    53         l=r-1;
    54         q=(double)a[r]/(double)a[l];
    55     }
    56     return res;
    57 }
    58 int main()
    59 {
    60     int T;
    61     scanf("%d",&T);
    62     while(T--)
    63     {
    64         scanf("%d",&n);
    65         for(int i=0;i<n;i++)
    66         {
    67             scanf("%d",&a[i]);
    68         }    
    69         if(n<=2)
    70         {
    71             printf("%d
    ",n);
    72             continue;
    73         }
    74         ans=2;
    75         ans=max(ans,AP());
    76         ans=max(ans,GP());
    77         printf("%d
    ",ans);
    78     }
    79     return 0;
    80  } 
    View Code
  • 相关阅读:
    Linux文本检索命令grep笔记
    Python中字典的相关操作
    Go 语言函数闭包
    Go 语言多维数组
    Go 错误处理
    Go 语言接口
    Go 语言类型转换
    Go 语言递归函数
    Go 语言Map(集合)
    Go 语言范围(Range)
  • 原文地址:https://www.cnblogs.com/itcsl/p/6964315.html
Copyright © 2011-2022 走看看