首页 > 代码库 > HDOJ 5037 Frog

HDOJ 5037 Frog


Frog

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


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
 

Source
2014 ACM/ICPC Asia Regional Beijing Online
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n,m,l;
int stone[200200];

int main()
{
    int T_T,cas=1;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%d%d%d",&n,&m,&l);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",stone+i);
        }
        n++;
        stone[0]=0; stone[n]=m;
        n++;
        sort(stone,stone+n);
        int k=l,ans=0;
        for(int i=1;i<n;i++)
        {
            int x=(stone[i]-stone[i-1])%(l+1);
            int y=(stone[i]-stone[i-1])/(l+1);
            if(k+x>=l+1)
            {
                k=x;
                ans+=y*2+1;
            }
            else
            {
                k=x+k;
                ans+=y*2;
            }
        }
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}



HDOJ 5037 Frog