现在的位置: 首页 > 综合 > 正文

hdu 5037 Frog

2019年11月06日 ⁄ 综合 ⁄ 共 2354字 ⁄ 字号 评论关闭

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


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
 

有只青蛙踩石子过河,河宽m,有n个石子坐标已知。青蛙每次最多跳L。现在可以在河中再放一些石子,使得青蛙过河跳的次数最多。

青蛙的策略肯定是尽可能往远的跳。如果它现在在cur位置跳不动了,且它上一次所在位置为pre。那么God肯定要把新石子放在max(cur,pre+L)+1的位置。这样可以保证每次青蛙能前进的长度最短。

肯定不能暴力直接做,但可以发现没有石子着陆时青蛙都是每两步跳L+1的距离,就能利用周期优化暴力的过程。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int stone[int(2e5)+10];
int main()
{
    int T,n,m,l;
    scanf("%d",&T);
    for(int cs=1;cs<=T;cs++)
    {
        scanf("%d%d%d",&n,&m,&l);
        for(int i=0;i<n;i++)
            scanf("%d",stone+i);
        stone[n++]=m;
        sort(stone,stone+n);
        int ans=0,step=l,now=0;
        for(int i=0;i<n;i++)
        {
        	int dis=stone[i]-now;
        	if(dis+step<=l)//如果上次已经能够到达这块石头 
        		step+=dis;
        	else if(dis<=l)//如果当前能一步到达这块石头 
        	{
        		step=dis;
        		ans++;
        	}
        	else//如果至少要走两步到达这块石头 
        	{
        		int k=dis/(l+1);//算出走了几个周期 
        		ans+=2*k;//每个周期是两步 
        		int rm=dis%(l+1);//算出周期外还剩下多少距离 
        		if(rm+step<=l)//如果在最后一个周期内可以走到这块石头 
        			step+=rm;
        		else//否则,必然可以一步走到这块石头 
        		{
        			step=rm;
        			ans++;
        		}
        	}
        	now=stone[i];//此时走到了这块石头 
        }
        printf("Case #%d: %d\n",cs,ans);
    }
}

抱歉!评论已关闭.