題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5037
題意:青蛙跳石頭,上帝放最少石頭數情況下,青蛙跳最少的步數。
AC代碼:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int st[200005];
int main(){
int T,n,m,l,t=1,i,ans,now,s,g;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&l);
for(i=1;i<=n;i++)
scanf("%d",&st[i]);
printf("Case #%d: ",t++);
st[++n]=m;
ans=0;
now=l;
sort(st+1,st+n+1);
for(i=1;i<=n;i++){
s=(st[i]-st[i-1])%(l+1);
g=(st[i]-st[i-1])/(l+1);
if(now+s>l){
now=s;
ans+=g*2+1;
}
else{
now+=s;
ans+=g*2;
}
}
printf("%d\n",ans);
}
return 0;
}