https://vjudge.net/problem/POJ-2385
Swipe the first question on the first day of the simple DP.
State: Dp[i][j] represents the maximum number of apples that are obtained by moving J times in the second. The key is to think about moving J times, judging by J's parity where the person is.
Thought for a long time, and finally a reference to their own ideas and the latest code 52050207
I am more than what he lacks is the state of dp[i][0], and everything behind it is based on this.
1#include <iostream>2#include <cstdio>3#include <queue>4#include <cstring>5#include <algorithm>6#include <cmath>7#include <Set>8 #defineINF 0x3f3f3f3f9typedefLong Longll;Ten using namespacestd; One inta[1010], dp[1010][1010]; A intMain () - { -Memset (DP,0,sizeof(DP)); the intN, M; -CIN >> N >>m; - for(inti =1; I <= N; i++){ -CIN >>A[i]; + } - for(inti =1; I <= N; i++){ +dp[i][0] = dp[i-1][0]; A if(A[i] = =1){//apples on the left atdp[i][0]++; - for(intj =1; J <= M; J + +){ - if(j&1)//man on the right. -DP[I][J] = max (dp[i-1][J], dp[i-1][j-1]); - Else//man on the left -DP[I][J] = max (dp[i-1][J], dp[i-1][j-1])+1; in } - } to Else{//the apples are on the right + for(intj =1; J <= M; J + +){ - if(j&1)//man on the right. theDP[I][J] = max (dp[i-1][J], dp[i-1][j-1])+1; * Else//man on the left $DP[I][J] = max (dp[i-1][J], dp[i-1][j-1]); Panax Notoginseng } - } the } + intMAXM =-INF; A for(inti =0; I <= m; i++){ theMAXM =Max (MAXM, Dp[n][i]); + } -cout << MAXM <<Endl; $ return 0; $}
poj2385 Apple catching (DP state transfer equation derivation)