poj2385 Apple catching (DP state transfer equation derivation)

Source: Internet
Author: User

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)

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.