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. 【转】(八)unity4.6Ugui中文教程文档-------概要-UGUI Rich Text

    原创至上,移步请戳:(八)unity4.6Ugui中文教程文档-------概要-UGUI Rich Text 7.Rich Text UI元素和文本网格的文本可以合并多个字体样式和大小.对 UI系统 ...

  2. ubuntu 中DNAT SNAT配置实验.

    1.      目的 图1 如图1所示,有A,B两台计算机,其中A配置成普通PC,B是网关.实现由A向一个不存在的IP 发起tcp连接,并能向这个不存在的ip发送数据. 同时响应这个tcp连接的是B中 ...

  3. Python爬取猫眼top100排行榜数据【含多线程】

    # -*- coding: utf-8 -*- import requests from multiprocessing import Pool from requests.exceptions im ...

  4. 【Math】证明:实对称阵属于不同特征值的的特征向量是正交的

    证明:实对称阵属于不同特征值的的特征向量是正交的. 设Ap=mp,Aq=nq,其中A是实对称矩阵,m,n为其不同的特征值,p,q分别为其对应得特征向量. 则 p1(Aq)=p1(nq)=np1q (p ...

  5. NewStyleClass学习笔记[一]

    from : https://www.python.org/doc/newstyle/ New-style Classes Unfortunately(遗憾,不幸的), new-style class ...

  6. Node.js学习笔记(4)--简单路由,老师学生id

    说明(2017-5-2 17:57:23): 1. foo.js var http = require("http"); var url = require("url&q ...

  7. 4. 文本相似度计算-CNN-DSSM算法

    1. 文本相似度计算-文本向量化 2. 文本相似度计算-距离的度量 3. 文本相似度计算-DSSM算法 4. 文本相似度计算-CNN-DSSM算法 1. 前言 之前介绍了DSSM算法,它主要是用了DN ...

  8. hashCode()方法与equals()方法的说明

    1,一般我们自己用时,只使用equals()方法,用于判断两个对象是否是业务上等价的.2,在重写equals()方法时,强烈推荐也要重写hashCode()方法,因为有的集合用到了hashCode() ...

  9. http连接优化与浏览器允许的并发请求资源数相关资料(整理转载)

    网页性能优化相关资料: https://developer.yahoo.com/performance/rules.html#page-nav 前端技术的逐渐成熟,还衍生了domain hash, c ...

  10. Hadoop运行单词统计

    1.创建input文件夹 hadoop fs -mkdir input 2.上传文件到hadoop hadoop fs -put /root/data/output.txt input 3.运行wor ...