Dp:apple catching (POJ 2385)

Source: Internet
Author: User

            

How Cows eat apples

The main problem: a cow called Bessie, can eat apples, and then there are two trees, apples on the tree will be dropped one minute, the cow one minute can be in two trees to eat apples (and do not eat on the ground), and then turn back only limited times W, ask you This is called Bessie cattle can eat up to how many apples

First we should be very easy to think that this must be done with DP, and then is to consider how to store the old value of the problem, because the tree has two trees, and then each round-trip state corresponds to different results, so we should use a two-dimensional matrix to store (the number of apples is not important, because we only calculate the last, The front can be thrown away).

And how should the state equation be written? It is also very simple to define a state of dp[i][j] that represents the maximum number of times a tree can be eaten by the time it is left, and the cow can walk from a tree next to it.

So the state transfer equation is natural.

Dp[i][j]=max (dp[i][j],dp[i-1][j+1]) +1 (j<w && i+1>w-j)

dp[i][j]=dp[i][j]+1; (j=w)

  Also note that there are some states are meaningless, typical is the time of i+1<w-j (you think that the number of minutes the Apple dropped more than the number of cows ran over, how possible!) )

  

1#include <stdio.h>2#include <stdlib.h>3 #defineMAX (a) (a) > (b)? (a):(B)4 5 Static inttree[2][ to];6 7 intMainvoid)8 {9     intT, W, I, K, which_tree, ans =0;Ten      while(~SCANF ("%d%d", &t, &W)) One     { A          for(i =0; i < T; i++) -         { -scanf"%d", &which_tree); theTree[which_tree-1][w]++; -              for(k = W-1; K >=0&& i +1> w-k; k--) -Tree[which_tree-1][K] = -MAX (tree[! ( Which_tree-1)][k +1] +1, Tree[which_tree-1][K] +1); +         } -          for(i =0; I <2; i++) +              for(k =0; K <= W; k++) AAns =MAX (Tree[i][k], ans); atprintf"%d", ans); -     } -  -     return 0; -}

Dp:apple catching (POJ 2385)

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.