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 
#include 
#include 
#include 
#include 
#include <string>
#include 
#include 
#include 
#include <set>
#include 
#include 
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\n", ans);
    }

    return 0;
}

 

转载于:https://www.cnblogs.com/Running-Time/p/4592491.html

你可能感兴趣的:(DP UVALive 6506 Padovan Sequence)