码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5037 Frog (贪心)

时间:2014-09-21 20:44:21      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   java   ar   

http://acm.hdu.edu.cn/showproblem.php?pid=5037

网络赛 北京 比较难的题

Frog

Time 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 could 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 could 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 could jump over the river in maximal steps.And you don‘t care 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 should jump.
 

Sample Input
2 1 10 5 5 2 10 3 3 6
 

Sample Output
Case #1: 2 Case #2: 4
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5041 5040 5039 5038 5036

题意:

有个青蛙在数轴上跳,要从0跳到M,每次能跳0~L,数轴是个水面,只能跳到石头上。给出若干已有石头,可以在任意位置添加石头。要让青蛙能跳到终点,并且跳的步数最大。

题解:

贪心狂跳。

先给石子排个序,添加一个石子m代表终点。

设当前青蛙在now处,上一步在pre处(初始化负无穷)。

先在已有石子上狂跳,每次跳到能跳的最远的那个。

当不能继续跳的时候(下一个石子距离过远),就加石子。

加石子要加在max(now+1,pre+l+1)处,也就是pre刚好不能跳到的,now能跳到的最近的那一个。

然后重复,如果能跳到已有石头就跳,跳不到就加石头。

简直贪,这样就能得到正确答案!

不过这样有可能要跳太多次,会TLE,所以当两个石头距离过远的时候,可以特殊处理一下新建好多石头跳好多步。

可以发现连续新建两个石头跳两步的话,肯定是到now+l+1,于是可以直接跳若干个(l+1)步。

 

(简直比青蛙棋还难,我怕了)

代码:

bubuko.com,布布扣
 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 long
14 #define usll unsigned ll
15 #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_pair
27 #define pb push_back
28 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 (贪心)

标签:des   style   blog   http   color   io   os   java   ar   

原文地址:http://www.cnblogs.com/yuiffy/p/3984846.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!