zoukankan      html  css  js  c++  java
  • cf255C Almost Arithmetical Progression

    C. Almost Arithmetical Progression
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as:

    • a1 = p, where p is some integer;
    • ai = ai - 1 + ( - 1)i + 1·q (i > 1), where q is some integer.

    Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression.

    Sequence s1,  s2,  ...,  sk is a subsequence of sequence b1,  b2,  ...,  bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1  ≤  i1  <  i2  < ...   <  ik  ≤  n), that bij  =  sj. In other words, sequence s can be obtained from b by crossing out some elements.

    Input

    The first line contains integer n (1 ≤ n ≤ 4000). The next line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106).

    Output

    Print a single integer — the length of the required longest subsequence.

    Examples
    input
    2
    3 5
    output
    2
    input
    4
    10 20 10 30
    output
    3
    Note

    In the first test the sequence actually is the suitable subsequence.

    In the second test the following subsequence fits: 10, 20, 10.

    dp[i][j]表示以a[i]为尾,a[j]是这个序列前一个数的最长长度

    dp[i][j]=max(dp[j][k]+1){0<=k<j&&a[k]==a[i]}但这样复杂度是O^3

    但是dp[j][k]中的k肯定是选择最靠近j的那个,假如有k1<k2<……<kx<j,dp[j][kx]>=dp[j][k(1~x-1)]

    这样的话,复杂度就变成O^2了

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    typedef long long ll;
    #define X first
    #define Y second
    #define mp(a,b) make_pair(a,b)
    #define pb push_back
    #define sd(x) scanf("%d",&(x))
    #define Pi acos(-1.0)
    #define sf(x) scanf("%lf",&(x))
    #define ss(x) scanf("%s",(x))
    #define maxn 10000000
    #include <ctime>
    const int inf=0x3f3f3f3f;
    const long long mod=1000000007;
    using namespace std;
    int dp[5005][5005];
    int num[5005];
    int main()
    {
        #ifdef local
        freopen("in","r",stdin);
        //freopen("data.txt","w",stdout);
        int _time=clock();
        #endif
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
            sd(num[i]);
        int ans=0;
        int la;
        for(int i=1;i<=n;i++)
        {
            la=0;
            for(int j=0;j<i;j++)
            {
                dp[i][j]=dp[j][la]+1;
                if(num[i]==num[j])la=j;
                ans=max(dp[i][j],ans);
            }
        }
        cout<<ans<<endl;
        #ifdef local
        printf("time: %d
    ",int(clock()-_time));
        #endif
    }
    View Code

     

  • 相关阅读:
    javascript
    js中 filter()方法
    DOM 属性方法用法
    zabbix-4.0-如何监控windows服务和实战监控SQL SERVER数据
    路由交换-华为交换机查询MAC/ARP配置电脑地址实战
    杀毒软件-使用杀毒软件带的防火墙设置端口访问控制-保护终端安全
    windows-win10各版本升级密钥
    等保心得经验总结
    Emacs服务器模式以及emacsclient配置
    PyTorch自动微分基本原理
  • 原文地址:https://www.cnblogs.com/scau-zk/p/5654397.html
Copyright © 2011-2022 走看看