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
 
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
",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 }
原文地址:https://www.cnblogs.com/njczy2010/p/3985370.html