Hdu5037 Frog (Greedy)

Source: Internet
Author: User
Tags cmath

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5037

Difficult questions about the Internet competition in Beijing

FrogTime Limit: 3000/1500 MS (Java/others) memory limit: 262144/262144 K (Java/Others)
Total submission (s): 99 accepted submission (s): 11


Problem description once 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.
Input the 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.
Output for 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 input21 10 552 10 336
Sample outputcase #1: 2 case #2: 4
Source 2014 ACM/ICPC Asia Regional Beijing online
Recommendhujie | we have carefully selected several similar problems for you: 5041 5040 5039 5038

Question:

A frog jumps from 0 to m on the number axis ~ L. The number axis is a water surface and can only jump to a stone. Given several existing stones, you can add them anywhere. The frog must be able to jump to the destination and have the largest number of hops.

Question:

Greedy and crazy.

First, sort the stones in order, and add a stone m to represent the ending point.

Set the current frog at now and the previous step at pre (initialize negative infinity ).

Jump on an existing stone first, and jump to the farthest one each time.

When you cannot continue to jump (the next stone is too far away), add stones.

Add stones to max (now + 1, pre + L + 1), that is, the closest one that pre can jump.

Repeat it. If you can jump to an existing Stone, you can jump to the existing stone. If you cannot jump, you can add a stone.

Greedy, so that you can get the correct answer!

However, it may jump too many times, but it will be TLE. So when the two stones are too far away, you can take special measures to create a lot of stones and jump many steps.

We can find that two consecutive steps of creating two stones are now + L + 1, so we can skip several (L + 1) steps directly.

 

(It's more difficult than frog, I'm afraid)

Code:

 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cstring> 6 #include<algorithm> 7 #include<cmath> 8 #include<map> 9 #include<set>10 #include<stack>11 #include<queue>12 using namespace std;13 #define ll long long14 #define usll unsigned ll15 #define mz(array) memset(array, 0, sizeof(array))16 #define mf1(array) memset(array, -1, sizeof(array))17 #define minf(array) memset(array, 0x3f, sizeof(array))18 #define REP(i,n) for(i=0;i<(n);i++)19 #define FOR(i,x,n) for(i=(x);i<=(n);i++)20 #define RD(x) scanf("%d",&x)21 #define RD2(x,y) scanf("%d%d",&x,&y)22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)23 #define WN(x) printf("%d\n",x);24 #define RE  freopen("D.in","r",stdin)25 #define WE  freopen("huzhi.txt","w",stdout)26 #define mp make_pair27 #define pb push_back28 const double pi=acos(-1.0);29 const double eps=1e-10;30 31 const int maxn=222222;32 33 int n,m,l;34 int a[maxn];35 36 int farm() {37     int now=0,pre=-maxn;38     int i=1;39     int ans=0;40     a[n+1]=m;41     sort(a+1,a+n+2);42     a[n+2]=m+l+1;43     while(now<m) {44         while(a[i]<=now+l)i++;45         //printf("i=%d\n, a[i]=%d, a[i-1]=%d\n",i,a[i],a[i-1]);46         if(a[i-1]>now) {47             pre=now;48             now=a[i-1];49             ans++;50         } else {51             int w=(a[i]-now)/(l+1)-1;52             if(w>0) {53                 int t=max(now+1,pre+l+1);54                 pre=t+(w-1)*(l+1);55                 now+=w*(l+1);56                 ans+=w*2;57             } else {58                 int t=now;59                 now=max(now+1,pre+l+1);60                 pre=t;61                 ans++;62             }63         }64         //printf("%d %d %d\n",now,pre,ans);65     }66     return ans;67 }68 69 int main() {70     int T,cas=1;71     int i;72     RD(T);73     while(T--) {74         RD3(n,m,l);75         FOR(i,1,n)scanf("%d",&a[i]);76         printf("Case #%d: %d\n",cas++,farm());77     }78     return 0;79 }
View code

 

Hdu5037 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.