题意:河宽为m,里面有n个石头,青蛙一次跳的长度是l,青蛙想跳过河去,上帝可以在河里面放石头,帮助青蛙过河,但是他要青蛙跳的次数最多。青蛙每次会选择最长的距离跳。问最多要跳多少次。注意这里石头只能放在整数点上
思路:贪心。在处理两个石头距离大于l的时候要注意。上一次跳跃的距离是step,那么布置下一个石头的地方一定是step+l+1,这样才能保证青蛙不会从上一次的地方直接跳到布置的这个石头上。再下一个石头的位置是布置的第一个石头的位置加上step,这样可以保证不会从原有的那个石头上直接跳过去。如此,每两个石头占据了l+1的距离,青蛙以l-step+1,step的周期跳跃。在布置完石头以后,剩余的一些距离也需要注意,从布置的倒数第二个石头跳到布置的最后一块石头会有step的距离,如果剩下的距离加上step比l小,那么布置的最后一个石头会没有用。
参考:http://blog.csdn.net/alpc_paul/article/details/39457193
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int rock[200005];
int main()
{
//freopen("data.txt","r",stdin);
int T;
scanf("%d",&T);
int kase=0;
while(T--)
{
printf("Case #%d: ",++kase);
int n,m,l;
scanf("%d%d%d",&n,&m,&l);
for(int i=1;i<=n;++i)
{
scanf("%d",&rock[i]);
}
rock[0]=0;
rock[n+1]=m;
sort(rock,rock+n+1);
int step=l;
int pos=0;
int ans=0;
while(rock[pos]<m)
{
int dis=rock[pos+1]-rock[pos];
if(step+dis<=l)
{
pos++;
step+=dis;
}
else{
if(dis<=l)
{
pos++;
step=dis;
ans++;
}
else
{
int x=dis/(l+1);
int y=dis%(l+1);
ans+=x*2;
dis=dis-x*(l+1);
if(step+dis>l)
{
step=dis;
ans++;
pos++;
}
else
{
step+=dis;
pos++;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}