题意:在给定的数组里,寻找一个最长的序列,满足ai-2+ai-1=ai。并输出这个序列。

很容易想到一个DP方程

dp[i][j]=max(dp[k][i])+1. (a[k]+a[i]==a[j],1<=k&&k<i)

dp[i][j]表示序列最后两位是a[i],a[j]时的最长长度。

这个方程状态是O(n^2),转移是O(n),总复杂度是O(n^3)会超时。

进一步思考会发现转移这里是可以优化的。实际上我们只需要知道离i最近的那个满足a[k]+a[i]==a[j]的k就行,即最大k。

举个例子

2 3 -1 2 1 3

当i=5,j=6,即ai=1,aj=3时,这时需要找一个ak=2的,显然a1和a4满足,我们会选择a4而不是a1。因为可以转移到a1的状态一定都可以转移到a1,但能转移到a4的状态却不一定都能转移到a1,因此dp[4][5]>=dp[1][5]。这样我们只需要在遍历数组的时候维护数组每个数的最大的下标即可。这里使用hash来做。

我用了stl的hash_map。最后需要逆序输出结果。另外注意卡内存,要用short。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<hash_map>
#include<ext/hash_map>
using namespace std;
using __gnu_cxx::hash_map;
hash_map <int,short> has;
];
][];
void output(int n,int x,int y)
{
    if(!n) return ;
    output(n-,y-x,x);
    ) printf("%d",y-x);
    else printf(" %d",y-x);
}
int main()
{
    int n;
    bool blank=false;
    while(scanf("%d",&n)!=EOF)
    {
        if(blank) printf("\n");
        else  blank=true;
        memset(dp,,sizeof(dp));
        ; i<=n; ++i)
        {
            scanf("%d",&a[i]);
            ; j<i; ++j)
                dp[j][i]=;
        }
        )
        {
            printf(]);
            continue;
        }
        has.clear();
        ,x=a[],y=a[];
        ; i<=n; ++i)
        {
            ; j<=n; ++j)
            {
                hash_map<int,short>::iterator it=has.find(a[j]-a[i]);
                if(it!=has.end())
                    dp[i][j]=dp[it->second][i]+;
                if(dp[i][j]>ans)
                {
                    ans=dp[i][j];
                    x=a[i];
                    y=a[j];
                }
            }
            has[a[i]]=i;
        }
        printf("%d\n",ans);
        ans-=;
        output(ans,x,y);
        if(ans) printf(" ");
        printf("%d %d\n",x,y);
    }
    ;
}

当然也可以定义状态 dp[i][j]为序列开头两个数字是ai,aj的最长长度。这样就可以正序输出了。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<hash_map>
#include<ext/hash_map>
using namespace std;
using __gnu_cxx::hash_map;
hash_map <int,short> has;
];
][];
int main()
{
    int n;
    bool blank=false;
    while(scanf("%d",&n)!=EOF)
    {
        if(blank) printf("\n");
        else  blank=true;
        memset(dp,,sizeof(dp));
        ; i<=n; ++i)
        {
            scanf("%d",&a[i]);
            ; j<=n; ++j)
                dp[i][j]=;
        }
        has.clear();

        ,x=a[],y;
        ; --i)
        {
            ; j>=; --j)
            {
                hash_map<int,short>::iterator it=has.find(a[i]+a[j]);
                if(it!=has.end())
                {
                    dp[j][i]=dp[i][it->second]+;
                }
                if(ans<dp[j][i])
                {
                    ans=dp[j][i];
                    x=a[j];
                    y=a[i];
                }
            }
            has[a[i]]=i;
        }
        ans++;
        printf("%d\n",ans);
        printf("%d",x);
        ; i<=ans; ++i)
        {
            printf(" %d",y);
            int t=y;
            y=x+y;
            x=t;
        }
        printf("\n");
    }
    ;
}

下面是自己实现的hash。

