HDU 5037 Frog (greedy)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.