HDU 5037 frog greedy DP

Source: Internet
Author: User

Ah, pay attention to the details .....

 

 

11699193 08:46:42 Accepted 5037 796 Ms 1864 K 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 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 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 }

 

HDU 5037 frog greedy DP

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.