HDU 5037 (frog-Greedy frog jump stone)

Source: Internet
Author: User

Frog Time Limit: 3000/1500 MS (Java/others) memory limit: 262144/262144 K (Java/Others)
Total submission (s): 1596 accepted submission (s): 442


Problem descriptiononce upon a time, there is a little frog called Matt. One day, he came to a river.

The river cocould be considered as an axis. matt is standing on the Left Bank now (at position 0 ). he wants to cross the river, reach the right bank (at position m ). but Matt cocould only jump for at most l units, for example from 0 to L.

As the god of nature, you must save this poor frog. there are n rocks lying in the river initially. the size of the rock is negligible. so it can be indicated by a point in the axis. matt can jump to or from a rock as well as the bank.

You don't want to make the things that easy. so you will put some new rocks into the river such that Matt cocould jump over the river in maximal steps. and you don't care about the number of rocks you add since you are the God.

Note that Matt is so clever that he always choose the optimal way after you put down all the rocks.
Inputthe first line contains only one integer T, which indicates the number of test cases.

For each test case, the first line contains N, M, L (0 <= n <= 2*10 ^ 5, 1 <= m <= 10 ^ 9, 1 <= L <= 10 ^ 9 ).

And in the following n lines, each line contains one integer within (0, m) indicating the position of rock.
Outputfor each test case, just output one line "case # X: Y", where X is the case number (starting from 1) and Y is the maximal number of steps Matt shoshould jump.
Sample Input
21 10 552 10 336
 
Sample output
Case #1: 2Case #2: 4
 
Source2014 ACM/ICPC Asia Regional Beijing online
Recommendhujie | we have carefully selected several similar problems for you: 5057 5056 5055 5054

The frog crosses the river. There are n stones on the river. The frog wants to jump from to m. A maximum of L units of length can be jumped at a time. You can place any stones up, this allows frogs to jump most times when they adopt the optimal strategy. Q: the maximum number of times a frog jumps.


Apparently:

1. frogine is always the farthest stone.

2. When a frog can jump, the best stone is not added.

Consider that when frogs do not have stones, jump from 0 to T (L + 1) + x

Apparently, the frog jumped 2nd stones forward at least from it (L + 1)

Therefore, such a jump is not inferior.

0, 1, L + 1, (L + 1) + 1, 2 (L + 1) + 1 ,... T (L + 1), T (L + 1) + X. // The distance between Item X and item x + 2 is L + 1, which is optimal.

Then, when the frog jumps from pre (negative number) to 0 and then to T (L + 1) + X, the second rock starting with pre should be at a distance of> = L + 1.

0, pre + L + 1, L + 1, pre + 2 (L + 1), 2 (L + 1 )... T (L + 1), T (L + 1) + x


# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <functional> # include <iostream> # include <cmath> # include <cctype> # include <ctime> using namespace STD; # define for (I, n) for (INT I = 1; I <= N; I ++) # define fork (I, k, n) for (INT I = K; I <= N; I ++) # define rep (I, n) for (INT I = 0; I <n; I ++) # define Ford (I, n) for (INT I = N; I --) # define repd (I, n) for (INT I = N; I> = 0; I --) # define forp (x) for (INT P = pre [X]; P = Next [p]) # define lson (x <1) # define rson (x <1) + 1) # define MEM (a) memset (A, 0, sizeof (a); # define Memi (a) memset (A, 127, sizeof (a); # define Memi (a) memset (A, 128, sizeof (a); # define Inf (2139062143) # define F (100000007) # define maxn (2*100000 + 10) Long MUL (long a, long B) {return (A * B) % F;} long add (long a, long B) {return (a + B) % F ;} long long sub (long a, long B) {return (a-B + (A-B)/f * F + F) % F;} typedef long ll; int t, n, M, L, A [maxn]; bool AVI (int, int B) {return a + L> = B;} int main () {// freopen ("hdu5037.in", "r", stdin ); // freopen ("hdu5037.out", "W", stdout); scanf ("% d", & T); For (t, t) {scanf ("% d", & N, & M, & L); // total_rocks length max_stepfor (I, n) scanf ("% d ", & A [I]); sort (a + 1, A + 1 + n); A [+ + N] = m; A [n + 1] = inf; int now = 0, pre =-L, ANS = 0; // When pre =-L, now = 0, pre + L + 1 = now + 1 for (I, n) {if (a [I + 1] <= now + l) c Ontinue; // jump to the front if (a [I] <= now + l) {pre = now; now = A [I]; ans ++ ;} else {int t = (a [I]-Now)/(L + 1), x = (a [I]-Now) % (L + 1 ); pre + = T * (L + 1); ans + = 2 * t; If (! AVI (PRE, a [I]) {now + = T * (L + 1); if (I <n & AVI (now, A [I + 1]) continue; else pre = now, now = A [I], ANS ++;} else {int K = now-pre; now = pre; pre-= K; ans --; if (I <n & AVI (now, a [I + 1]) continue; else pre = now, now = A [I], ANS ++ ;}} printf ("case # % d: % d \ n", T, ANS);} return 0 ;}











HDU 5037 (frog-Greedy frog jump stone)

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.