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, 这样就相当于最终一定会落在地上
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cmath>
using namespace std ;
int a[], b[] ;
int dp[] ;
int main()
{
int t ;
scanf("%d",&t) ;
while(t--)
{
int n ;
scanf("%d",&n) ;
for(int i = ; i <= n ;++i) scanf("%d",&a[i]) ;
for(int i = ; i <= n ;++i) scanf("%d",&b[i]) ;
a[n + ] = a[n + ] = a[n + ] = b[n + ] = b[n + ] = b[n + ] = ;
memset(dp, , sizeof dp) ;
for(int i = ; i <= n + ; ++i)//延长地图长度为n + 3
if(i > ) dp[i] = max(b[i] + a[i - ] + a[i - ] + dp[i - ], b[i] + dp[i - ]) ;
else dp[i] = b[i] + dp[i - ] ;
printf("%d\n",dp[n + ]) ;
}
}
17996 Daily Cool Run (dp)的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场) A - run - [DP]
题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...
- uva12486 Space Elevator(数位dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题目链接:https://uva.onlinejudge.org/index.ph ...
- Linux之crontab
一 cron crond位于/etc/rc.d/init.d/crond 或 /etc/init.d 或 /etc/rc.d /rc5.d/S90crond,最总引用/var/lock/subsys ...
- Project Euler 76:Counting summations
题目链接 原题: It is possible to write five as a sum in exactly six different ways: 4 + 13 + 23 + 1 + 12 + ...
- Linux系统下使用crontab添加计划任务的方法
在服务器中添加定期执行的任务,在很多情况下是非常必要的.比如,每天清理一次/tmp目录下的文件;没几分钟检查某一守护进程是否正常等等.这样计划任务就显得尤为方便.下面将介绍如何在Linux系统中 ...
- Linux小知识点汇总
1.crontab (1)crontab每10秒执行一次 * * * * * /bin/date >>/tmp/date.txt * * * * * sleep 10; ...
- F, A, MS, QM, RF的OFFER和经历 -- Final update
昨天收到FB的电话,我的OFFER已经批下来了,这也意味着我的JOB HUNTING结束了,下 面是我这两个月来申请结果汇总: Applications (7): Facebook, Google, ...
- 算法入门经典大赛 Dynamic Programming
111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence ...
- Linux的cron和crontab
一 cron crond位于/etc/rc.d/init.d/crond 或 /etc/init.d 或 /etc/rc.d /rc5.d/S90crond,最总引用/var/lock/subsys/ ...
随机推荐
- LeetCode 453 Minimum Moves to Equal Array Elements
Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...
- codeforces gym 100694 M The Fifth Season (巴什博奕)
题目链接 一直觉得巴什博奕是最简单的博弈遇到肯定没问题,结果被虐惨了,看完标程错了10多遍都没反应过来,当然标程题解和代码的意思也写反了,但是还是想对自己说一句mdzz,傻啊!!!这道题很不错,我觉得 ...
- Keepalived虚拟ip
linux下如何设置vip(虚拟ip) 在做HA的时候需要为服务器设计虚拟IP,也就是一个主机对应多个IP地址?刚听起来好神奇,原来这样也是可能的看了下面的这个链接 自己配了一下http://hi.b ...
- 转载_虚拟机下LInux(终端)配置网络的方法
出自: http://www.360doc.com/content/14/1027/11/17496895_420258403.shtml 对文章的重点进行剪贴,方便查看. 这几天在虚拟机vmware ...
- c语言字符集
一.字符常量 'A', 'B','\n','\'','1' 二.字符类型变量的赋值 char c1='A'; char c2='b'; char c3=65; c2='\''; c2='\n'; 三. ...
- Jquery.Datatables 结合时间段查询,daterangepicker实现Datatables表格带参数查询
参考:http://datatables.club/example/user_share/send_extra_param.html 下载地址:http://pan.baidu.com/s/1 ...
- Linux安装mysql最新版本纪要
http://blog.csdn.net/frt007/article/details/50184143 http://blog.csdn.net/wb96a1007/article/details/ ...
- java 泛型 -- 泛型类,泛型接口,泛型方法
泛型T泛型的许多最佳例子都来自集合框架,因为泛型让您在保存在集合中的元素上指定类型约束.在定义泛型类或声明泛型类的变量时,使用尖括号来指定形式类型参数.形式类型参数与实际类型参数之间的关系类似于形式方 ...
- 一致性hash算法简介与代码实现
一.简介: 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance) 2.单调性(Monotonicity) 3.分散性(Spread) 4.负 ...
- SublimeText 插件
Markdown 预览 想要预览,可以配置下面的快捷方式. { "keys": ["alt+m"], "command": "ma ...