HDU 5037 FROG 贪心 2014北京网络赛F

来源:互联网 发布:汽车修理厂软件 编辑:程序博客网 时间:2024/04/27 00:17

题目链接;点击打开链接

题意:有一条小河长为M的小河,可以看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙,随意添加石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多需要多少次。

思路:不妨假设青蛙每个石头都要经过一次,用step表示青蛙上一次跳的步长,每跳一次对目前点到下一点的距离和step的和与L做比较,如果小与,证明青蛙可以一次跳到这,更新step和青蛙位置,cnt保持不变,若大于,证明青蛙至少需要再跳一次,若length<=l,则直接跳,更新step=length,cnt++;若length>l,则让青蛙以l+1-step,step,的周期跳,易证可以保证解最优,然后相当于把石头向后平移x*(l+1)个单位。and so on。详情请看代码,非常明了~比赛的时候手残wa到哭!!!

cpp:

#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>using namespace std;int data[200010],T,n,m,l;void slove(){sort(data,data+n+1);int step=l,now=0,cnt=0;for(int i=0;i<=n;i++){int length=data[i]-now;if(length==0) continue;if(length+step<=l){now=data[i];step=length+step;}else if(length<=l){now=data[i];step=length;cnt++;}else if(length>l){int tp=length%(l+1);cnt+=(length/(l+1))*2;if(tp+step<=l){step=tp+step;now=data[i];}else {step=tp;now=data[i];cnt++;}}}printf("%d\n",cnt);}int main(){//freopen("data.in","r",stdin);int cas=0;scanf("%d",&T);while (T--){printf("Case #%d: ",++cas);scanf("%d%d%d",&n,&m,&l);for(int i=0;i<n;i++){scanf("%d",data+i);}data[n]=m;slove();}return 0;}


1 0
原创粉丝点击