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. Asp.Net文件夹没有读写权限的特殊问题

    如果asp.net网站权限都配置正确,但是仍然出现某一个文件或者文件夹没有读写权限时 原来只用在Web.config里面的  <system.web>节点下增加<identity i ...

  2. Kali Linux安装Remmina无法加载RDP插件

    原因是确少匹配的 libfreerdp库 可以到这里下载 http://ftp.de.debian.org/debian/pool/main/f/freerdp/ 我的电脑是64位的  我下载的是ht ...

  3. SpringBoot 跨域 Access-Control-Allow-Origin 问题

    https://blog.csdn.net/taoism_jerry/article/details/79695336 **************************************** ...

  4. javascript检测浏览器的缩放状态实现代码 是指浏览器网页内容的百分比缩放(按Ctrl和+号键或者-号键的缩放)

    这里所说的缩放不是指浏览器大小的缩放,而是指浏览器网页内容的百分比缩放(按Ctrl和+号键或者-号键的缩放).检测这种缩放有很种方法,QQ空间都通过flash来检测浏览器是否处于缩放.这里提供java ...

  5. C#如何删除数组中的一个元素

    C#如何删除数组中的一个元素,剩余的元素组成新数组,数组名不变double[] arr = new double[n];需要删除的是第m+1个数据arr[m]求新数组arr.(新数组arr包含n-1个 ...

  6. 面试总结:QuickSort 解析

    Quick Sort http://en.wikipedia.org/wiki/Quicksort Quicksort, or partition-exchange sort, is a sortin ...

  7. LVS负载均衡模型及算法概述

    集群类型 LB: Load Balancing,负载均衡 HA:High Availability, 高可用 HP:High Performance, 高性能   负载均衡 负载均衡设备 Hardwa ...

  8. 这到底是什么bug?---已结贴

    问题描述:全局变量,会被莫名其妙更改!打印为50,后面做比较的时候这个值为0了. 第一,我肯定没有犯低级错误,没有其他的更改,搜索全部代码,没有发现这个变量因为我程序问题导致不符合预期,同时找了两位同 ...

  9. <第一次买基金就赚钱>读书笔记

    基金,是指专门用于某种特定目的的并进行独立核算的资金 基金的开放日指基金契约规定的投资者可以在销售网点办理基金申购.赎回交易业务的日期 基金资产总值是指一个基金所拥有的资产(包括现金.股票.债券和其他 ...

  10. virtualbox ubuntu 虚拟画面卡顿问题

    要在虚拟机全局配置里面添加选项: