hdu 5037 Frog(2014年北京地区試合テニスF題1006)

5822 ワード

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が隣の2つの石を越えることができず、神の助けが必要なときだけ、神は破壊することができます.では神はどのように石を置いて、Mattが歩く歩数が一番多いのでしょうか.
Mattは最大l単位を跳ぶことができて、それではl+1単位ごとに彼に2歩跳ばせて、最悪です.i−1番目の石の位置をa[i−1],i番目の石の位置をa[i],a[i]−a[i−1]>lとする.神は必ずa[i-1]+l+1の場所に石を置いて、その前に1枚置いて、2回踊らせます.本来なら前の石の位置は自由に置けるが、step[i-1]-1歩と最も遠い石の位置距離<=lであれば、Mattはまずstep[i-1]-1歩で最も遠い石の位置から中間石にジャンプし、さらにa[i-1]+l+1にジャンプすることができ、このときa[i-1]+l+1にジャンプするとa[i-1]に相当する歩数に1を加えることができ、この場合は神の希望ではないに違いない.だから前の石の位置はstep[i-1]-1歩の最も遠い石の位置+l+1で、このように彼を2歩踊らせることができて、また最小の下標です.
#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
", test_case++, dp[n + 1]); } return 0; }