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. [安洵杯 2019]easy_web

    0x00 知识点 md5强类型的绕过 方法比较固定: POST: a=%4d%c9%68%ff%0e%e3%5c%20%95%72%d4%77%7b%72%15%87%d3%6f%a7%b2%1b%d ...

  2. axios基础介绍

    axios基础介绍 get请求要在params中定义,post要在data中定义.

  3. psp --2

    PSP0 ---2 项目计划日志 姓名:赵腾                                                日期:9/12/2017 任务 日期 听课 编写程序 阅读课 ...

  4. Tensorflow学习教程------普通神经网络对mnist数据集分类

    首先是不含隐层的神经网络, 输入层是784个神经元 输出层是10个神经元 代码如下 #coding:utf-8 import tensorflow as tf from tensorflow.exam ...

  5. 18 12 29 css background

    background属性 属性解释 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个 ...

  6. VBA代码优化及其他设置操作

    一.代码优化的一些方法 尽量减少在循环中遍历调用对象,公式计算 (操作VBA代码若出现屏幕闪屏,会拖慢运行速度),可以禁止屏幕闪屏.多用在操作工作表/薄,单元格的时候. Application.Scr ...

  7. python的debug神器PySnooper

    同事给我推荐了这个调试神器,一直没工夫看,今天看了下. 原文链接: 史上最方便的Python Debug工具|腾讯技术说 体验了下,感觉最好的用法:1.优先逐行调试:2.一些复杂状态处理或者偶现的bu ...

  8. Cracking Digital VLSI Verification Interview 第一章

    目录 Digital Logic Design Number Systems, Arithmetic and Codes Basic Gates Combinational Logic Circuit ...

  9. Exchange 2016 CU12安装报错

    1.         报错信息: Exchange 2016 升级 CU12补丁报错,主要是在进行第10步安装管理工具时报SeSecurityPrivilega错误,详细如下: 查看安装log信息如下 ...

  10. go语言小练习——给定英语文章统计单词数量

    给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出.思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下: package main import ( " ...