Dynamic planning Issues

Source: Internet
Author: User

Reference http://chuansong.me/n/112761

  Start with a basic example

  We have a number of coins of $1, $3 and $5, how to make up $11 with the fewest coins?

Make N=f (v), indicating that n coins can be together to the V-dollar, now need to find the minimum value of n

When V is 0 o'clock, f (0) =0

When V is 1 o'clock, can take 1 yuan coin, I take 1 1 yuan coin, f (1) =1+f (1-1)

When V is 2 o'clock, can take 1 yuan coins, I take 1 1 yuan coins, F (2) =1+f (2-1) =2

When V is 3 o'clock, can take 1 yuan coins and 3 yuan coins, I can take 1 1 yuan or a 3 Yuan, F (3) =min (1+f (3-1), 1+f (3-3)) =1

When V is 4 o'clock, can take 1 yuan coins and 3 yuan coins, I can take a 1 yuan or a 3 Yuan, F (4) =min (1+f (4-1), 1+f (4-3)) =2

When V is 5 o'clock, you can take 1 yuan coins, 3 yuan coins and 5 yuan coins, F (5) =min (1+f (5-5), 1+f (5-3), 1+f (5-1))) =1

That's enough, the pass. F (v) =min (f (v-x[j]) +1) x[j] denotes a variety of denominations

1 intGetmin (intv,vector<int>&x) {//x[0]=1,x[1]=3,x[2]=52     inttemp[ -]={0}; In fact, only with a guaranteed temp[0]=0.3     intFlag; 4      for(intI=1; i<=v;++i) {5flag= +; 6          for(intj=0; J<x.size (); + +j) {//min (f (i-x[j]) +1), bottom-up way, no separate memo7             if(x[j]<=i&&flag>temp[i-x[j]]+1){    8flag=temp[i-x[j]]+1;9             }Ten         } Onetemp[i]=Flag; A     } -     returnTemp[v]; -}

To summarize, dynamic programming is looking for the state equation F (v) =min (f (v-x[j]) +1)

  And then the last example

  Find the length of the longest non-descending subsequence of a sequence, eg, sequence 5,3,4,8,6,7

  N=f (v), which represents the longest non-descending subsequence of the first v number is n

When V=1, F (1) =1

When v=2, the 2nd number is smaller than the 1th one, F (2) =1

When v=3, the 3rd number is larger than the 2nd, F (3) =f (2) +1

When v=4, the 4th number is larger than the 1th, 2, 3 digits, F (4) =max (f (1) +1,f (2) +1,f (3) +1)

That's enough, State equation F (v) =max (1,f (j) +1) J<v&&a[v]>=a[j]

1 intGetmax (vector<int>&a) {2     if(A.empty ())return 0;3     intb[ +]={1};4     intflag=1;5      for(intI=0; I<a.size (); + +i) {6          for(intj=i-1; j>=0;--j) {7             if(a[i]>=a[j]&&b[i]<b[j]+1){8b[i]=b[j]+1;9             }Ten             if(b[i]<B[j]) Oneb[i]=B[j]; A         } -     } -     returnB[a.size ()-1]; the}

Dynamic planning Issues

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.