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 ...
随机推荐
- 带你探索关于飞机Wi-Fi服务的神奇科学
资料来源: Colin Anderson制片公司/ Getty图片社 在35000英尺的高空冲浪?哇哦,这当然是我们现在所期望的飞行方式了.根据2018年全球旅行者研究(2018 Global Tra ...
- Sqlserver 增删改查----删
--我们就以院系,班级,学生来举例. create TABLE [dbo].YuanXi ( Id ,) NOT NULL,--学校id 自增量 YuanXiName varchar() null, ...
- Spring Boot作为Spring Cloud基础设施
spring cloud包含的核心特性: Distributed/versioned configuration(分布式配置) Service registration and discovery(服 ...
- SQL基础之JDBC、连接池
JDBC JDBC四个核心对象 这几个类都是在java.sql包中 DriverManager(类): 数据库驱动管理类.这个类的作用:1)注册驱动; 2)创建java代码和数据库之间的连接,即获取C ...
- Java 创建对象的几种方式
转自https://www.cnblogs.com/wxd0108/p/5685817.html 作为Java开发者,我们每天创建很多对象,但我们通常使用依赖管理系统,比如Spring去创建对象.然而 ...
- 吴裕雄--天生自然JAVA线程编程笔记:进程与线程
- [Python Cookbook]Pandas: How to increase columns for DataFrame?Join/Concat
1. Combine Two Series series1=pd.Series([1,2,3],name='s1') series2=pd.Series([4,5,6],name='s2') df = ...
- POJ 1979:Red and Black
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26058 Accepted: 14139 D ...
- YouTube推出慈善组合工具,能引国内视频网站跟风吗?
互联网的出现不仅仅让大众的工作和生活更便利,更深度改变着传统事物的形态,让其被更多人广泛地认知并接触到.如,原本在线下通过彩页.手册.横幅等进行宣传.募捐的慈善,就通过互联网展现出更为强大的影响力.而 ...
- HttpClient4.x 上传文件
https://blog.csdn.net/wsdtq123/article/details/78888734