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
用DP[i][j]表示:第一个人到了I点距离第二个人j的最小步数。
dp[i][j]=min{dp[j][k]+1}.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int INF=0x3f3f3f;
const int maxn=1e5+100;
int dp[maxn][15];
int hash[10*maxn];
int num[maxn],n;
int main()
{
while(~scanf("%d",&n)&&n)
{
memset(dp,INF,sizeof(dp));
memset(hash,-1,sizeof(hash));
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
hash[num[i]]=i;
}
dp[2][num[2]-num[1]]=0;
for(int i=2;i<=n;i++)
{
for(int j=1;j<=10;j++)
{
if(j>num[i]) break;
for(int k=j+1;k<=10;k++)
{
int tt=num[i]-j;
if(hash[tt]>=0)
dp[i][j]=min(dp[hash[tt]][k-j]+1,dp[i][j]);//距离小于10的前面点中递推
}
}
}
int ans=INF;
for(int i=1;i<=10;i++)
// {
// cout<<"1111 "<<dp[n][i]<<endl;
ans=min(ans,dp[n][i]);
// }
if(ans<INF) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}

 

HDU 3455 Leap Frog(线性DP)的更多相关文章

  1. HDU 3455 Leap Frog 2016-09-12 16:34 43人阅读 评论(0) 收藏

    Leap Frog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. HDU 1421 搬寝室 (线性dp 贪心预处理)

    搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. HDU 1069 Monkey and Banana(线性DP)

    Description   A group of researchers are designing an experiment to test the IQ of a monkey. They wi ...

  4. 动态规划——线性dp

    我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...

  5. 动态规划_线性dp

    https://www.cnblogs.com/31415926535x/p/10415694.html 线性dp是很基础的一种动态规划,,经典题和他的变种有很多,比如两个串的LCS,LIS,最大子序 ...

  6. LightOJ1044 Palindrome Partitioning(区间DP+线性DP)

    问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...

  7. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  8. hdu1712 线性dp

    //Accepted 400 KB 109 ms //dp线性 //dp[i][j]=max(dp[i-1][k]+a[i][j-k]) //在前i门课上花j天得到的最大分数,等于max(在前i-1门 ...

  9. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

随机推荐

  1. java向mysql插入数据乱码

    修改jdbc的链接,将原来的         jdbc:mysql://localhost:3306/demo改为        jdbc:mysql://localhost:3306/demo?us ...

  2. cf 366C C. Dima and Salad(01背包)

    http://codeforces.com/contest/366/problem/C 题意:给出n个水果的两种属性a属性和b属性,然后挑选苹果,选择的苹果必须要满足这样一个条件:,现在给出n,k,要 ...

  3. 2. K-Means的优化

    1. K-Means原理解析 2. K-Means的优化 3. sklearn的K-Means的使用 4. K-Means和K-Means++实现 1. 前言 上一篇博文K-Means原理解析简单清晰 ...

  4. 对象序列化为何要定义serialVersionUID的来龙去脉

    在很多应用中,需要对某些对象进行序列化,让它们离开内存空间,入住物理硬盘,以便长期保存.比如最常见的是Web服务器中的Session对象,当有10万用户并发访问,就有可能出现10万个Session对象 ...

  5. matplotlib-plot-style

    style 1.绘制x=1 2.不同线宽 enumerate(Widths) 3.线型(实线,虚线,点划线) linestyle set_dashes 4.自动设置线颜色 5.点的显示形式 marke ...

  6. call_user_func_array 应用场景分析

    1. 场景一 a.你要调用的函数名是未知的 b.要调用函数的参数类型及个数也是未知的 定时任务类需要定时运行一个函数,则接口就可以类似这样设计: /** * 在$run_time时刻运行$call_b ...

  7. <漫步华尔街——股市历久弥新的成功投资策略>读书笔记

    书在这里 随机游走是指基于过去的表现,无法预测将来的发展步骤和方向. 仅仅为了达到盈亏相抵点,你的投资回报率至少也要等于通货膨胀率 磐石理论认为,无论是普通股票还是不动产,每一种投资工具都具有被称为“ ...

  8. C++中 explicit的用法

    在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. #include <iostream> using ...

  9. sam9260 adc 头文件

    /* * driver/char/at91_adc.h * * Copyright (C) 2007 Embedall Technology Co., Ltd. * * Analog-to-digit ...

  10. hive表增量抽取到oracle数据库的通用程序(二)

    hive表增量抽取到oracle数据库的通用程序(一) 前一篇介绍了java程序的如何编写.使用以及引用到的依赖包.这篇接着上一篇来介绍如何在oozie中使用该java程序. 在我的业务中,分为两段: ...