Test instructions more difficult to understand, a frog across the river, it at most one jump l meters, now there are stones in the river, distance, God can add stones, frogs are very smart, it will choose the least number of hops to the path. Ask how to add a stone to make the frog the most number of times. Output the maximum number of times a frog jumps.
Considering the length of the l+1, God will make the frog jump two times. So just construct the l+1 as much as you can. Then you need to ask how many l+1 on the line. There is a need to record the distance of the previous jump, if the distance of the previous jump plus this distance is less than l+1, then the last time will be jumping to the current point, instead of jumping to the last point, so update the last distance. This is also the case for both of these:
1) Previous step pre plus this step remainder y is greater than L, then the last remaining part needs to be skipped separately;
2) The previous step pre plus this step remainder y is less than or equal to L, the last remaining part can be up one step, namely pre+y.
The code is as follows:
#include <cstdio>#include<algorithm>using namespacestd;Const intMAXN =2* 1e5 +Ten;intA[MAXN];intMain () {intT, N, M, L, Kase =0; scanf ("%d", &T); while(t--) {scanf (" %d%d%d", &n, &m, &m); for(inti =1; I <= N; i++) scanf ("%d", &A[i]); a[0] =0; A[++n] =m; Sort (A, a+N); intAns =0, pre =L; for(inti =1; I <= N; i++) { intx = (A[i]-a[i-1])/(L +1); inty = (A[i]-a[i-1])% (L +1); if(y + pre >= L +1) {Pre=y; Ans+=2* x +1; } Else{Pre= Pre +y; Ans+=2*x; }} printf ("Case #%d:%d\n", ++Kase, ans); } return 0;}
HDU 5037 Frog (greedy)