Description
Daily Cool Run is a popular game, and Xdp enjoys playing the game recently.
While playing the game, you may get normal coins or flying coins by running and jumping.
Now, he meets a problem that what is the maximum score he can obtain.
To simplify the problem, we suppose that the maps of the game are 2 * n retangles, whose second rows are the ground.
At the beginning of the game, the player will start from the grid (2, 1) (the lower left corner of a map).
During the game, you have two choices, Run or Jump. When you are on the ground of grid (2, i),
1. Run to grid (2, i + 1);
2. Jump to grid (2, i + 3) by go through grids (1, i + 1) and (1, i + 2).
Know that you can’t land while jumping , and must follow the path stated above .When you arrive one of the last two grids, the game will be over.
Now, Xdp knows the maps of the game, whose grids are assigned to a value x(0 <= x <= 100).
If x=0, it means this grid is empty, else it means there is a coin whose value is x.
Now, can you tell me what is the maximum score you can get?
输入格式
There are at most 100 cases.
The first line is an integer T, the number of the cases.
In each case, the first line is a integer n (n <= 105).
The Following two lines this a 2 * n rectangular, that means in each line,
there are n integers (x1, x2, …. xn). ( 0 <= x < =100, 0 means that this gird is an empty gird,
others represent the coins, x is its value).
输出格式
For each test case, output a single line with an integer indicates the maximum score .
输入样例
2
8
0 0 1 1 0 1 1 0
2 1 0 0 1 0 0 1
5
0 0 1 1 0
2 1 2 0 1
输出样例
9
6
题意:2*n的矩阵,从(2,1)每次可以往右走一步,或者经过(1,i+1)和(1,i+2)然后落在(2,i+3),若碰到最右边两个就算退出。问能得到的最大贡献 |
思路(动态规划DP):
当前这个点的状态,可以由左边一个点向右走一步得到。也可以左边三个位置之前的地方跳过来,同时获取上面两个格子的贡献。如果其中一步取不到(越界)就取贡献为0 。 写成状态转移方程就是
ll DP()
{
ll y;
rep(i,1,n+2)
y = i, dp[i] = max ( ( check(1,y-1)&&check(1,y-2)&&check(2,y-3)?dp[y-3]+a[1][y-2]+a[1][y-1]:0) , dp[y-1] ) + a[2][i];
return max(dp[n], max(dp[n+2], dp[n+1]));
}
或者写成记忆化搜索的形式:
ll dfs(ll x, ll y)
{
if(!check(x,y)) return 0;
if(vis[x][y]!=-1) return vis[x][y];
if(y==n+2)
{
return vis[x][y] = a[x][y];
}
ll tmp1=0 ,tmp2=0;
if(check(x,y+1))
tmp1 = dfs(x,y+1)+a[x][y];
if(check(x,y+3))
tmp2 = dfs(x,y+3)+ (check(1,y+1)? a[1][y+1]:0) + (check(1,y+2)?a[1][y+2]:0) + a[x][y];
return vis[x][y] = max(tmp1,tmp2);
}
因为题目说了碰到右边界就算结束。所以有可能到右边界的时候是在空中(上面那一格),所以DP的时候多计算两个位置。
AC代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll a[4][maxn];
ll dp[maxn];
ll n;
bool check(ll x ,ll y)
{
if(x<1||y<1||x>2||y>n+2) return false;
return true;
}
ll DP()
{
ll y;
rep(i,1,n+2)
y = i, dp[i] = max ( ( check(1,y-1)&&check(1,y-2)&&check(2,y-3)?dp[y-3]+a[1][y-2]+a[1][y-1]:0) , dp[y-1] ) + a[2][i];
return max(dp[n], max(dp[n+2], dp[n+1]));
}
int main()
{
int kase;
cin>>kase;
while(kase--)
{
n = read(); mem(a,0); mem(dp,0);
rep(i,1LL,2LL) rep(j,1LL,n) a[i][j] = read();
cout<<DP()<<'\n';
}
return 0;
}