HDU 5037 (Frog-Greedy Frog jump stone), hdu5037

Source: Internet
Author: User

HDU 5037 (Frog-Greedy Frog jump stone), hdu5037

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 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 ;}














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.