hdu 3455 Leap Frog

Leap Frog

                                                            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768
K (Java/Others)

Total Submission(s): 623    Accepted Submission(s): 219

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

Source

2009 Stanford Local ACM Programming Contest

  题意:

       给定一个从小到大的正整数序列表示位置,刚开始A在序列中第一个元素位置,B在序列中第二个元素位置,接下来A要跳到B的后面的位置,且跳跃的距离不能超过10,求其中一人到达最后一个位置时,两人跳跃的总次数最少;

  题解:

       首先想到的是普通的DP,dp[i][j]表示一个人在第i个位置,另一个人在第j个位置所需要的最少跳跃次数。但是n<=100000,这样内存会超,题目中说跳跃距离不超过10,也就是说第j个位置只有前面10个是有用的,于是dp[i][j]的i可以表示一个人在第j位置,另一个在第j-i个位置。

       i为当前要到达的第i个位置,j是前一个人在第i-j个位置,k是后一个人在i-j-k个位置,现在从第i-j-k跳到i位置,所以dp[j][i]=min(dp[j][i],dp[k][i-j]+1),dp[k][i-j]是状态为一个在第i-j个位置,一个在i-j-k个位置时的最小跳跃次数。

  代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
const int inf=199999999;
int dp[12][maxn];
int a[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        memset(dp,-1,sizeof(dp));
        dp[1][2]=0;
        for(int i=3;i<=n;i++)
        {
            for(int j=1;j<=10;j++)
            {
                if(i-j>=1&&a[i]-a[i-j]<=10)
                {
                    for(int k=1;k<=10;k++)
                    {
                        if(i-j-k>=1&&a[i]-a[i-j-k]<=10)
                        {
                            if(dp[k][i-j]!=-1)//判断是否存在一个在第i-j个位置,一个在i-j-k个位置
                            {
                                if(dp[j][i]==-1)
                                {
                                    dp[j][i]=dp[k][i-j]+1;
                                }
                                else
                                {
                                    dp[j][i]=min(dp[j][i],dp[k][i-j]+1);
                                }
                            }
                        }
                    }
                }
                else
                break;
            }
        }
        int ans=inf;
        for(int i=1;i<=10;i++)
        if(dp[i][n]!=-1)
        ans=min(ans,dp[i][n]);
        if(ans==inf)
        printf("-1\n");
        else
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-07 07:14:15

hdu 3455 Leap Frog的相关文章

HDU 3455 Leap Frog (线性dp)

Leap Frog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 638    Accepted Submission(s): 224 Problem Description Jack and Jill play a game called "Leap Frog" in which they alternate turns

HDU 3455 Leap Frog(线性DP)

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 posit

hdu 3455 Leap Frog(状压DP)

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 posit

HDU 4004 The Frog&#39;s Games(基本算法-贪心,搜索-二分)

The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The

HDU 4004 The Frog&#39;s Games 二分 贪心

戳这里:HDU 4004 //思路:二分经典入门题...贪心判方案是否可行 1 #include "bits/stdc++.h" 2 using namespace std; 3 int L, n, m; 4 int pos[500010], dis[500010]; 5 6 bool Cant(int Dis_Jump) 7 { 8 int i, Dis_Sum = 0, Count = 0; 9 for(i = 1; i <= n + 1; ++i) { 10 if(dis[

hdu 4004 The Frog&#39;s Games【二分】

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3980    Accepted Submission(s): 1931 Problem Description The annual Games in frogs' kingdom started again. The most famous game

HDU 4004 The Frog&#39;s Games 二分

1.题意:一条河长为L,河上有N块石头,一只青蛙可以利用这些石头从河的一端跳到对岸,可以跳不超过M次,求各种跳法中,找到最小化的最大步长.输入第一行依次给出L.N.M,第二行依次给出N块石头距离起点的距离. 2.分析:这类最小化最大值的问题用二分来求解最高效.先预先处理,连同起点,终点,共N+2个点排序,两两相减得到N+1段距离C[i],即跨一步的最小距离.二分维护mid值,上界为河长L,下界为两两距离之中的最大值maxC[i](反证易知),以mid值分割C[i]数组,分割的段数<=M则mid偏

hdu 4004 The Frog&#39;s Games 【二分】

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4565    Accepted Submission(s): 2225 Problem Description The annual Games in frogs' kingdom started again. The most famous game

hdu 4004 The Frog&#39;s Games

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over