HDU 5037 Frog(模擬跳石頭的過程)

題目鏈接: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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章