hdu 5037 Frog 贪心 dp

哎,注意细节啊,,,,,,,思维的严密性。。。。。

11699193 2014-09-22 08:46:42 Accepted 5037 796MS 1864K 2204 B G++ czy

Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 454    Accepted Submission(s): 96

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

2014 ACM/ICPC Asia Regional Beijing Online

Recommend

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

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<string>
 10
 11 #define N 200005
 12 #define M 15
 13 #define mod 10000007
 14 //#define p 10000007
 15 #define mod2 100000000
 16 #define ll long long
 17 #define LL long long
 18 #define maxi(a,b) (a)>(b)? (a) : (b)
 19 #define mini(a,b) (a)<(b)? (a) : (b)
 20
 21 using namespace std;
 22
 23 int T;
 24 int n;
 25 int m,l;
 26 int dp[N];
 27 int p[N];
 28
 29 void ini()
 30 {
 31     memset(dp,0,sizeof(dp));
 32     scanf("%d%d%d",&n,&m,&l);
 33     for(int i=1;i<=n;i++){
 34         scanf("%d",&p[i]);
 35     }
 36     sort(p+1,p+1+n);
 37     p[n+1]=m;
 38 }
 39
 40
 41 void solve()
 42 {
 43     int sh;
 44     int te;
 45     int now;
 46     int end;
 47     int d;
 48     int tnow;
 49     now=0;
 50     d=1;
 51     for(int i=1;i<=n+1;){
 52         dp[i]=dp[i-1];
 53         te=(p[i]-now);
 54         sh=te/(l+1);
 55         dp[i]+=sh*2+1;
 56         if(te%(l+1)!=0){
 57             //dp[i]--;
 58           //  if(sh!=0 && te%(l+1)<d){
 59                // dp[i]--;
 60                // tnow=now+(sh-1)*(l+1)+d;
 61                // end=tnow+l;
 62                // now=p[i];
 63
 64            // }
 65            // else{
 66                 now=now+sh*(l+1);
 67                 tnow=now;
 68                 end=tnow+l;
 69                 now=p[i];
 70            // }
 71
 72             i++;
 73             while(i<=n+1 && p[i]<=end){
 74                 dp[i]=dp[i-1];
 75                 now=p[i];
 76                 i++;
 77             }
 78             d=l+1-(now-tnow);
 79         }
 80         else{
 81             dp[i]--;
 82             tnow=now+(sh-1)*(l+1)+d;
 83             end=tnow+l;
 84             now=p[i];
 85             i++;
 86             while(i<=n+1 && p[i]<=end){
 87                 dp[i]=dp[i-1];
 88                 now=p[i];
 89                 i++;
 90             }
 91             d=l+1-(now-tnow);
 92         }
 93     }
 94 }
 95
 96 void out()
 97 {
 98     printf("%d\n",dp[n+1]);
 99 }
100
101 int main()
102 {
103     //freopen("data.in","r",stdin);
104     //freopen("data.out","w",stdout);
105     scanf("%d",&T);
106     for(int cnt=1;cnt<=T;cnt++)
107    // while(T--)
108    // while(scanf("%d%d",&n,&m)!=EOF)
109     {
110       //  if(n==0 && m==0) break;
111         printf("Case #%d: ",cnt);
112         ini();
113         solve();
114         out();
115     }
116
117     return 0;
118 }
时间: 2024-10-14 14:12:52

hdu 5037 Frog 贪心 dp的相关文章

HDU 5037 FROG (贪心)

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 (

HDU 5037 FROG 贪心 2014北京网络赛F

题目链接:点击打开链接 题意:有一条小河长为M的小河,可以看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙,随意添加石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多需要多少次. 思路:不妨假设青蛙每个石头都要经过一次,用step表示青蛙上一次跳的步长,每跳一次对目前点到下一点的距离和step的和与L做比较,如果小与,证明青蛙可以一次跳到这,更新step和青蛙位置,cnt保持不变,若大于,证明青蛙至少需要再跳一次,若lenth<=l,则直接跳,更新step=lenth,cnt+

hdu 5037 Frog(高效)

题目链接:hdu 5037 Frog 题目大意:给定N,M,L,表示有一只条宽为M的河,青蛙要从0跳到M,青蛙的最大跳跃能力为L,现在已经存在了N块石头,现在要放任意数量的石头,使得说青蛙需要用最多的步数跳到M. 解题思路:维护两个指针,pos表示青蛙当前的位置,mv表示青蛙在前一个位置能跳到的最远位置.然后周期L+1的长度可以让青蛙需要跳两次.但注意每一段长度的最后一个周期需要特判,因为有可能只需要一步. #include <cstdio> #include <cstring>

hdu 5037 Frog (贪心)

Frog Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 789    Accepted Submission(s): 198 Problem Description Once upon a time, there is a little frog called Matt. One day, he came to a river.

hdu 5037 Frog(贪心)

分析:贪心吧,让三个石头第一个和第三个距离为L+1,并让每次跳的点尽量小. 石头是可能无序的,比赛是实在没发现,就加了个排序过了,哎... 和代码一起讲做法吧,假设情况1 :上一步k加这一步余数x大于L,则最后剩余部分需要单独跳:情况2:上一步k加这一步余数x小于等于L,最后剩余部分可以并进上一步,即k+x. 画了一张图: 代码: #include<iostream> #include<queue> #include<map> #include<cstdio>

hdu 2128 Frog(简单DP)

Frog Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 712    Accepted Submission(s): 338 Problem Description A little frog named Fog is on his way home. The path's length is N (1 <= N <= 100),

hdu 5037 Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 2040    Accepted Submission(s): 616 Problem Description Once upon a time, there is a little frog called Matt. One day, he came to a river. The

hdu 5037 Frog(北京网络赛)

Frog                                                              Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1086    Accepted Submission(s): 285 Problem Description Once upon a time, the

hdoj 5037 Frog 【万恶的贪心】

题目:hdoj 5037 Frog 题意:一直聪明的青蛙,每次能条 l 的长度,河宽为m,喝中心有一些陆地,它会选择尽量少的次数跳,现在上帝可以任意往喝里面放陆地(视为点),让青蛙跳的次数最多,求最大次数? 分析:1:首先如果加入大于(l+1)的距离,那么上帝会给他分成(l+1)的段,因为我给他(l+1),它一下子跳不过去,必然中间还要一个点,让青蛙跳,这样肯定步数最大.(贪心策略) 2:贪心的时候以每一段剩余区间开始点作为贪心的开始点,假如它和后面区间的和大于等于(l+1)时,那么可以让它跳过