题目链接:https://vjudge.net/problem/HDU-5037
转自:https://blog.csdn.net/hyczms/article/details/44230083
题意:有一条小河长为M的小河。能够看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙。任意加入石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多须要多少次。、
思路:首先,每一步之间看看有多少l+1,因为l+1的距离最多需要2步。在分情况讨论。情况1:上一步跳的步数last+这一步跳的余数>=l+1,这时候应该多跳一步。情况2:上一步跳的步数last+这一步跳的余数<l+1,也就是说上一步跳的余数可以跟这一步跳的余数合并为一步。这时候就不需要多跳一步。
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int n,m,l;
int num[maxn];
int main()
{
int T,kase=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&l);
for(int i=1; i<=n; i++)
{
scanf("%d",&num[i]);
}
num[0]=0;
num[++n]=m;
int ans=0;
int last=l;
sort(num,num+n);
for(int i=1; i<=n; i++)
{
int x=(num[i]-num[i-1])%(l+1);
int y=(num[i]-num[i-1])/(l+1);
if(last+x>=l+1)
{
last=x;
ans+=2*y+1;
}
else if(last+x<l+1)
{
last=last+x;
ans+=2*y;
}
}
printf("Case #%d: %d\n",++kase,ans);
}
return 0;
}