Question link; click the open link
There is a small river with m length. It can be seen as a one-dimensional axis. There are n stones in the river, and there is a small frog that can jump l meters each time, add a stone at will to ensure that the frog can jump from the beginning to the end, and ask the frog how many times it will take to use the best strategy to jump to the other side.
Idea: assume that each stone of a frog goes through a step to represent the step of the last hop of the frog. The distance between the current point and the next point is compared with that of L, if it is small, it proves that the frog can jump to this one time, update the step and the position of the frog, and the CNT remains unchanged. If it is larger, it indicates that the frog must jump at least once. If lenth <= l, skip directly. Update step = lenth, CNT ++. If lenth> L, the frog will jump through the cycle of L + 1-step, step. This is easy to prove to ensure optimal solution, then it is equivalent to translating the stone backward into x * (L + 1) units. And so on. For more information, see the code ~ WAF is crying during the competition !!!
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 lenth=data[i]-now;if(lenth==0) continue;if(lenth+step<=l){now=data[i];step=lenth+step;}else if(lenth<=l){now=data[i];step=lenth;cnt++;}else if(lenth>l){int tp=lenth%(l+1);cnt+=(lenth/(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;}
HDU 5037 frog greedy 2014 Beijing Network Competition F