http://poj.org/problem?id=3661

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


Sample Output

 

编程思想:

设dp[i][j]表示第i分钟疲劳值为j的最优状态,每分钟都有两种选择,

该分钟选择跑时的最优状态由上一分钟的最优状态决定,状态转移方程为:dp[i][j]=dp[i-1][j-1]+D[i],

该分钟选择休息时,由于一开始休息就要等疲劳值恢复为0时才可以开始选择跑或继续休息,在开始休息到疲劳值还未减为0的这段时间(用k表示)的每一分钟,只能选择休息,故该状态的最优状态由前面开始决定休息的时间点(i-k)决定,则态转移方程为:dp[i][0]=max(dp[i-k][k]);其中1=<k<=i-k。
题解AC代码:

 #include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<vector>
#include<string.h>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f;
int dp[][];
int D[];
int main()
{
//freopen("D:\\in.txt","r",stdin);
int T,cas,i,j,k,n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%d",&D[i]);
}
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
dp[i][j]=dp[i-][j-]+D[i];
}
dp[i][]=dp[i-][];
for(k=;k+k<=i;k++)
{
dp[i][]=max(dp[i][],dp[i-k][k]);
}
}
printf("%d\n",dp[n][]);
}
return ;
}

自己AC代码:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
const int INF=0x3f3f3f3f;
using namespace std;
#define maxn 10010 int a[maxn];
int dp[][maxn]; int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
int MAX=;
dp[][]=a[];
for(int j=;j<=n;j++)
{
int x=,y=j-;
while(y>&&x<=m)//找dp[0][j]
{
if(dp[x][y]>dp[][j])
dp[][j]=dp[x][y];
x++;
y--;
if(dp[][j]>MAX)
MAX=dp[][j];
else dp[][j]=MAX;
} for(int i=;i<=m;i++)//更新其余的
{
dp[i][j]=dp[i-][j-]+a[j];
} }
printf("%d\n",dp[][n]);
return ;
}

最新文章

  1. .net程序员转行做手游开发经历(一)
  2. 【Unity3D游戏开发】之利用语法糖添加自定义拓展方法(下) (十八)
  3. Direct3D11学习:(四)计时和动画
  4. linux系统下设置oracle开机自动启动
  5. 注册dll失败
  6. POJ 2689 Prime Distance (素数+两次筛选)
  7. Android 代码设置密码输入框内容的显示/隐藏
  8. 如何在 Swift 中优雅地处理 JSON
  9. linux搭建apache服务并修改默认路径
  10. 前端面试题-----js和jquery的区别是什么?
  11. python面试终极准备
  12. java简单框架设计
  13. 使用with语句优化pymysql的操作
  14. 03-Python入门学习-Python基础
  15. Shell脚本2
  16. Lock 从来就没有成功过
  17. js时间戳如何转时间
  18. ZOJ 3156 Taxi (二分 + 二分匹配)
  19. 基于Linux的Samba开源共享解决方案测试(一)
  20. tomcat服务器访问网址组成

热门文章

  1. C/C++学习笔记_gdb调试
  2. PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]
  3. docker入门资料及常用命令
  4. pytorch学习问题汇总
  5. 题解 P1654 【OSU!】
  6. KVM以及其虚拟机安装
  7. 学习Github必须要会的知识
  8. ubuntu root用户下找不到环境变量解决办法
  9. Mycat简介及适用场景
  10. ZJNU 2349 - 抽抽抽