题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1613
水题,DP
f[i][j]表示第i秒末疲劳度为j时的最大跑步距离
那么两种情况,跑步和休息
休息也分两种情况,疲劳度>0和疲劳度=0
转移方程很好推,见代码
贴代码
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int f[10005][505];
int a[10005];
char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
int read(int &x){
char ch=nc();int ff=1,res=0;
while (!('0'<=ch&&ch<='9'))ch=='-'?ff=-1:0,ch=nc();
while ('0'<=ch&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=nc();
x=ff*res;
}
int Max(int x,int y){
return x>y?x:y;
}
int main(){
// freopen("1613.in","r",stdin);
// freopen("1613.out","w",stdout);
read(n);read(m);
for (int i=1;i<=n;i++)read(a[i]);
memset(f,0,sizeof(f));
for (int i=0;i<=n;i++)
for (int j=0;j<=m;j++){
// printf("%d %d %d\n",i,j,f[i][j]);
if (j+1<=m)f[i+1][j+1]=Max(f[i+1][j+1],f[i][j]+a[i+1]);//跑1s
if (i+j<=n)f[i+j][0]=Max(f[i+j][0],f[i][j]);//开始休息
if (j==0)f[i+1][0]=Max(f[i+1][0],f[i][0]);//继续休息,不跑
}
printf("%d",f[n][0]);
return 0;
}
【写的有漏洞的,欢迎路过大神吐槽】
2017/08/15 23:11:19
Ending.