DP, get the most apples, (POJ2385)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=2385

Test instructions

Cows collect apples under two apple trees, and cows can only walk between the two trees w times, and at t time, a tree will fall off an apple.

Problem Solving Report:

///Dp[t][w] Indicates the maximum number of apples that can be obtained by turning w times in 1~t seconds///state transition Equation Dp[t][w]=max (dp[t-1][w],dp[t-1][w-1])#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;intMain () {inta[1010];///A[i] moment, the position of the Apple landing    intdp[1010][ *]; intt,w; scanf ("%d%d",&t,&W);  for(intI=1; i<=t;i++) scanf ("%d",&A[i]); dp[0][0]=0; if(a[1]==1) {dp[1][0]=1; dp[1][1]=0; }    Else{dp[1][1]=1; dp[1][0]=0; }     for(intI=2; i<=t;i++)    {         for(intj=0; j<=w;j++)        {            if(j==0) {Dp[i][j]=dp[i-1][j]+a[i]%2; Continue; } Dp[i][j]=max (dp[i-1][j],dp[i-1][j-1]); if(j%2+1==A[i]) dp[i][j]++; }    }    intans=0;  for(intI=0; i<=w;i++) ans=Max (ans,dp[t][i]); printf ("%d\n", ans); return 0;}

DP, get the most apples, (POJ2385)

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.