poj-3661 Running(DP)
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)的更多相关文章
- poj 3661 Running(区间dp)
Description The cows are trying to become better athletes, so Bessie ≤ N ≤ ,) minutes. During each m ...
- POJ 3034 Whac-a-Mole(DP)
题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在( ...
- poj 2229 Sumsets(dp)
Sumsets Time Limit : 4000/2000ms (Java/Other) Memory Limit : 400000/200000K (Java/Other) Total Sub ...
- poj -2229 Sumsets (dp)
http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...
- poj 3230 Travel(dp)
Description One traveler travels among cities. He has to pay for this while he can get some incomes. ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- POJ题目分类(转)
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
随机推荐
- 寒假day24
数据挖掘得继续深入,人物画像需要进行更多层次的分析
- Ubuntu16.04安装配置Caffe教程(GPU版)
推荐博客:https://www.linuxidc.com/Linux/2017-11/148629.htmhttps://blog.csdn.net/yggaoeecs/article/detail ...
- Linux|Zookeeper--CentOS7开机启动Zookeeper
参考 https://www.cnblogs.com/zhangmingcheng/p/7455278.html 在 /etc/rc.d/init.d 下创建zookeeper脚本 #!/bin/ba ...
- 82.常用的返回QuerySet对象的方法使用详解:all,select_related
1. all: 返回这个ORM模型的QuerySet对象. articles = Article.objects.all() print(articles) 2.select_related: 查找数 ...
- 学生信息的添加 Java web简单项目初试(失败)
题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...
- 关于域名转发proxy_pass
在配置nginx的时候,有一个需求,访问m.XXX.com的时候,需要实际访问www.YYY.com/m,并且域名不能发生变化. 达成这个需求有两种做法: 第一种就是301跳转,使用rewrite来跳 ...
- centos6-7 yum安装php的方法
1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包,先删除他们 yum remove php.x86_64 php-cli.x86_64 ph ...
- dac mssql server
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- 使用Dom4j生成xml文件(utf-8编码)
xml文件内容: <?xml version="1.0" encoding="UTF-8"?> <result> <code> ...
- Java中static变量作用和用法详解
static表示"全局"或者"静态"的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static ...