17996 Daily Cool Run(动态规划dp)

17996 Daily Cool Run

时间限制:1000MS  内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题   语言: 不限定

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
思路:动态规划。每一步之前要么是跳过来,要么是跑过来。maxdp[i]记录前i步得到的最大分值,那么状态转移方程为
maxdp[i]=max{maxdp[i-1]+ground[i],sky[i-1]+sky[i-2]+maxdp[i-3]+ground[i]}(i>=4);
maxdp[i]=maxdp[i-1]+ground[i] (2=<i<=3,因为这时候任何一步之前都不能跳过来);
maxdp[i]=ground[i] (i<=1)。
动态规划,一定要弄清楚上一步是什么!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
long long sky[100010],ground[100010],maxdp[100010];
long long max(long long a,long long b)
{
        if(a>=b)
                return a;
        else return b;
}
long long maxthree(long long a,long long b,long long c)
{
        if(a>=b&&a>=c)
                return a;
        else if(b>=a&&b>=c)
                return b;
        else if(c>=a&&c>=b)
                return c;
}
int main()
{
      long long T;
      scanf("%lld",&T);
      while(T--)
      {
              memset(maxdp,0,sizeof(maxdp));
              memset(sky,0,sizeof(sky));
              memset(ground,0,sizeof(ground));
               long long n,i,j;
               long long s;
               scanf("%lld",&n);
               for(i=1;i<=n;i++)
                scanf("%lld",&sky[i]);
               for(i=1;i<=n;i++)
                scanf("%lld",&ground[i]);
                if(n>=2)
               for(i=1;i<=n+2;i++)
               {
                       if(i==1)
                       maxdp[i]=ground[i];
                       else if(i>=2&&i<=3)
                       maxdp[i]=maxdp[i-1]+ground[i];
                       else if(i>=4)
                       maxdp[i]=max(ground[i]+maxdp[i-1],sky[i-1]+sky[i-2]+maxdp[i-3]+ground[i]);
               }
               if(n<=1)
                s=ground[n];
               else
                s=maxthree(maxdp[n],maxdp[n+1],maxdp[n+2]);
               printf("%lld\n",s);
      }
      return 0;
}

你可能感兴趣的:(17996 Daily Cool Run(动态规划dp))