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");
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值