HDU-4004 the frog's games second answer

Source: Internet
Author: User

The definition of this question is that a frog wants to cross the river. This one has a width, and there are several stones in the middle of the river. The frog asks to jump to the other side of the river in step m, q: Under what circumstances can a frog jump to the other side of the river, in addition, the maximum number of hops in this solution is the least (meaning that the frog's jump capability is the lowest ).

We use a binary answer to solve this problem. Its value may be in the range of 0 to the width of the river L. For a value in this range, we know the distribution of stones to determine whether this scheme is optimal, that is, to determine the maximum distance of the jump, so that the frog can jump as many stones as possible. Then, determine whether or not the specified number of steps has jumped to the other side.

The Code is as follows:

# Include <cstring>
# Include <cstdlib>
# Include <cstdio>
# Define maxn500055
Using namespace STD;

Int L, n, m, s [maxn];

Inline int CMP (const void * a, const void * B)
{
Return * (int *) A-* (int *) B;
}

Int judge (INT dis)
{// X indicates the jump x distance jump. By default, the first jump has been to X.
Int step = m, x = 0, y = 1, cur = 0;
While (step --)
{
While (DIS> = s [y]-s [x])
{
++ Y;
If (Y-1 = n + 1) // If the coordinates have jumped to the other side
Break;
}
Cur = s [Y-1];
X = Y-1;
If (cur> = L)
Break;
}
If (cur <L) // if not
Return 0;
Else
Return 1;
}

Int bsearch (int l, int R)
{
Int mid;
While (r> = L)
{
Mid = (L + r)> 1;
If (! Judge (MID ))
L = Mid + 1;
Else // The arrival will also be considered as not saturated
R = mid-1;
}
Return R + 1;
}

Int main ()
{
While (scanf ("% d", & L, & N, & M) = 3)
{
S [0] = 0;
For (INT I = 1; I <= N; ++ I)
{
Scanf ("% d", S + I );
}
S [n + 1] = L;
Qsort (S, N + 2, sizeof (INT), CMP );
Printf ("% d \ n", bsearch (0, L ));
}
Return 0;
}

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.