17996 Daily Cool Run (dp)

时间限制: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 <= 10
5
).
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
思路:dp[i]表示在二维矩阵中走到第二行的第i列时所获得的最大值
dp[i] = max(dp[i - 1] + a[i], dp[i - 3] + a[i - 2] + a[i - 1] + b[i]) , i > 3 ;
dp[i] = dp[i - 1] + a[i] , i <= 3
由于在最后会从 a[n] 或 b[n]处结束, 为避免处理的麻烦,干脆延长地图,并使延长部分的价值为0, 这样就相当于最终一定会落在地上
复制代码
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <iostream>
 6 #include <cmath>
 7 using namespace std ;
 8 int a[100005], b[100005] ;
 9 int dp[100005] ;
10 int main()
11 {
12     int t ;
13     scanf("%d",&t) ;
14     while(t--)
15     {
16         int n ;
17         scanf("%d",&n) ;
18         for(int i = 1; i <= n ;++i) scanf("%d",&a[i]) ;
19         for(int i = 1; i <= n ;++i) scanf("%d",&b[i]) ;
20         a[n + 1] = a[n + 2] = a[n + 3] = b[n + 1] = b[n + 2] = b[n + 3] = 0 ;
21         memset(dp, 0, sizeof dp) ;
22         for(int i = 1; i <= n + 3; ++i)//延长地图长度为n + 3
23         if(i > 3) dp[i] = max(b[i] + a[i - 1] + a[i - 2] + dp[i - 3], b[i] + dp[i - 1]) ;
24         else dp[i] = b[i] + dp[i - 1] ;
25         printf("%d\n",dp[n + 3]) ;
26     }
27 }
View Code
复制代码

 

posted @   JL_Zhou  阅读(381)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示