Running (poj 3661)
来源:互联网 发布:数据漫游用打开吗 编辑:程序博客网 时间:2024/06/08 10:21
Description
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.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 contains the single integer: Di
Output
* Line 1: A single integer representing the largest distance Bessie can run while satisfying the conditions.
Sample Input
5 2534210
Sample Output
9
题意:
给出一个时间表,表示每分钟可以跑的路程,例如a[3]表示如果在第3分钟选择跑步则可以跑的距离。题目首先给出n,m表示一共有多少分钟以及最大的可接受疲劳度。
如果在某一分钟选择跑步则会疲劳度 ++,如果当前疲劳度等于m,那么就不能继续跑只能选择休息。而如果选择休息,每分钟就会下降一点疲劳,但是一旦选择休息就只能等疲劳到0才能继续前进。当然,任何时候都可以选择休息,包括疲劳为0的时候。
思路:
定义dp[i][j] 表示第 i 分钟疲劳为 j 的最大距离,那么显然 dp[i][j] = dp[i-1][j-1] + a[i];
然后因为在疲劳为0的时候也可以休息,则有 dp[i][0] = dp[i-1][0];
因为任意时刻都可以休息,则可以枚举在第i分钟疲劳为0的时候已经休息了k分钟,即从 i - k 分钟疲劳为k休息到现在
for(int k = 1;k <= m;k++)
if(i - k >= 0)
dp[i][0] = max(dp[i-k][k],dp[i][0]);
#include"iostream"#include"cstring"#include"cstdio"using namespace std;int n,m;int a[10005];int dp[10005][505];int main(void){ while(~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++) { for(int j = 1;j <= m;j++) { dp[i][j] = dp[i-1][j-1] + a[i]; } dp[i][0] = dp[i-1][0]; for(int k = 1;k <= m;k++) { if(i - k >= 0) { dp[i][0] = max(dp[i][0],dp[i-k][k]); } } } printf("%d\n",dp[n][0]); } return 0;}
- POJ 3661 Running
- poj 3661 Running
- POJ 3661 Running (DP)
- poj 3661 Running DP
- poj 3661 Running (dp)
- poj 3661 Running dp
- [dp] poj 3661 Running
- poj 3661 Running
- poj 3661 dp(Running)
- Running (poj 3661)
- POJ 3661 Running(dp)
- POJ-3661 Running(dp)
- poj 3661 running
- poj 3661——Running
- poj 3661 Running (区间DP)
- POJ 3661-Running(DP)
- poj 3661 Running(dp)
- POJ 3661 —— Running
- UML学习总结(1)——UML学习入门
- Mysql主从复制/读写分离/proxy机制
- NSIS 静默安装
- BZOJ 1082: [SCOI2005]栅栏|二分答案爆搜
- 在 Web 项目中应用 Apache Shiro
- Running (poj 3661)
- 使用Service和BroadcastReceiver实时监听网络状态
- CentOS7搭建Docker私有仓库
- 网站架构演化过程
- Android6.0 设备Idle状态(二)AlarmManagerService setIdleUntil接口
- catalan数
- 李白打酒问题
- android程序获取手机imei方法
- java跳出多重嵌套循环