HDU 3455 Leap Frog(线性DP)
2014-10-02 23:15
197 查看
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
Sample Output
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 [i]<<endl; ans=min(ans,dp [i]); // } if(ans<INF) printf("%d\n",ans); else printf("-1\n"); } return 0; }
相关文章推荐
- HDU 3455 Leap Frog(线性DP)
- HDU 3455 Leap Frog (线性dp)
- hdu 1087 Super Jumping! Jumping! Jumping! (最大 上升子序列 线性 dp)
- hdu 1244 Max Sum Plus Plus Plus(DP线性区间)
- HDU 1231 最大连续子序列 (线性dp)
- HDU 2845 Beans (两次线性dp)
- HDU 2577 How to Type (线性dp)
- hdu 3455 Leap Frog
- hdu 1080 Human Gene Functions 线性dp
- HDU 5074 Hatsune Miku (线性dp)
- HDU 1087 Super Jumping! Jumping! Jumping! (线性dp 最大上升子序列)
- hdu 1227 Fast Food(DP线性决策)
- HDU 4293 Groups (线性dp)
- HDU 4055 Number String (线性dp)
- HDU 5185 Equation (线性dp 完全背包)
- hdu 1244 Max Sum Plus Plus Plus(DP线性区间)
- HDU 4433 locker (线性dp)
- HDU 4293 Groups (线性dp)
- HDU 1506 Largest Rectangle in a Histogram (线性dp)
- HDU 1421 搬寝室 (线性dp 贪心预处理)