这个用绝对值取模的hash函数。跑了4s+。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
];
][];
pair<];
static const int mask=0x7fff;
void init()
{
    ; i<=mask; ++i) has[i].first=-;
}
int find(int x)
{
    int key=abs(x)%mask;
    while(has[key].first!=x)
    {
        )
            ;
        key=(key+)%mask;
    }
    return has[key].second;
}
void insert(int x,int val)
{
    int key=abs(x)%mask;
    )
    {
        if(has[key].first==x)
        {
            has[key].second=val;
            return ;
        }
        key=(key+)%mask;
    }
    has[key].first=x;
    has[key].second=val;
}
int main()
{
    int n;
    bool blank=false;
    while(scanf("%d",&n)!=EOF)
    {
        if(blank) printf("\n");
        else  blank=true;
        memset(dp,,sizeof(dp));
        init();
        ; i<=n; ++i)
        {
            scanf("%d",&a[i]);
            ; j<=n; ++j)
                dp[i][j]=;
        }
        ,x=a[],y;
        ; --i)
        {
            ; j>=; --j)
            {
                int it=find(a[i]+a[j]);
                )
                {
                    dp[j][i]=dp[i][it]+;
                }
                if(ans<dp[j][i])
                {
                    ans=dp[j][i];
                    x=a[j];
                    y=a[i];
                }
            }
            insert(a[i],i);
        }
        ans++;
        printf("%d\n",ans);
        printf("%d",x);
        ; i<=ans; ++i)
        {
            printf(" %d",y);
            int t=y;
            y=x+y;
            x=t;
        }
        printf("\n");
    }
    ;
}

这个是用&的hash函数。跑了1s+。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<hash_map>
#include<ext/hash_map>
using namespace std;

];
][];
pair<];
static const int mask=0x7fff;
int Hash(int val)
{
    return val&mask;
}
void init()
{
    ; i<=mask; ++i) has[i].first=-;
}
int find(int x)
{
    int key=x&mask;
    while(has[key].first!=x)
    {
        )
            ;
        key=(key+)&mask;
    }
    return has[key].second;
}
void insert(int x,int val)
{
    int key=x&mask;
    )
    {
        if(has[key].first==x)
        {
            has[key].second=val;
            return ;
        }
        key=(key+)&mask;
    }
    has[key].first=x;
    has[key].second=val;
}
int main()
{
    int n;
    bool blank=false;
    while(scanf("%d",&n)!=EOF)
    {
        if(blank) printf("\n");
        else  blank=true;
        memset(dp,,sizeof(dp));
        init();
        ; i<=n; ++i)
        {
            scanf("%d",&a[i]);
            ; j<=n; ++j)
                dp[i][j]=;
        }
        ,x=a[],y;
        ; --i)
        {
            ; j>=; --j)
            {
                int it=find(a[i]+a[j]);
                )
                {
                    dp[j][i]=dp[i][it]+;
                }
                if(ans<dp[j][i])
                {
                    ans=dp[j][i];
                    x=a[j];
                    y=a[i];
                }
            }
            insert(a[i],i);
        }
        ans++;
        printf("%d\n",ans);
        printf("%d",x);
        ; i<=ans; ++i)
        {
            printf(" %d",y);
            int t=y;
            y=x+y;
            x=t;
        }
        printf("\n");
    }
    ;
}

最后这个是watash写的hash_map。跑了1s+。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<hash_map>
#include<ext/hash_map>
using namespace std;
];
][];
struct Hash
{
    static const int mask = 0x7fff;
    ], q[];
    void clear()
    {
        ; i <= mask; ++i)
            q[i] = -;
    }
    int& operator[](int k)
    {
        int i = k & mask;
         && p[i] != k; i = (i + ) & mask);
        p[i] = k;
        return q[i];
    }
} hash;
int main()
{
    int n;
    bool blank=false;
    while(scanf("%d",&n)!=EOF)
    {
        if(blank) printf("\n");
        else  blank=true;
        memset(dp,,sizeof(dp));
        hash.clear();
        ; i<=n; ++i)
        {
            scanf("%d",&a[i]);
            ; j<=n; ++j)
                dp[i][j]=;
        }
        ,x=a[],y;
        ; --i)
        {
            ; j>=; --j)
            {
                int it=hash[a[i]+a[j]];
                )
                {
                    dp[j][i]=dp[i][it]+;
                }
                if(ans<dp[j][i])
                {
                    ans=dp[j][i];
                    x=a[j];
                    y=a[i];
                }
            }
            hash[a[i]]=i;
        }
        ans++;
        printf("%d\n",ans);
        printf("%d",x);
        ; i<=ans; ++i)
        {
            printf(" %d",y);
            int t=y;
            y=x+y;
            x=t;
        }
        printf("\n");
    }
    ;
}

