zoukankan      html  css  js  c++  java
  • DP UVALive 6506 Padovan Sequence

    题目传送门

    /*
        题意:两行数字,相邻列一上一下,或者隔一列两行都可以,从左到右选择数字使和最大
        DP:状态转移方程:dp[i][j] = max (dp[i][j], dp[1-i][j-1] + a[i][j], dp[i/1-i][j-2] + a[i][j]);
        要从前面一个转态推过来啊,我比赛写反了,内功不够:(
    */
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    const int MAXN = 1e5 + 10;
    const int INF = 0x3f3f3f3f;
    int a[2][MAXN];
    int dp[2][MAXN];
    
    int main(void)        //UVALive 6506 Padovan Sequence
    {
    //    freopen ("K.in", "r", stdin);
    
        int t;    scanf ("%d", &t);
        while (t--)
        {
            int n;    scanf ("%d", &n);
            memset (dp, 0, sizeof (dp));
    
            for (int i=0; i<=1; ++i)
            {
                for (int j=1; j<=n; ++j)
                {
                    scanf ("%d", &a[i][j]);
                }
            }
    
            dp[0][1] = a[0][1];    dp[1][1] = a[1][1];
            int ans = max (dp[0][1], dp[1][1]);
            for (int j=2; j<=n; ++j)
            {
                for (int i=0; i<=1; ++i)
                {
                    dp[i][j] = max (dp[i][j], dp[1-i][j-1] + a[i][j]);
                    if (j > 2)
                    {
                        dp[i][j] = max (dp[i][j], dp[i][j-2] + a[i][j]);
                        dp[i][j] = max (dp[i][j], dp[1-i][j-2] + a[i][j]);
                    }
                    ans = max (ans, dp[i][j]);
                }
            }
    
            printf ("%d
    ", ans);
        }
    
        return 0;
    }
    编译人生,运行世界!
  • 相关阅读:
    java8学习之Optional深入详解
    java8学习之Supplier与函数式接口总结
    java8学习之Predicate深入剖析与函数式编程本质
    conda
    matplotlib-折线图、散点图
    欧盟GDPR通用数据保护条例-原文
    python装饰器的应用案例
    python练习汇总
    python分段算利润、税收
    需求-shidebing
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4592491.html
Copyright © 2011-2022 走看看