题目链接

题目

题目描述

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;
}

NC24949 [USACO 2008 Jan S]Running的更多相关文章

  1. [USACO 2008 Jan. Silver]架设电话线 —— 最短路+二分

    一道图论的最短路题.一开始连最短路都没想到,可能是做的题太少了吧,完全没有思路. 题目大意: FJ的农场周围分布着N根电话线杆,任意两根电话线杆间都没有电话线相连.一共P对电话线杆间可以拉电话线,第i ...

  2. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

  3. USACO翻译:USACO 2012 JAN三题(1)

    USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...

  4. USACO翻译:USACO 2013 JAN三题(1)

    USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...

  5. USACO翻译:USACO 2014 JAN三题(2)

    USACO 2014 JAN 一.题目概览 中文题目名称 队伍平衡 滑雪录像 滑雪场建设 英文题目名称 bteams recording skicourse 可执行文件名 bteams recordi ...

  6. USACO翻译:USACO 2014 JAN三题(1)

    USACO 2014 JAN 一.题目概览 中文题目名称 滑雪场设计 滑雪降速 滑雪场评级 英文题目名称 skidesign slowdown skilevel 可执行文件名 skidesign sl ...

  7. Usaco 2019 Jan Platinum

    Usaco 2019 Jan Platinum 要不是昨天老师给我们考了这套题,我都不知道usaco还有铂金这么一级. 插播一则新闻:杨神坚持认为铂金比黄金简单,原因竟是:铜 汞 银 铂 金(金属活动 ...

  8. [USACO 2018 Jan Gold] Tutorial

    Link: USACO 2018 Jan Gold 传送门 A: 对于不同的$k$,发现限制就是小于$k$的边不能走 那么此时的答案就是由大于等于$k$的边形成的图中$v$所在的连通块除去$v$的大小 ...

  9. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

    [题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...

  10. NC24017 [USACO 2016 Jan S]Angry Cows

    NC24017 [USACO 2016 Jan S]Angry Cows 题目 题目描述 Bessie the cow has designed what she thinks will be the ...

随机推荐

  1. Synchronized的使用及原理总结

    本文为博主原创,未经允许不得转载 Synchronized的使用总结:   1.作用 原理 synchronized 的锁膨胀升级过程 对象的内存布局 锁的消除及逃逸分析 synchronized的方 ...

  2. P1914 小书童——凯撒密码

    1.题目介绍 小书童--凯撒密码 题目背景 某蒟蒻迷上了 "小书童",有一天登陆时忘记密码了(他没绑定邮箱 or 手机),于是便把问题抛给了神犇你. 题目描述 蒟蒻虽然忘记密码,但 ...

  3. 【MLA】内存泄漏检查

    项目地址:skullboyer/MLA (github.com) 介绍 MLA 即 Memory Leak Analyzer,是一个排查内存泄漏的分析器 实现机制是在malloc时记录分配位置信息,在 ...

  4. [转帖]实用小技能:一键获取Harbor中镜像信息,快捷查询镜像

    [摘要]一键获取Harbor中的镜像列表,无需登录harbor UI,也可批量下载镜像到本地并保存为tar包.本文已参与「开源摘星计划」,欢迎正在阅读的你加入.活动链接:https://github. ...

  5. [转帖]一文读懂 K8s 持久化存储流程

    https://zhuanlan.zhihu.com/p/128552232 作者 | 孙志恒(惠志) 阿里巴巴开发工程师 导读:众所周知,K8s 的持久化存储(Persistent Storage) ...

  6. [转帖]Kubernetes部署Minio集群存储的选择,使用DirectPV CSI作为分布式存储的最佳实践

    Kubernetes部署Minio集群存储的选择,使用DirectPV CSI作为分布式存储的最佳实践 个人理解浅谈 1. 关于在kubernetes上部署分布式存储服务,K8s存储的选择 非云环境部 ...

  7. [转帖]JVM——内存区域:运行时数据区域详解

    https://www.jianshu.com/p/cded765cfd1b 关注:CodingTechWork,一起学习进步. 引言   我们经常会被问到一个问题是Java和C++有何区别?我们除了 ...

  8. 【转帖】Linux性能优化(十四)——CPU Cache

    一.CPU Cache 1.CPU Cache简介 CPU Cache是位于CPU与内存之间的临时存储器,容量比内存小但交换速度却比内存要快得多.Cache的出现主要是为了解决CPU运算速度与内存读写 ...

  9. [转帖]漏洞预警|Apache Tomcat 信息泄露漏洞

    http://www.hackdig.com/03/hack-953615.htm 棱镜七彩安全预警 近日网上有关于开源项目Apache Tomcat 信息泄露漏洞,棱镜七彩威胁情报团队第一时间探测到 ...

  10. 如何给所有的 await async 函数添加try/catch?

    如何给所有的 await async 函数添加try/catch?做全局捕获异常. 面试官:如何给所有的 await async 函数添加try/catch?做全局捕获异常. 我们可以使用 windo ...