POJ 题目3661 Running(区间DP)

Running

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5652   Accepted: 2128

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

5 2
5
3
4
2
10

Sample Output

9

Source

USACO 2008 January Silver

题意:给你n,m,后边n个数,一个人第i分钟可以移动a[i]长度,每分钟会多1点疲劳,疲劳值不能超过m,到m必须休息,每个时间点都可以休息,每次休息就必须休息到疲劳值为0,问时间消耗m分钟后,疲劳为0走的最大长度

ac代码

#include<stdio.h>
#include<string.h>
#define min(a,b) (a>b?a:b)
int dp[10010][505],a[10010];
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,j,k;
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				dp[i][j]=dp[i-1][j-1]+a[i];
			}
			dp[i][0]=dp[i-1][0];
			for(k=1;k<=m;k++)
			{
				if(i-k>=0)
					dp[i][0]=min(dp[i][0],dp[i-k][k]);
			}
		}
		printf("%d\n",dp[n][0]);
	}
}

  

时间: 2024-08-09 05:16:49

POJ 题目3661 Running(区间DP)的相关文章

POJ 1141 Brackets Sequence (区间dp 记录路径)

题目大意: 给出一种不合法的括号序列,要求构造出一种合法的序列,使得填充的括号最少. 思路分析: 如果只要求输出最少的匹配括号的数量,那么就是简单的区间dp dp[i][j]表示 i - j 之间已经合法了最少添加的括号数. 转移 就是 dp[i] [j] = min  (dp[i+1][j]+1 , dp[ i+ 1] [ k -1 ] + dp[k+1] [j] (i k 位置的括号匹配)) 其次我们要记录路径,你发现  如果 dp [i] [j] 是由 dp [i+1] [j] 转移过来的

poj 1141 Brackets Sequence (区间DP)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25893   Accepted: 7295   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

uva1626 poj 1141 Brackets Sequence 区间dp 打印路径

// poj 1141 Brackets Sequence // 也是在紫书上看的一题,uva就是多了一个t组数据. // 经典区间dp // dp(i,j)表示区间[i,j]内所需要增加的括号数目 // 则分为两种情况 // 一种是s[i]和s[j]是匹配的则 // dp[i][j] = min(dp[i][j],dp[i+1][j-1]) // 另外一种情况是不匹配 // dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]){i<k<j}; // 但是无

poj(2955)——Brackets(区间dp)

题意: 现在我们定义一种R串,它必须满足以下条件: 1)当它的字串是空的时候时,那么它是R串. 2)当它是R串时,那么(s)或是[s]也是R串. 3)当a和b都是R串时,那么ab也是R串. 这里我没有完全领悟题目的意思,所以我发现递推不过去.其实它的实质就是括号匹配. 也就是说这里的合法序列是指括号能够两两匹配的. if((a[s]=='('&&a[e]==')')||(a[s]=='['&&a[e]==']')) 这里的这种情况时当外围是相互匹配的时候. dp[s][e]

POJ 2955 Brackets (区间dp入门)

Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are

POJ 3280 Cheapest Palindrome ( 区间DP &amp;&amp; 经典模型 )

题意 : 给出一个由 n 中字母组成的长度为 m 的串,给出 n 种字母添加和删除花费的代价,求让给出的串变成回文串的代价. 分析 :  原始模型 ==> 题意和本题差不多,有添和删但是并无代价之分,要求最少操作几次才能是其变成回文串 ① 其实细想之后就会发现如果没有代价之分的话删除和增添其实是一样的,那么除了原串原本拥有的最长回文串不用进行考虑处理,其他都需要进行删除或者增添来使得原串变成回文串,所以只要对原串 S 和其反向串 S' 找出两者的最长公共子串长度 L 再用总长减去 L 即可 ②

POJ - 2955 括号 (区间DP)

分析:区间DP的典型题,设dp[i][j]为i到j的最大匹配数 依次从小到大的区间进行更新 如果a[i]==a[j]那么产生新的匹配,dp[i][j]=max(dp[i][j],dp[i+1][j-1]+1) 再依次枚举断点从原先得到的匹配区间中转移,找最大值 dp[i][j]=max(dp[i][j],dp[i][i+k]+dp[i+k+1][j]); 代码如下: #include <cstdio> #include <iostream> #include <algorit

POJ 3661:Running 区间DP

Running 题目链接: http://poj.org/problem?id=3661 题意: 有一只牛在跑步,在第 i 分钟它可以跑Ni米,疲惫值加1(疲惫值初始为0),或者休息一分钟,疲惫值减1(只有疲惫值到了0才能重新开始跑步),当匹配值达到M时它必须休息,且第N分钟结束时牛的疲惫值必须为0,求这只牛最远可以跑几米. 题解: 设dp[i][j]在 i 分钟里疲惫值限制为j的情况下牛可以跑的最远距离.一般情况下这样设就行了,但是因为题目限制只要开始休息就必须休息到疲惫值为0,所以这里将dp

poj 2955 Brackets【区间DP】

题目链接:http://poj.org/problem?id=2955 题意:求回文子串的最大长度. 解法:枚举区间长度,更新答案. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include