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 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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3455

题目大意:n个网站在一条线上,位置为xi。两仅仅青蛙。第一仅仅開始在第一个网站,第二仅仅開始在第二个网站。它们每次最多跳10个单位长度,且每次跳必须超过彼此(不能落在同一个网站上),求两蛙随意一个到达目的地即最后一个网站时跳跃的总次数的最小值

题目分析:这题做的特别纠结,dp[i][0]。dp[i][1]。分别表示两青蛙跳i次的最远距离,跳的时候要做下面推断:
1.当前青蛙能超过前面的青蛙
2.超过后能落到某个网站(注意这里还要注意超过终点的情况)
3.超过后该青蛙的后面一仅仅青蛙也可以超过它且落在某个网站上
满足以上条件的情况下取最大值,可知若第一仅仅先到且跳了i次。总次数为2*i-1,若第二仅仅则为2*i


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 100005;
int dp[MAX][2], x[MAX];
bool has[MAX * 10];

int main()
{
    int n;
    while(scanf("%d", &n) != EOF && n)
    {
        int ans = -1;
        bool flag = false;
        memset(has, false, sizeof(has));
        memset(dp, 0, sizeof(dp));
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &x[i]);
            has[x[i]] = true;
        }
        if(n == 2)
        {
            printf("0\n");
            continue;
        }
        dp[0][0] = x[0];
        dp[0][1] = x[1];
        for(int i = 1; i < n; i++)
        {
            for(int j = 2; j <= 10; j++)
            {
                int d0 = dp[i - 1][0] + j;
                if((has[d0] || d0 >= x[n - 1]) && d0 > dp[i - 1][1])
                {
                    for(int k = 2; k <= 10; k++)
                    {
                        if(dp[i - 1][1] + k > d0 && (has[dp[i - 1][1] + k] || dp[i - 1][1] + k >= x[n - 1]))
                        {
                            dp[i][0] = max(dp[i][0], d0);
                            break;
                        }
                    }
                }
            }
             for(int j = 2; j <= 10; j++)
            {
                int d1 = dp[i - 1][1] + j;
                if((has[d1] || d1 >= x[n - 1]) && d1 > dp[i][0])
                {
                    for(int k = 2; k <= 10; k++)
                    {
                        if(dp[i][0] + k > d1 && (has[dp[i][0] + k] || dp[i][0] + k >= x[n - 1])) 
                        {
                            dp[i][1] = max(dp[i][1], d1);
                            break;
                        }
                    }
                }

            }
        }
        for(int i = 1; i < n; i++)
        {
            if(dp[i][0] >= x[n - 1])
            {
                flag = true;
                printf("%d\n", 2 * i - 1);
                break;
            }
            if(dp[i][1] >= x[n - 1])
            {
                flag = true;
                printf("%d\n", 2 * i);
                break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
}


posted @ 2017-08-14 13:36  jzdwajue  阅读(155)  评论(0编辑  收藏  举报