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 mysql大数据量批量插入与流式读取分析

    总结下这周帮助客户解决报表生成操作的mysql 驱动的使用上的一些问题,与解决方案.由于生成报表逻辑要从数据库读取大量数据并在内存中加工处理后在 生成大量的汇总数据然后写入到数据库.基本流程是 读取- ...

  2. JS中回调函数的使用

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 解决windows 下mysql 表名自动转成小写的问题

    由于web用的是mvc,数据库用的是mysql.为了方便开发,在windows7下面也安装了个mysql,今天在创建表的时候,遇到了个棘手的问题.所有的表名都转成了小写,这不是我要的,作为处女座,是不 ...

  4. SVN常见问题I

    TortoiseSVN ->Settings 把权限给清空,不让用户A的权限再保留在里面 右键->SVN Checkout 之后需要在A账户和B账户之间来回切换

  5. Finding Hotels

    Finding Hotels http://acm.hdu.edu.cn/showproblem.php?pid=5992 Time Limit: 2000/1000 MS (Java/Others) ...

  6. VMware克隆CentOS网络配置

    配置网络 如果是克隆CentOS的: vi /etc/udev/rules.d/70-persistent-net.rules 注释掉网络eth0,把最后一个改为eth0,记录下mac地址. vi / ...

  7. 121. Best Time to Buy and Sell Stock买卖股票12

    一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...

  8. Boolean.getBoolean用法

    相信很多人被Boolean.getBoolean(String name)欺骗过,想当然的认为它是将"true"或者"false"转换为Boolean类型的AP ...

  9. php调用C#写的dll包

    1.转到需要注册的dll路径下 2.输入regasm命令+文件名 3问题解决

  10. window下装redis扩展(以php5.5为例)

    一.安装redis服务 1.下载并解压 https://github.com/dmajkic/redis/downloads 2.运行redis服务 3.检查能否正常访问  二.安装redis扩展 1 ...