传送门:牛客
题目描述:
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.
输入:
5 2
5
3
4
2
10
输出:
9
一道简单的dp题目,这种线性的转移方程也不是很难想,而且数据不大,意味着可以乱搞
主要思路:
- 可以使用 d p [ i ] [ j ] dp[i][j] dp[i][j]记录前 i i i分钟疲劳程度为 j j j时所走的最远距离.那么我们的转移方程也就不难得出了.显然就是前一分钟走或者不走
假设前一分钟准备走
d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i − 1 ] [ j − 1 ] + d [ i ] ) dp[i][j]=min(dp[i][j],dp[i-1][j-1]+d[i]) dp[i][j]=min(dp[i][j],dp[i−1][j−1]+d[i])
假设前几分钟不准备走,注意此时我们必须休息到底
d p [ i ] [ 0 ] = m i n ( d p [ i ] [ j ] , d p [ i − k ] [ j ] ) dp[i][0]=min(dp[i][j],dp[i-k][j]) dp[i][0]=min(dp[i][j],dp[i−k][j])
此 时 我 们 不 能 使 用 d p [ i ] [ j ] = d p [ i − 1 ] [ j + 1 ] 此时我们不能使用dp[i][j]=dp[i-1][j+1] 此时我们不能使用dp[i][j]=dp[i−1][j+1]
搞清楚这些之后我们的代码也就不难写出了:
下面是具体的代码部分:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
#include <deque>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x=0,w=1;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*w;
}
#define maxn 1000000
#define ll_maxn 0x3f3f3f3f3f3f3f3f
const double eps=1e-8;
int n,m;int d[maxn];
int dp[12000][510];
int main() {
n=read();m=read();
for(int i=1;i<=n;i++) d[i]=read();
for(int i=1;i<=n;i++) {
dp[i][0]=dp[i-1][0];
for(int j=1;j<=min(i,m);j++) {
dp[i][0]=max(dp[i][0],dp[i-j][j]);
dp[i][j]=max(dp[i][j],dp[i-1][j-1]+d[i]);
}
}
cout<<dp[n][0]<<endl;
return 0;
}