17996 daily cool run (DP)

Source: Internet
Author: User

Time Limit: 1000 ms memory limit: 65535 K
Submission times: 0 pass times: 0

Question type: Programming Language: Not Limited

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?




Input Format
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).



Output Format
For each test case, output a single line with an integer indicates the maximum score . 



Input example
280 0 1 1 0 1 1 02 1 0 0 1 0 0 150 0 1 1 02 1 2 0 1



Output example
96
Idea: DP [I] indicates the maximum value obtained when the column I in the second row is obtained in a two-dimensional matrix.
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
Since it will end at the end of a [n] Or B [N], to avoid the trouble of processing, simply extend the map and make the value of the Extended Part 0, this is equivalent to eventually falling onto the ground.
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) // extend the map length to N + 323 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

 

17996 daily cool run (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.