Hdu--4487--dp

Source: Internet
Author: User

DP is the most interesting. Because it never has a fixed routine.

The most boring is the template problem.

I got a hole in the problem.   Test instructions read the wrong. Here's a more interesting question was what's the expected rightmost position you'll attain during the walk.

That is, the rightmost position that we can reach in each walk, at first, as far as the right-hand position can be reached after n-step = =

This is a very obvious linear expectation DP.

A total of 2 state transitions for dp[x,y,z] go to step x the current position is Y at this point in the process of X-step, the rightmost distance is Z

Then Y and Z have a total of 3 size relationships.

Y<z

Dp[x&1][y][z] = dp[(x-1) &1][y+1][z] * L + dp[(x-1) &1][y][z] * M + dp[(x-1) &1][y-1][z] * R;

First you have to be clear dp[x,y,z] I was transferred by this state of dp[x-1,u,v, so it's guaranteed that even if I walk a step forward at U, it's not going to be more than V.

Y==z

Dp[x&1][y][z] = dp[(x-1) &1][y-1][z-1] * r + dp[(x-1) &1][y][z] * M + dp[(x-1) &1][y-1][z] * R;

This is probably the first time I have reached the z position, or I stopped at the z position in the last step, or I got to the top of Z, but the last step to the left, this step to the right, or the furthest distance is Z.

Y<z

Because I'm now in the Y position, so far from the right, Z must be at least equal to Y. < left to take the origin processing >

As to whether this scrolling array is optimized or not, it doesn't matter. Data is small 100

1#include <iostream>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 intN;7 DoubleL, R, M;8 Const intSize = the;9 Doubledp[2][size*2][size*2];//Dp[i,j,k] Go to step I at the time of the J this position to reach the far right distance is KTen  One voidInit () A { -Memset (DP,0,sizeof(DP)); -dp[0][size][size] =1; the } -  - voidSolve () - { +      for(inti =1; I<=n; i++ ) -     { +          for(intj = size-i; I<=size+i; J + + ) A         { at              for(intk = max (j,size); K<=size+i; k++ ) -             { -                 if(j==k)//The first time I walked to J, I walked to K and walked back. -                 { -dp[i&1][j][k] = dp[(i-1) &1][j-1][k-1] * R + dp[(i-1) &1][J][K] * M + dp[(i-1) &1][j-1][K] *R; -                 } in                 Else//case of k > J -                 { todp[i&1][j][k] = dp[(i-1) &1][j+1][K] * L + dp[(i-1) &1][J][K] * M + dp[(i-1) &1][j-1][K] *R; +                 } -             } the         } *     } $ }Panax Notoginseng  - intMain () the { +     intT, M; A     Doubleans; thescanf"%d",&t); +      while(t-- ) -     { $ init (); $scanf"%d%d%lf%lf",&m,&n,&l,&R); -M =1LR; - solve ();  theAns =0; -          for(intj = size-n; J<=size+n; J + + )Wuyi         { the              for(intk = size; K<=size+n; k++ ) -             { WuAns + = dp[n&1][j][k] * (K-size); -             } About         } $printf"%.4lf\n", ans); -     } -     return 0; -}
View Code

Hdu--4487--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.