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

    return 0;
}
时间: 2024-12-17 15:32:42

DP UVALive 6506 Padovan Sequence的相关文章

找规律 UVALive 6506 Padovan Sequence

题目传送门 1 /* 2 找规律:看看前10项就能看出规律,打个表就行了.被lld坑了一次:( 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <iostream> 7 #include <cstring> 8 #include <cmath> 9 #include <string> 10 #include <vector> 11 #include &l

状压DP uvalive 6560

1 // 状压DP uvalive 6560 2 // 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值 3 // 思路: 4 // dp[i][j]:第i行j状态下的值 5 // j:0表示不合并,1表示向下合并 6 // 一开始输入要修改一下,然后滚动数组优化 7 8 #include <iostream> 9 #include <algorithm> 10 #include <cstring> 11 #include &

区间DP [POJ 1141] Brackets Sequence

Brackets Sequence Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, th

Regionals 2013 Asia - Daejeon (部分题目题解)

题目链接:Regionals 2013 Asia - Daejeon 6500 Boxes 题意:将箱子(矩阵的1)全移动到矩阵的底部需要几步 思路:按列从下到上统计.(n,m)的矩阵,移动一个箱子(x,y),如果有c个箱子在底部,那么移动该箱子的步数是(n-x-c-1). AC代码: #include <stdio.h> #include <string.h> int mp[110][110]; int main() { int t; int i,j,n,m; scanf(&qu

UVA1626 / ZOJ1463 Brackets sequence 区间DP

简单区间DP (有空串... ...) Brackets sequence Time Limit: 4500MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence. If S is a

Arithmetic Slices II - Subsequence LT446

446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequences: 1,

UVALive 4764 简单dp水题(也可以暴力求解)

B - Bing it Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4764 Description I guess most of you played cards on the trip to Harbin, but I'm sure you have never played the following card game. Thi

HDU 6065 RXD, tree and sequence (LCA DP)

RXD, tree and sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 234    Accepted Submission(s): 82 Problem Description RXD has a rooted tree T with size n, the root ID is 1, with the dep

poj 1699 Best Sequence(AC自动机+状压DP)

题目链接:poj 1699 Best Sequence 题目大意:给定N个DNA序列,问说最少多长的字符串包含所有序列. 解题思路:AC自动机+状压DP,先对字符串构造AC自动机,然后在dp[s][i]表示匹配了s,移动到节点i时候的最短步数. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include &