时间限制: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 }
时间: 2024-10-04 19:22:17