POJ 2385 Apple Catching

Source: Internet
Author: User

Compared to the backpack has been in the brush, the problem can be considered the most pure DP, write down a simple question.

Test instructions is said cows in 1 trees and 2 trees back and forth to move apples, there is a limit on the number of moves, asked the last to get the maximum number of apples, containing the best sub-structural properties, the approximate state transfer is not difficult to come up with, to Dp[i][j] for the first minute to use the J-Mobile opportunity to obtain the maximum number of apples ( Implicit in the 1 tree or 2 tree information, the parity can be found, at the beginning of the 0min at 1 trees , the general state transfer equation is:

DP[I][J] = j & 1? C[I][2]: c[i][1] + max (Dp[i+1][j], dp[i+1][j+1]), if (j<w)

+ Dp[i+1][j], else

The j&1 in the formula is to judge at this time Dp[i][j] is located in 1 trees or 2 trees, the whereabouts of the Apple with an array c[] recorded (can be visually drawn on the draft paper), the code is not difficult to write:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 intdp[1003][ -];7 BOOLc[1003][3];8 9 intMain () {Ten     intt,w,i,j,x; One      while(~SCANF ("%d%d",&t,&W)) { AMemset (c,0,sizeof(c)); -          for(i=1; i<=t; ++i) { -scanf"%d",&x); thec[i][x]=1; -         } -          for(j=0; j<=w; ++j) -dp[t][j]= j&1? c[t][2]:c[t][1]; +          for(I= t1; i>=1; --i) { -             inttmp=min (i,w); +              for(j=0; j<=tmp; ++j) { Adp[i][j]= j&1? c[i][2]:c[i][1]; at                 if(j<w) dp[i][j]+= Max (dp[i+1][j],dp[i+1][j+1]); -                 Elsedp[i][j]+= dp[i+1][j]; -             } -         } -printf"%d\n", Max (dp[1][0],dp[1][1])); -     } in     return 0; -}

I also saw people using more streamlined code and the ability to optimize space in one dimension, but in order to intuitive, can let themselves understand, I do not want to toss those, once the obsessive-compulsive disorder is the time to put a put, the full brush problem is the king, see himself so little poor brush problem, and brother almost poor a magnitude of the problem, You don't want to go all the way to perfection for every question.

POJ 2385 Apple Catching

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.