Leap Frog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 676    Accepted Submission(s): 244

Problem Description
Jack and Jill play a game called "Leap Frog" in which they alternate turns jumping over each other. Both Jack and Jill can jump a maximum horizontal distance of 10 units in any single jump. You are given a list of valid positions x1,x2,…,
xn where Jack or Jill may stand. Jill initially starts at position x1, Jack initially starts at position x2, and their goal is to reach position xn.Determine the minimum number of jumps needed until either Jack or
Jill reaches the goal. The two players are never allowed to stand at the same position at the same time, and for each jump, the player in the rear must hop over the player in the front.
 
Input
The input file will contain multiple test cases. Each test case will begin with a single line containing a single integer n (where 2 <= n <= 100000). The next line will contain a list of integers x1,x2,…, xn where 0 <=x1,x2,…,
xn<= 1000000. The end-of-fi le is denoted by a single line containing "0".
 
Output
For each input test case, print the minimum total number of jumps needed for both players such that either Jack or Jill reaches the destination, or -1 if neither can reach the destination.
 
Sample Input
6
3 5 9 12 15 17
6
3 5 9 12 30 40
 
Sample Output
3
-1
 

题目的意思是初始第一个点和第二个点有两个人,人每次最多前进10步,两人轮番前进,每次是后面的人跳到前面的人的前面,,问任意一人走到最后他一个点最少多少步?

本来我们如果开dp[i][j]保存两个人的位置,题目很简单了,但是n有100000,数组开不下,我们发现每次最多走十步,那就是说只要取i之前10步以内的数就好

于是定义dp[10][100000],dp[i][j]为一个人在j位置骂另一个人在他左边i个点。

代码如下

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f int dp[15][100005];
int a[100005];
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
memset(dp,-1,sizeof(dp));
dp[1][2]=0;
for(int i=3; i<=n; i++)
{
for(int j=1; j<=10; j++)
{
if(i-j<1)
break;
if(a[i]-a[i-j]<=10)
{
for(int k=j+1; k<=10; k++)
{
if(i-k<1)
break;
if(a[i]-a[i-k]<=10)
{
if(dp[j][i]==-1&&dp[k-j][i-j]!=-1)
dp[j][i]=dp[k-j][i-j]+1;
else if(dp[k-j][i-j]!=-1)
dp[j][i]=min(dp[j][i],dp[k-j][i-j]+1);
}
}
} }
}
int ans=inf;
for(int i=1; i<=10; i++)
{
if(dp[i][n]!=-1)
ans=min(ans,dp[i][n]);
}
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
} return 0;
}


HDU 3455 Leap Frog 2016-09-12 16:34 43人阅读 评论(0) 收藏的更多相关文章

  1. Help Me with the Game 分类: POJ 2015-06-29 16:34 17人阅读 评论(0) 收藏

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3706   Accepted: ...

  2. winform 解决界面闪动、提升加载速度 分类: WinForm 2015-02-03 16:34 161人阅读 评论(0) 收藏

    说明: 从一个技术交流群里获得,经验证效果不错. //作用 加快界面加载 protected override CreateParams CreateParams          {         ...

  3. iOS自定义字体及类目 分类: ios技术 2015-05-15 16:34 195人阅读 评论(0) 收藏

    1:获取字体文件 从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例) 2:将fzltxh.ttf文件拷贝到工程中 3:在Info.plist中添加项: Fon ...

  4. Hdu2204 Eddy's爱好 2017-06-27 16:11 43人阅读 评论(0) 收藏

    Eddy's爱好 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Subm ...

  5. HRBUST1315 火影忍者之~大战之后 2017-03-06 16:14 54人阅读 评论(0) 收藏

    火影忍者之-大战之后 经历了大战的木叶村现在急需重建,人手又少,所以需要尽可能多的接受外来的任务,以赚取报酬,重建村庄,假设你现在是木叶的一名高级忍者,有一大堆的任务等着你来做,但毕竟个人时间有限,所 ...

  6. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  7. hdu 1052 (greedy algorithm) 分类: hdoj 2015-06-18 16:49 35人阅读 评论(0) 收藏

    thanks to http://acm.hdu.edu.cn/discuss/problem/post/reply.php?action=support&postid=19638&m ...

  8. hdu1171 Big Event in HDU(01背包) 2016-05-28 16:32 75人阅读 评论(0) 收藏

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏

    a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...

随机推荐

  1. 疯狂JAVA——第八章 java集合

    集合类主要负责保存.盛装其他数据,因此集合类也被称为容器类. 数组元素既可以是基本类型的值,也可以是对象(实际上是保存的对象的引用): 集合里只能保存对象.

  2. R画散点图、线型图、箱型图、直方图基本知识

    1.导入数据 2.散点图 plot(iris[,1]~iris[,4],xlab='Length',ylab='Width',col='red',main='Length VS Width')

  3. IDEA artifacts Web Application:Exploded Web Application:Archive

    首先,artifacts是maven中的一个概念,表示项目/modules如何打包,比如jar,war,war exploded,ear等打包形式,一个项目或者说module有了artifacts 就 ...

  4. web程序的配置文件的修改

    vs上面的config与wwwroot中的config有可能不一样,注意修改配置的资源路径及ip及数据库及禁止ip

  5. VB 共享软件防破解设计技术初探(一)

    VB 共享软件防破解设计技术初探(一) ×××××××××××××××××××××××××××××××××××××××××××××× 其他文章快速链接: VB 共享软件防破解设计技术初探(二)http ...

  6. 源码包的解压 .tar.gz /.tar.bz2的解压

    解压方式如下: .tar.gz     格式解压为          tar   -zxvf   xx.tar.gz .tar.bz2   格式解压为          tar   -jxvf    ...

  7. Extjs面板和布局初探

    面板相当于一张干净的白纸,如果直接在上面添加内容,将很难控制面板中内容的显示位置,面板元素越多就越显得凌乱,所以需要在面板上划分不同的区域,将面板内容展示到希望的位置上.ExtJS通过提供多种布局类来 ...

  8. [leetcode]278. First Bad Version首个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  9. linux 下 php 安装 pthreads

    1.下载pthreads的源码包 https://pecl.php.net/package/pthreads 如:pthreads-3.1.6.tgz 2.解压 > tar zxvf pthre ...

  10. 20172325 2017-2018-2 《Java程序设计》第八周学习总结

    20172325 2017-2018-2 <Java程序设计>第八周学习总结 教材学习内容总结 1.关于绑定 绑定:在执行程序时产生一个请求事件,需要执行一段代码来来完成方法调用,即一个方 ...