[USACO08JAN] Running S - dp

Required \ (n-\) minutes of the morning run, in the \ (I \) within minutes run \ (D_i \) m, increases fatigue \ (1 \) , whenever fatigue can not exceed \ (m \) , rest one minute fatigue will reduce \ (1 \) , it is necessary to take a break to rest fatigue is \ (0 \) , fatigue recovery to \ (0 \) will not continue to decline. The initial state of fatigue \ (0 \) . At the end of fatigue must be restored to \ (0 \) . Seeking up to run far. \ (n \ leq 10 ^ 4 , d_i \ leq 1000, m \ leq 500 \)

Solution

Set \ (f [i] [j ] \) represents Before considering \ (I \) minutes, at the end of fatigue \ (J \) , the maximum distance that can be obtained, the second decision is running or when the transfer start can rest

Mispronounce problem ruined his life

#include <bits/stdc++.h>
using namespace std;

const int N = 20005;
int n,m,d[N],f[N][505];

void sh(int &x,int y) {
    x=max(x,y);
}

signed main() {
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>d[i];
    for(int i=1;i<=n;i++) {
        for(int j=0;j<=m;j++) {
            sh(f[i][j+1],f[i-1][j]+d[i]);
            if(j==0) sh(f[i][j],f[i-1][j]);
            sh(f[i+j-1][0],f[i-1][j]);
        }
    }
    cout<<f[n][0];
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12443757.html