ZOJ 2672 Fibonacci Subsequence(动态规划+hash)的更多相关文章

  1. ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!

    两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...

  2. LC 873. Length of Longest Fibonacci Subsequence

    A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...

  3. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  4. ZOJ 3349 Special Subsequence

    Special Subsequence Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Ori ...

  5. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  6. HDU 1159 Common Subsequence 动态规划

    2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...

  7. LeetCode 873. Length of Longest Fibonacci Subsequence

    原题链接在这里:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/ 题目: A sequence X_1, X ...

  8. 斐波那契数列Fibonacci问题—动态规划

    斐波那契数列定义 Fibonacci array:1,1,2,3,5,8,13,21,34,... 在数学上,斐波那契数列是以递归的方法来定义: F(0) = 0 F(1) = 1 F(n) = F( ...

  9. hdu1159Common Subsequence——动态规划(最长公共子序列(LCS))

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

随机推荐

  1. selenium 加载RemoteDriver浏览器驱动

    title NodeWebDriver java -jar selenium-server-standalone-2.42.2.jar -Dwebdriver.ie.driver="C:\S ...

  2. ActionErrors 使用说明 struts1 validate 处理流程 详细教程(转)

    转自(http://blog.csdn.net/wyx100/article/details/8736445). struts1  处理流程是  jsp  -->  ActionForm 中的A ...

  3. Flask中mongodb实现flask_login保持登录

    最近在学习Flask,使用flask-login时,一直无法完成保持登录的状态,网上的例子都是使用SQLAlchemy,但是我用的是mongodb. 网上的例子使用SQLAlchemy时,定义User ...

  4. [转载]SoapUI 参数化&数据库连接

    引用自 : http://www.cnblogs.com/liulinghua90/p/4954045.html 如果是没有代码能力的小白,要利用工具进行接口测试的时候,经常会遇到接口地址 或者接口参 ...

  5. MVC之URL路由

    注册路由规则集合 一个 Web 应用具有一个全局的路由表,该路由表通过 System. Web.Routing.RouteTable的静态只读属性 Routes 表示,该属性返回一个类型为 Syste ...

  6. eclipse 中提示tomcat 的端口被占用了 后的最快捷解决方法

    很多时候运行tomcat 的时候总是会提示tomcat 的端口被占用 但是任务管理器里面还找不到是哪个端口被占用了 因此很多人就重新配置tomcat  或者去修改tomcat的端口号 ,其实这么做太麻 ...

  7. UESTC 2016 Summer Training #1 Div.2

    最近意志力好飘摇..不知道坚不坚持得下去.. 这么弱还瞎纠结...可以滚了.. 水题都不会做.. LCS (A) 水 LCS (B) 没有看题 Gym 100989C 水 1D Cafeteria ( ...

  8. mongodb的监控与性能优化

    一.mongodb的监控 mongodb可以通过profile来监控数据,进行优化. 查看当前是否开启profile功能用命令 db.getProfilingLevel()  返回level等级,值为 ...

  9. CentOS 常用命令大全

    下面,就给大家介绍这些CentOS常用命令. 一:使用CentOS常用命令查看cpu more /proc/cpuinfo | grep "model name" grep &qu ...

  10. Future 模式介绍

    假设一个任务执行需要花费一些时间,为了省去不必要的等待时间,可以先获取一个提货单,即future,然后继续处理别的任务,知道货物到达,即任务完成得到结果,此时可以使用提货单提货,即通过future得到 ...