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 ;
}

poj-3661 Running(DP)的更多相关文章

  1. poj 3661 Running(区间dp)

    Description The cows are trying to become better athletes, so Bessie ≤ N ≤ ,) minutes. During each m ...

  2. POJ 3034 Whac-a-Mole(DP)

    题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在( ...

  3. poj 2229 Sumsets(dp)

    Sumsets Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 400000/200000K (Java/Other) Total Sub ...

  4. poj -2229 Sumsets (dp)

    http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...

  5. poj 3230 Travel(dp)

    Description One traveler travels among cities. He has to pay for this while he can get some incomes. ...

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  8. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  9. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

随机推荐

  1. 201771010123汪慧和《面向对象程序设计Java》第十三周实验总结

     一.理论部分 1.GUI为用户提供交互式的图形化操作界面. (1)提供了程序的外观和感觉.(2)程序利用图形用户界面接受用户的输入,向用户输出程序运行的结果. 2.Java有专门的类库生成各种标准图 ...

  2. Neo4j--常用的查询语句

    参考 https://www.w3cschool.cn/neo4j 准备工作 插入一堆朝代节点 插入我大明皇帝节点 创建大明皇帝统治大明王朝的关系 看一下结果 WHERE WHERE 语法 WHERE ...

  3. 用Pandas Dataframe支撑起多只金融产品股票的数据内部形态

    3. 如果同时拿一个板块股票的收市价和成交额 前一篇说到,用大盘指数,如恒生指数,上证,深证,这些重要的大盘指数来做Dataframe主键,那麽如果是同时拿一个板块股票的收市价和成交额,可以怎样操作呢 ...

  4. ES6 之 数组的扩展

    ES5 检测数组 let arr = [1,2,3,4] Array.isArray(arr) arr instanceof Array 转换方法 arr.toLocaleString() arr.t ...

  5. MySQL视图和事务

    视图的操作                                                                                                ...

  6. 总结一些常用的训练 GANs 的方法

    众所周知,GANs 的训练尤其困难,笔者自从跳入了 GANs 这个领域(坑),就一直在跟如何训练 GANs 做「对抗训练」,受启发于 ganhacks,并结合自己的经验记录总结了一些常用的训练 GAN ...

  7. linux文件软链接与硬链接

    1.命令格式: ln [参数][源文件或目录][目标文件或目录] 软链接只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间. 2.命令功能: Linux文件系统中,有所谓的链接(link),我们 ...

  8. C++构造函数概念作用

    作用: 对对象进行初始化,如给成员变量赋初值,而不用专门再写初始化函数. 防止有些对象没被初始化就使用,导致程序出错. 要求: 名字与类名相同,可以有参数,但不能有返回值(void也不行) 编译时: ...

  9. Eclipse Springboot项目Dokcer

    配置好Dockerfile FROM openjdk:8-jdk-alpine ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar ENTRYPOIN ...

  10. POJ-2561 Network Saboteur(DFS)

    题目: A university network is composed of N computers. System administrators gathered information on t ...