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 }

时间: 2024-10-04 19:22:17

17996 Daily Cool Run (dp)的相关文章

UVA 590 Always on the run(DP)

Screeching tires. Searching lights. Wailing sirens. Police cars everywhere. Trisha Quickfinger did it again! Stealing the `Mona Lisa' had been more difficult than planned, but being the world's best art thief means expecting the unexpected. So here s

docker入门篇

基础知识不回顾了,直接上. docker的安装与启动 yum remove docker -y yum install docker-io -y # 需要先配置好epel源 /etc/init.d/docker start chkconfig docker on 获取镜像 docker pull centos       # 从docker仓库下载一个镜像例如:docker pull centos:6.7 docker images            #列出本地已存在的镜像 docker i

Linux的cron和crontab

一 cron crond位于/etc/rc.d/init.d/crond 或 /etc/init.d 或 /etc/rc.d /rc5.d/S90crond,最总引用/var/lock/subsys/crond. cron是一个linux下的定时执行工具(相当于windows下的scheduled task),可以在无需人工干预的情况下定时地运行任务task.由于cron 是Linux的service(deamon),可以用以下的方法启动.关闭这个服务: /sbin/service crond

算法入门经典大赛 Dynamic Programming

111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence LCS 674 - Coin Change 全然背包求方案数 10003  - Cutting Sticks 区间DP dp[l][r]代表分割l到r的最小费用 116 - Unidirectional TSP 简单递推 输出字典序最小解 从后往前推 10131 - Is Bigger Smarte

Linux -- crontab

*manpages*File: *manpages*,  Node: crontab,  Up: (dir) CRONTAB(1)                  General Commands Manual                 CRONTAB(1) NAME       crontab - maintain crontab files for individual users (Vixie Cron) SYNOPSIS       crontab [ -u user ] fil

一些linux小笔记

1.crontab (1)crontab每10秒执行一次 * * * * * /bin/date >>/tmp/date.txt * * * * * sleep 10; /bin/date >>/tmp/date.txt (2)还可以用以下方式表达 string            meaning ------           ------- @reboot        Run once, at startup. @yearly         Run once a yea

Project Euler 76:Counting summations

题目链接 原题: It is possible to write five as a sum in exactly six different ways: 4 + 13 + 23 + 1 + 12 + 2 + 12 + 1 + 1 + 11 + 1 + 1 + 1 + 1 How many different ways can one hundred be written as a sum of at least two positive integers? 翻译: 加和计数 将5写成整数的和有

Linux Crontab及使用salt进行管理

一.引言: 最近无意之间看到salt有一个cron的模块,今天就在这里介绍linux crontab以及通过salt的cron对crontab的管理. 二.Linux crontab的介绍: crontab是用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放在"crontab"文件中,以供之后读取和执行.crontab存储的指令被守护进程激活,crond常常在后台运行,每一分钟检查是否有预定的作业需要执行. 2.1.crond的启动与关闭: #查看crond的状态 [

inux的cron和crontab

一 cron crond位于/etc/rc.d/init.d/crond 或 /etc/init.d 或 /etc/rc.d /rc5.d/S90crond,最总引用/var/lock/subsys/crond. cron是一个linux下的定时执行工具(相当于windows下的scheduled task),可以在无需人工干预的情况下定时地运行任务task.由于cron 是Linux的service(deamon),可以用以下的方法启动.关闭这个服务: /sbin/service crond