poj 3661 Running(区间dp)
2015-12-03 22:11
375 查看
Description
Input
Output
Sample Input
Sample Output
Source
USACO 2008 January Silver
题意:给你一个n,m,n表示有n分钟,每i分钟对应的是第i分钟能跑的距离,m代表最大疲劳度,每跑一分钟疲劳度+1,当疲劳度==m,必须休息,在任意时刻都可以选择休息,如果选择休息,那么必须休息到疲劳度为0,当然,当疲劳度为0的时候也是可以继续选择休息的,求在n分钟后疲劳度为0所跑的最大距离
思路:dp[i][j]表示在第i分钟疲劳度为j的时候所跑的最大距离,dp[i][j]=dp[i-1][j-1]+d[i];这个转移,说的是,第i分钟选择跑步,当然,第i分钟可以选择不跑步,那么就是选择休息,题目说了,选择休息的话必须要休息到疲劳度为0才可以跑,那还有一点,当疲劳度为0了,还是选择继续休息呢?dp[i][0]=dp[i-1][0];
选择休息,那么疲劳度到0了,这一点的最大距离怎么做呢?dp[i][0]=max(dp[i][0],dp[i-k][k]) (0<k<=m&&i-k>0)
这样所有的状态都考虑完了,可以写代码了.......
View Code
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 2 5 3 4 2 10
Sample Output
9
Source
USACO 2008 January Silver
题意:给你一个n,m,n表示有n分钟,每i分钟对应的是第i分钟能跑的距离,m代表最大疲劳度,每跑一分钟疲劳度+1,当疲劳度==m,必须休息,在任意时刻都可以选择休息,如果选择休息,那么必须休息到疲劳度为0,当然,当疲劳度为0的时候也是可以继续选择休息的,求在n分钟后疲劳度为0所跑的最大距离
思路:dp[i][j]表示在第i分钟疲劳度为j的时候所跑的最大距离,dp[i][j]=dp[i-1][j-1]+d[i];这个转移,说的是,第i分钟选择跑步,当然,第i分钟可以选择不跑步,那么就是选择休息,题目说了,选择休息的话必须要休息到疲劳度为0才可以跑,那还有一点,当疲劳度为0了,还是选择继续休息呢?dp[i][0]=dp[i-1][0];
选择休息,那么疲劳度到0了,这一点的最大距离怎么做呢?dp[i][0]=max(dp[i][0],dp[i-k][k]) (0<k<=m&&i-k>0)
这样所有的状态都考虑完了,可以写代码了.......
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<bitset> #include<map> #include<vector> #include<stdlib.h> #include <stack> using namespace std; #define PI acos(-1.0) #define max(a,b) (a) > (b) ? (a) : (b) #define min(a,b) (a) < (b) ? (a) : (b) #define ll long long #define eps 1e-10 #define MOD 1000000007 #define N 10006 #define M 506 #define inf 1e12 int n,m; int d ,dp [M];//dp[i][j]表示i分钟j疲劳时的最大距离。 int main() { while(scanf("%d%d",&n,&m)==2){ for(int i=1;i<=n;i++){ scanf("%d",&d[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]+d[i];//在i分钟时选择跑 } dp[i][0]=dp[i-1][0];//如果在疲劳度为0时选择休息,=dp[i-1][0] for(int j=1;j<=m;j++){ if(i-j>=0){ dp[i][0]=max(dp[i][0],dp[i-j][j]);//在i分钟选择休息到疲劳度为0 } } //dp[i][0]=dp[i-1][0]; } printf("%d\n",dp [0]); } return 0; }
View Code
相关文章推荐
- C中关于常量的概念
- Android 的事件分发机制(二)---ViewGroup的事件传递
- 2015.12.3
- U盘启动安装Cent0S 6.4安装中问题:安装程序试图挂载映像#1,但在硬盘上无法找到该映像...
- 黑马程序员—javaSE—多线程(一)
- 【动态规划+递归】:poj1664,放苹果
- WebKit之Port篇幅介绍
- 大一见习自我感想
- 使用字符串输出图形效果
- light oj 1214 - Large Division
- 黑马程序员--字符串对称问题的代码
- 内核自动探测中断号
- java编译的莫名奇妙问题总结
- Android之项目推荐使用的第三方库
- 中小企业集群ntpd服务搭建
- 杭电1063 hdu1063Exponentiation
- Ansible Playbooks的使用
- 一起学libcef--开篇(下载、CMake的使用)
- android NFC开发
- 一起学libcef--开篇(下载、CMake的使用)