Problem Description
ZZZ is an enthusiastic ACMer and he spends lots of time on training. He always stays up late for training. He needs enough time to sleep, and hates skipping classes. So he always sleeps in the class. With the final exams coming, he has to spare some time to
listen to the teacher. Today, he hears that the teacher will have a revision class. The class is N (1 <= N <= 1000) minutes long. If ZZZ listens to the teacher in the i-th minute, he can get Ai points (1<=Ai<=1000). If he starts listening, he will listen to
the teacher at least L (1 <= L <= N) minutes consecutively. It`s the most important that he must have at least M (1 <= M <= N) minutes for sleeping (the M minutes needn`t be consecutive). Suppose ZZZ knows the points he can get in every minute. Now help ZZZ
to compute the maximal points he can get.
 

Input
The input contains several cases. The first line of each case contains three integers N, M, L mentioned in the description. The second line follows N integers separated by spaces. The i-th integer Ai means there are Ai points in the i-th minute.
 

Output
For each test case, output an integer, indicating the maximal points ZZZ can get.
 

Sample Input

10 3 3
1 2 3 4 5 6 7 8 9 10
 

Sample Output

49
 
题意:一节课有n分钟,Z每听一分钟课都能得到那分钟对应的分数,一旦她开始听课就必须至少连续听L分钟,但是她每节课都要有m分钟的睡眠时间(这m分钟可以不连续),问在睡眠时间不少于m的条件下 她一节课最多能得多少分。
思路:容易想出dp状态,dp[i][j]表示前i分钟睡j分钟最多能得的分数。那么对第i分钟进行讨论,如果第i分钟睡觉的话,那么dp[i][j]=dp[i-1][j-1];如果第i分钟听课的话,那么[i-l,i]分钟一定是听课的,所以dp[i][j]=max(dp[k][j]+sum[i]-sum[k]).(k>=j && k<=i-L) (注意:这里k不能直接用i-L带进去,而不是枚举1~i-L,错误地认为dp[i][j]=dp[i-L][j]+sum[i]-sum[k],因为dp[i-L][j]的本质含义是在满足如果i-L这一分钟听课,那么这之前L分钟都在听课,但是这里状态转移的时候并不一定需要满足条件,比如i-L,i-L-1这两分钟可以和[i-L+1,i]合并,然后再加上dp[i-L-2],这个状态就不再dp[i-L][j]中)。状态方程写完后我们会发现时间复杂度不足,这里要再开一个dp_tmp数组,用dp_tmp[i][j]来优化max(dp[k][j]+sum[i]-sum[k]),即dp_tmp[k][j]=max(dp[1][j]+v[2]+...+v[k],dp[2][j]+sum[3]+...+sum[k],...dp[k][j]
)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
#define maxn 1005
#define inf 999999999
int v[maxn],sum[maxn];
int dp[maxn][maxn],dp_tmp[maxn][maxn]; int main()
{
int n,m,l,i,j,k;
while(scanf("%d%d%d",&n,&m,&l)!=EOF)
{
sum[0]=0;
for(i=1;i<=n;i++){
scanf("%d",&v[i]);
sum[i]=sum[i-1]+v[i];
}
for(i=0;i<=n;i++){
for(j=0;j<=m && j<=i;j++){
dp[i][j]=0;
dp_tmp[i][j]=0;
}
}
for(i=0;i<=n;i++){
if(i>=l)dp[i][0]=sum[i];
dp[i][i]=0;
}
for(i=1;i<=n;i++){
for(j=1;j<=i && j<=m;j++){
dp[i][j]=max(dp[i][j],dp[i-1][j-1] ); //第i分钟睡
if(i-l>=j){
dp[i][j]=max(dp[i][j],dp_tmp[i-l][j]+sum[i]-sum[i-l] );
}
if(i-l>=j) //这里是为算i+1做铺垫,算出dp_tmp[i+1-l][j]
dp_tmp[i+1-l][j]=max(dp[i+1-l][j],dp_tmp[i-l][j]+v[i+1-l] );
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}

hdu3905 Sleeping (区间dp)的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. win7安装oracle11g和oracle client和pl/sql

    一.安装oracle11g 1.下载Oracle 11g R2 for Windows的版本 下载地址:hhttps://www.oracle.com/technetwork/database/ent ...

  2. C语言实现蛇形矩阵

    今天大一考试C语言的时候看见了这道题,下面是我转载的一个大佬的博客,自认为分析的很清楚,特来分享一下. **原文地址: https://blog.csdn.net/jack22333/article/ ...

  3. Azure Key Valut 简介

    Azure Key Vault(密钥库)是用于安全地存储和访问Secret的云服务,Secret是需要严格控制访问权限的内容,例如API密钥,密码,证书或加密密钥.Key Vault Service支 ...

  4. 彻底搞懂MySQL为什么要使用B+树索引

    目录 MySQL的存储结构 表存储结构 B+树索引结构 B+树页节点结构 为什么要用B+树索引 二叉树 多叉树 B树 B+树 搞懂这个问题之前,我们首先来看一下,MySQL表的存储结构 MySQL的存 ...

  5. MySQL全面瓦解19:游标相关

    定义 我们经常会遇到这样的一种情况,需要对我们查询的结果进行遍历操作,并对遍历到的每一条数据进行处理,这时候就会使用到游标. 所以:游标(Cursor)是处理数据的一种存储在MySQL服务器上的数据库 ...

  6. 前端知识(一)02 初识 Node.js-谷粒学院

    目录 初识Node.js 一.Node.js的概念 1.JavaScript引擎 2.什么是Node.js 3.Node.js有什么用 二.BFF 1.BFF 解决什么问题 2.BFF是什么 三.安装 ...

  7. ThreadLocal 原理分析

    用法 ThreadLocal<String> threadLocal = new ThreadLocal<>(); // 无初始值 ThreadLocal<String& ...

  8. Angular入门到精通系列教程(14)- Angular 编译打包 & Docker发布

    目录 1. 概要 2. 编译打包 2.1. 基本打包命令 2.2. 打包部署到二级目录 3. Angular站点的发布 3.1. web服务器发布 3.2. 使用docker发布 4. 总结 环境: ...

  9. hive报错:Failed with exception java.io.IOException: rename for src path:

    在hive中,会有这样一种情形: 1.创建一个分区外部表A(比如A表有5个字段),并且向A表里指定的分区(比如20160928这个分区)里插入数据 2.发现A表缺少一些字段,因为存在元数据不实时更新的 ...

  10. Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79530732

    Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79 ...