[USACO08JAN]Running

嘟嘟嘟

这很显然是一道dp题。

令dp[i][j]表示第 i 分钟末,疲劳度为 j 是的最大跑步距离,则

   dp[i][0] = max(dp[i - 1][0], max(dp[i - j][j]))

   dp[i][j] = max(dp[i - 1][j - 1] + a[i])

因为题中说即使疲劳值为0了,仍可以休息,所以dp[i][0]也可以从dp[i - 1][0]转换过来。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const int eps = 1e-;
const int maxn = 1e4 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, m, a[maxn];
int dp[maxn][maxn]; int main()
{
n = read(); m = read();
for(int i = ; i <= n; ++i) a[i] = read();
for(int i = ; i <= n; ++i)
{
dp[i][] = dp[i - ][];
for(int j = ; j <= min(i, m); ++j) dp[i][] = max(dp[i][], dp[i - j][j]);
for(int j = ; j <= min(i, m); ++j) dp[i][j] = max(dp[i][j], dp[i - ][j - ] + a[i]);
}
write(dp[n][]); enter;
return ;
}
上一篇:php – 从XMLReader打开的simplexml中的CData


下一篇:UVA 11770 Lighting Away