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)