洛谷 P.1353 [USACO08JAN]跑步Running(基础dp[刷表])

题目描述

The cows are trying to become better athletes, so Bessie is running on a track for exactly N (1 ≤ N ≤ 10,000) minutes. During each minute, she can choose to either run or rest for the whole minute.

The ultimate distance Bessie runs, though, depends on her ‘exhaustion factor’, which starts at 0. When she chooses to run in minute i, she will run exactly a distance of Di (1 ≤ Di ≤ 1,000) and her exhaustion factor will increase by 1 – but must never be allowed to exceed M (1 ≤ M ≤ 500). If she chooses to rest, her exhaustion factor will decrease by 1 for each minute she rests. She cannot commence running again until her exhaustion factor reaches 0. At that point, she can choose to run or rest.

At the end of the N minute workout, Bessie’s exaustion factor must be exactly 0, or she will not have enough energy left for the rest of the day.

Find the maximal distance Bessie can run.
输入格式
第1行: 2个用空格隔开的整数:N 和 M

第2…N+1行: 第i+1为1个整数: D i D_i Di

输出格式
输出1个整数,表示在满足所有限制条件的情况下,贝茜能跑的最大距离

输入输出样例
输入 #1
5 2
5
3
4
2
10
输出 #1
9
说明/提示
【样例说明】

贝茜在第1分钟内选择跑步(跑了5米),在第2分钟内休息,在第3分钟内跑步(跑了4米),剩余的时间都用来休息。因为在晨跑结束时贝茜的疲劳度必须为0,所以她不能在第5分钟内选择跑步

题意: 一个人跑步,共 n 分钟,第 i 分钟跑的距离为 D i D_i Di,若选择跑步每分钟会增加1疲劳度,选择休息每分钟减少1疲劳度,至疲劳度为0为止。整个过程中疲劳度不能超过 m ,且第 n 分钟疲劳度需是 0,求能跑的最大距离。
思路: dp[i][j] 表示第 i 分钟疲劳度为 j 时跑的最大距离。

有状态转移方程

  • dp[i][0] = max(dp[i][0],dp[i - 1][0]) //接着休息,
  • dp[i + j][0] = max(dp[i + j][0],dp[i][j]) //休息
  • dp[i + 1][j + 1] = max(dp[i + 1][j + 1],dp[i][j] + dis[i + 1]) //跑步
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;
inline LL read() {
    LL s = 0,w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-')
            w = -1;
        ch = getchar();
    }
    while(isdigit(ch))
        s = s * 10 + ch - '0',ch = getchar();
    return s * w;
}
inline void write(LL x) {
    if(x < 0)
        putchar('-'), x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

const int maxn = 1e4 + 510;
int d[maxn];
int dp[maxn][510];

int main() {
//#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//#endif
    int n = read(),m = read();
    for(int i = 1; i <= n; ++ i)
        d[i] = read();
    dp[1][1] = d[1];
    for(int i = 1; i <= n; ++ i){
        dp[i][0] = max(dp[i - 1][0],dp[i][0]);
        for(int j = 0; j <= min(i,m); ++ j) {
            dp[i + j][0] = max(dp[i + j][0],dp[i][j]);
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1],dp[i][j] + d[i + 1]);
        }
    }
    write(dp[n][0]);
    ptch('\n');
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值