HDU 3455 Leap Frog(线性DP)
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.
xn<= 1000000. The end-of-fi le is denoted by a single line containing "0".
6
3 5 9 12 15 17
6
3 5 9 12 30 40
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)的更多相关文章
- 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 ...
- HDU 1421 搬寝室 (线性dp 贪心预处理)
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 1069 Monkey and Banana(线性DP)
Description A group of researchers are designing an experiment to test the IQ of a monkey. They wi ...
- 动态规划——线性dp
我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...
- 动态规划_线性dp
https://www.cnblogs.com/31415926535x/p/10415694.html 线性dp是很基础的一种动态规划,,经典题和他的变种有很多,比如两个串的LCS,LIS,最大子序 ...
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- 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门 ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
随机推荐
- HTTP 代理服务器技术选型之旅
HTTP 代理服务器技术选型之旅 背景 长期以来,贴吧开发人员多,业务耦合大,需求变化频繁,因此容易产生 bug.而我所负责的广告相关业务,和 UI 密切相关,一旦因为某种原因(甚至是被别人改了代码) ...
- NGUI的UISprite动态染色的一种方法
本文主要参考iwinterice 的 NGUI的UISprite动态染色的一种解决方案 文章. 参考参考,就是既参详又拷贝,-,-||| 相关理论依据,还请去移步 NGUI的UISprite动态染色的 ...
- Asp.Net时间方法大全
DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays(- Convert.ToInt32(dt.DayOfWeek.To ...
- Eclipse - Mac Os Default JRE missing
转:http://stackoverflow.com/questions/1736993/eclipse-mac-os-default-jre-missing 1) Follow Joshua's a ...
- 对Inductive Bias(归纳偏置)的理解
参考资料: https://en.wikipedia.org/wiki/Inductive_bias http://blog.sina.com.cn/s/blog_616684a90100emkd.h ...
- S3C2440的七种模式之——未定义模式(去掉bl print1 bug解决)
现在做第一个实验,模拟未定义模式. 未定义模式,是cpu遇到自己不能识别的指令时候做出的异常处理. arm指令的机器码一定是按照某种规范要求的,不然你随意写一条指令,cpu不是都可以执行吗?在cpu没 ...
- php 找出异常发生的地方
多层嵌套发生异常,找到异常最早发生的地方. final public int Exception::getLine ( void ) final public string Exception::ge ...
- Website Develop: Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list
1. install all features in IIS 2. Try the following steps to register it. run %windir%\Microsoft.NET ...
- Android Studio OkHttpClient使用
本次来记录下OkHttpClient的使用,OkHttpClient是用来完成android 客户端对服务端请求的工具. 首先记住,使用网络的时候一定要加入权限,加入到AndroidMainfest. ...
- spring data jpa的update操作
简介 使用jpa进行update操作主要有两种方式: 1.调用保存实体的方法 1)保存一个实体:repository.save(T entity) 2)保存多个实体:repository.save(I ...