POJ 3661-running (DP)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: In a straight line movement, every minute can exercise distance a[i], every minute can choose to exercise or rest, there is a fatigue coefficient, initially 0, each exercise one minute fatigue coefficient plus 1, (can not be greater than m) the same, each rest a minute, fatigue coefficient minus 1, (can not be less than 0) for n minutes after the maximum distance A fatigue factor of 0 is required for n minutes.

Two states, the current time and the current fatigue factor. Set DP[I][J] =dp[i-1][j-1]+a[i] (j>0) Else Dp[i][j] =max (Dp[i][j],max (dp[i-k][0],dp[i-k][k)) (k<=m&&k <i) that is, the current fatigue coefficient of 0 o'clock, may be from the previous state of rest, take the largest. Final output DP[N][0]

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 1<<12#define _ll __int64# Define ll long long#define INF 0x3f3f3f3f#define Mod 1000000007#define pp pair<int,int> #define ull unsigned long lon Gusing namespace Std;int dp[10002][502],n,m,a[10002];void solve () {memset (dp,0,sizeof (DP));DP [1][0]=0;dp[1][1]=a[1] ; for (int i=2;i<=n;i++) {for (int j=0;j<=m;j++) {if (j==0) {int tem=-1;for (int k=0;k<=m&&k<i;k++) TEM =max (Tem,max (dp[i-k][0],dp[i-k][k));DP [I][0]=tem;} Elsedp[i][j]=dp[i-1][j-1]+a[i];}} printf ("%d\n", Dp[n][0]);}    int main () {while (~scanf ("%d%d", &n,&m)) {for (int i=1;i<=n;i++) scanf ("%d", a+i); solve ();} return 0;}
Memory is not very good to write. Don't write it ~ ~


POJ 3661-running (DP)

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.