hdu 5037 Frog(2014年北京区域赛网赛F题1006)

12 篇文章 0 订阅

Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
  
  
Once upon a time, there is a little frog called Matt. One day, he came to a river. The river could be considered as an axis.Matt is standing on the left bank now (at position 0). He wants to cross the river, reach the right bank (at position M). But Matt could only jump for at most L units, for example from 0 to L. As the God of Nature, you must save this poor frog.There are N rocks lying in the river initially. The size of the rock is negligible. So it can be indicated by a point in the axis. Matt can jump to or from a rock as well as the bank. You don't want to make the things that easy. So you will put some new rocks into the river such that Matt could jump over the river in maximal steps.And you don't care the number of rocks you add since you are the God. Note that Matt is so clever that he always choose the optimal way after you put down all the rocks.
 

Input
  
  
The first line contains only one integer T, which indicates the number of test cases. For each test case, the first line contains N, M, L (0<=N<=2*10^5,1<=M<=10^9, 1<=L<=10^9). And in the following N lines, each line contains one integer within (0, M) indicating the position of rock.
 

Output
  
  
For each test case, just output one line “Case #x: y", where x is the case number (starting from 1) and y is the maximal number of steps Matt should jump.
 

Sample Input
  
  
2 1 10 5 5 2 10 3 3 6
 

Sample Output
  
  
Case #1: 2 Case #2: 4
因为Matt非常聪明,所以,当一开始Matt就可以跳到对岸时,上帝不可能害到Matt。只有出现Matt无法跨越相邻两块石头,需要上帝帮忙时,上帝才可以使坏。那么上帝如何放石头,Matt走的步数才会最多呢?

Matt最多能跳l单位,那每l+1单位都让他跳两步,就是最坏的了。假设第i-1块石头的位置为a[i-1], 第i块位置为a[i],a[i] - a[i-1] > l。则上帝肯定会在a[i - 1] + l + 1的地方放一块石头,再在它前面放一块,让他跳两次。本来前面一块石头的位置可以随意放的,不过考虑到如果与step[i-1]-1步最远石头位置距离<=l的话,则Matt可以先从step[i-1]-1步最远石头位置跳到中间石头,再跳到a[i-1] + l + 1,此时跳到a[i-1] + l + 1时相当于a[i-1]的步数加一,这种情况肯定不是上帝希望的。所以前面一块石头的位置是step[i-1]-1步最远石头位置 + l + 1,这样既能让他跳两步,又是最小的下标。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn = 200005;
int a[maxn];
int dp[maxn];

int main()
{
    int test, test_case = 1, n, m, l;
    scanf("%d", &test);
    while(test--)
    {
        scanf("%d%d%d", &n, &m, &l);
        a[0] = 0, a[n+1] = m;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &a[i]);
        }
        sort(a, a+n+2);
        memset(dp, 0, sizeof(dp));
        int step = 0, maxlast = 0;
        for (int i = 1; i <= n + 1; ++i)
        {
            if (a[i] - a[i - 1] <= l) //如果两石头间距离小于或等于l,则与上帝无关
            {
                if (maxlast + l >= a[i])
                {
                    dp[i] = step + 1;
                }
                else
                {
                    maxlast = a[i - 1];
                    step = dp[i - 1];
                    dp[i] = step + 1;
                }
            }
            else
            {//否则,上帝不断的交替增加maxlast+l+1,a[i-1]+l+1的石头
                //由maxlast增加的最接近a[i]的坐标
                int ii = maxlast + (a[i] - maxlast) / (l + 1) * (l + 1);
                int ii_step = step + (a[i] - maxlast) / (l + 1) * 2;
                //由a[i-1]增加的最接近a[i]的坐标
                int jj = a[i - 1] + (a[i] - a[i - 1]) / (l + 1) * (l + 1);
                int jj_step = dp[i - 1] + (a[i] - a[i - 1]) / (l + 1) * 2;
                //if ((jj > ii && ii + l >= a[i]) || (jj < ii && jj + l < a[i]))
                if (jj > ii)
                {//跳到ii后再跳到a[i]
                    if (a[i] != ii)
                    {
                        dp[i] = ii_step + 1;
                        maxlast = ii;
                        step = ii_step;
                    }
                    else
                    {
                        dp[i] = ii_step;
                        maxlast = jj;
                        step = jj_step;
                    }
                }
                else
                {
                    if (a[i] != jj)
                    {
                        dp[i] = jj_step + 1;
                        maxlast = jj;
                        step = jj_step;
                    }
                    else
                    {
                        dp[i] = jj_step;
                        maxlast = ii;
                        step = ii_step;
                    }
                }
            }
        }
        printf ("Case #%d: %d\n", test_case++, dp[n + 1]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值