NC24949 [USACO 2008 Jan S]Running

题目链接

题目

题目描述

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.

输入描述

  • Line 1: Two space-separated integers: N and M
  • Lines 2..N+1: Line i+1 contains the single integer: Di

输出描述

  • Line 1: A single integer representing the largest distance Bessie can run while satisfying the conditions.

示例1

输入

5 2
5
3
4
2
10

输出

9

说明

Bessie runs during the first minute (distance=5), rests during the second minute, runs for the third (distance=4), and rests for the fourth and fifth. Note that Bessie cannot run on the fifth minute because she would not end with a rest factor of 0.

题解

方法一

知识点:线性dp。

要注意,要求是每次休息要休息到疲劳为 \(0\) ,才能继续跑,并且疲劳为 \(0\) 还能继续休息。

\(dp[i][j]\) 为在第 \(i\) 分钟疲劳为 \(j\) 跑的最远距离。有转移方程:

\[\left \{ \begin{array}{l} dp[i][0] &= \max(dp[i-1][0],dp[i-j][j])\\ dp[i][j] &= dp[i-1][j-1] +d[i] \end{array} \right. \]

前者是休息的转移,后者是跑步的转移。

时间复杂度 \(O(nm)\)

空间复杂度 \(O(nm)\)

方法二

知识点:线性dp。

\(dp[i][j][k]\) 为第 \(i\) 分钟,疲劳为 \(j\) ,第 \(i\) 分钟的状态是 \(k\) (0/1,休息/跑步)的最远距离。转移方程为:

\[\left \{ \begin{array}{l} dp[i][j][0] = \max(dp[i-1][j+1][0] ,dp[i-1][j+1][1]) &,0\leq j \leq m-1\\ dp[i][0][0] = \max(dp[i-1][0][0],dp[i][0][0]) &\\ dp[i][j][1] = dp[i-1][j-1][1] + d[i] &,1\leq j \leq m\\ dp[i][1][1] = \max(dp[i][1][1],dp[i-1][0][0]+d[i]) & \end{array} \right. \]

可以用滚动数组优化空间。

时间复杂度 \(O(nm)\)

空间复杂度 \(O(m)\)

代码

方法一

#include <bits/stdc++.h>

using namespace std;

int d[10007], dp[10007][507];

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1;i <= n;i++) cin >> d[i];
    memset(dp, -0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1;i <= n;i++) {
        ///有且只有j = 0只能通过休息得到,因此只有(i,0)能继承休息
        for (int j = 0;j <= min(i, m);j++)
            dp[i][0] = max(dp[i][0], dp[i - j][j]);///(i,0) 可以通过 (i-j,j) 休息得到
        dp[i][0] = max(dp[i][0], dp[i - 1][0]);///也可以通过(i-1,0) 休息得到
        for (int j = 1;j <= m;j++)///只能通过跑步得到
            dp[i][j] = dp[i - 1][j - 1] + d[i];
    }
    cout << dp[n][0] << '\n';
    return 0;
}

方法二

#include <bits/stdc++.h>

using namespace std;

int d[10007], f[507][2], g[507][2];

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1;i <= n;i++) cin >> d[i];
    memset(f, -0x3f, sizeof(f));
    f[0][0] = 0;
    for (int i = 1;i <= n;i++) {
        for (int j = 0;j <= m;j++) {
            if (j <= m - 1) g[j][0] = max(f[j + 1][0], f[j + 1][1]);
            if (j) g[j][1] = f[j - 1][1] + d[i];
        }
        g[0][0] = max(g[0][0], f[0][0]);
        g[1][1] = max(g[1][1], f[0][0] + d[i]);
        for (int j = 0;j <= m;j++)
            f[j][0] = g[j][0], f[j][1] = g[j][1];
    }
    cout << f[0][0] << '\n';
    return 0;
}
posted @ 2022-08-13 16:57  空白菌  阅读(60)  评论(0编辑  收藏  举报