题解 | Running-牛客假日团队赛8J题

J-Running_牛客假日团队赛8

https://ac.nowcoder.com/acm/contest/1069/J

题目描述

The cows are trying to become better athletes, so Bessie is running on a track for exactly 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 and her exhaustion factor will increase by 1 -- but must never be allowed to exceed . 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:

输出描述:

* 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.

解答

题意:Bessie想成为一名运动牛,它每分钟跑的距离为,初始时疲劳度为0,每跑一分钟它就增加1疲劳度,但是它的疲劳度不能超过M。当它选择休息时,它的疲劳度每分钟减少1,但是它必须休息到疲劳度为0时才能继续跑。当N分钟过去后,它的疲劳度必须为0,否则它就没有足够的能量维持生活。求Bessie能跑的最远距离。
思路:定义为第分钟疲劳度为时能够跑的最远距离,若第分钟疲劳度不为,说明上一分钟在跑步,所以
若第分钟疲劳度为,那么上一分钟疲劳度可能也为,或者歇息了分钟,由此可得以下状态转移方程:



Accepted Code:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
typedef long long ll;
int dp[MAXN][505], a[MAXN];
int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= n; i++) {
        dp[i][0] = dp[i - 1][0];
        for (int j = 1; j <= m; j++) {
            if (j < i)
                dp[i][0] = max(dp[i][0], dp[i - j][j]);
            dp[i][j] = dp[i - 1][j - 1] + a[i];
        }
    }
    printf("%d\n", dp[n][0]);
    return 0;
}

来源:子夜葵
全部评论

相关推荐

点赞 评论 收藏
分享
VirtualBoo...:都去逗他了?
点赞 评论 收藏
分享
就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务