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. How to Pronounce WH Words — what, why, which

    How to Pronounce WH Words — what, why, which Share Tweet Share Have you noticed that there are two d ...

  2. tensor flow 的两种padding方式

    https://segmentfault.com/a/1190000007846181

  3. 关于关闭TAB,IFRAME占用的内存不能释放问题

    资料来源:http://jxd-zxf.iteye.com/blog/1440611 使用TAB时注意,如果TAB是引用IFRAME,关闭TAB时IFRAME不会被销毁从而导致内存不能释放,大量使用T ...

  4. Java发送HTTPS请求

    前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊, ...

  5. Null Hypothesis and Alternate Hypothesis

    1.Null Hypothesis Overview 零假设,H0是普遍接受的事实;这与备择假设(alternate hypothesis)正好相反.研究人员努力否定.驳斥零假设.研究人员提出了另一种 ...

  6. luoguP1090 合并果子 (贪心+优先队列)

    题目链接:https://www.luogu.org/problemnew/show/P1090 思路: 典型的贪心题,显然每次选择两个最小的堆合并最后耗费的体力最少,但每次合并之后都需要寻找最小的两 ...

  7. Java Tomcat下载、安装和环境变量配置

    win10下Tomcat的下载.安装和环境变量的配置 -----made by siwuxie095                             1.首先到Tomcat官网,传送阵:点击开 ...

  8. 【校招面试 之 C/C++】第11题 C++ 纯虚函数

    1.纯虚函数 成员函数的形参后面写上=0,则成员函数为纯虚函数. 纯虚函数声明: virtual 函数类型 函数名 (参数表列) = 0: class Person { virtual void Di ...

  9. python之列表【list】

    这里介绍下列表的功能 #切片:列表[a:b],从下标为a开始,到下标为(b-1)的元素 # name = [0,1,2,3,4,5,6,7,8,9] # print(name[1:6]) # # 结果 ...

  10. python之socket运用之执行命令

    服务端的代码 import socket import subprocess HOST = "127.0.0.1" PORT = 5001 ip_bind = (HOST,PORT ...