HDU 5037 Frog (week2--XDUACM)

Frog

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


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

题意:一只青蛙想过河,河中开始有N个石头,小青蛙每次最多只能跳L个单位,问到终点M需要最多的步数

青蛙一定是想最少步数到达终点的,但是我们需要给河中放石头使青蛙到达终点的步数是最大的

既然青蛙每次尽量跳的远,距离最大为L,那么两个石头距离<=L的,一定一次就跳过了。

而L+1的距离刚好挑不到,把L+1的距离分为两步来跳,对我们来说是最划算的。

既然如此,我们如何添加石头呢?

可以发现,如果青蛙可以跳到的范围内本身就有石头,它一定会跳的最远的那个,此时我们在该点之前之后添加点都没意义(要么没用,要么便宜它了)

如果没有可以跳到的,那么我们就得添加石头,青蛙必定会跳到这个点,我们的目的是要使它尽量的靠右。

这个点的位置和青蛙现在所在位置以及上一个石头的位置有关,即要让青蛙从上次位置刚好跳不到。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
int a[200005];
using namespace std;
int main(){
    int T,n,m,l,cas=0;
    //freopen("in.in","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&l);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        a[0]=0,a[++n]=m;
        sort(a,a+n);
        int ans_1,ans_2,step=0,last=l;
        for(int i=1;i<=n;i++){
            ans_1=(a[i]-a[i-1])/(l+1);
            ans_2=(a[i]-a[i-1])%(l+1);
            step+=ans_1*2;
            if(ans_2+last<l+1){
                last+=ans_2;
                
            }else{
                step++;
                last=ans_2;
            }
        }
        printf("Case #%d: %d\n",++cas,step);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/coderdogg/article/details/80355320