poj2385 简单DP
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).
Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.
Input
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Output
Sample Input
7 2
2
1
1
2
2
1
1
Sample Output
6
Hint
Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.
OUTPUT DETAILS:
Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn1=1000+10;
const int maxn2=30+5;
int dp[maxn1][maxn2];//dp[i][j]代表的是前imin,移动j次可以捡到的最大苹果数
int num[maxn1];
const int inf=0xffffff;
int main()
{
int t,w;
while(scanf("%d%d",&t,&w)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=t;i++)
scanf("%d",&num[i]);
if(num[1]==1) dp[1][0]=1,dp[1][1]=0;
else dp[1][0]=0,dp[1][1]=1;
for(int i=2;i<=t;i++)
{
for(int j=0;j<=w;j++)
{
if(j==0) dp[i][j]=dp[i-1][j]+(num[i]==1);
else
{
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
if(j%2+1==num[i]) dp[i][j]++;
}
}
}
int ma=-inf;
for(int j=0;j<=w;j++)
if(dp[t][j]>ma) ma=dp[t][j];
cout<<ma<<endl;
}
return 0;
}
poj2385 简单DP的更多相关文章
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
- hdu 2471 简单DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=( dp[n-1][m],dp[n][m-1],d[i][k ...
- Codeforces 41D Pawn 简单dp
题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...
- poj1189 简单dp
http://poj.org/problem?id=1189 Description 有一个三角形木板,竖直立放.上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周 ...
随机推荐
- [Leetcode][048] Rotate Image 略详细 (Java)
题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...
- uva 10125 - Sumsets
题意: 输入n,然后输入n个数字,,要在这n个数字中找出a,b,c,d..满足a,b,c,d是不同元素,并且a + b + c = d...求出最大的d 直接暴力时间复杂度为O(n^4)..会超时.. ...
- Hadoop学习历程(五、真正的分布式系统搭建)
之前都是单节点进行的操作与测试,现在进行真正的多节点系统搭建 1. 准备系统与配置 共准备4台机器搭建Hadoop集群.基于CentOS6.2,jdk1.6.0_31,Hadoop2.2.0版本 19 ...
- swift闭包-备
我给Swift 中的闭包一个定义:闭包是自包含的匿名函数代码块,可以作为表达式.函数参数和函数返回值,闭包表达式的运算结果是一种函数类型. Swift中的闭包类似于Objective-C中的代码块.J ...
- cf C. Fixing Typos
http://codeforces.com/contest/363/problem/C s2用于存处理之后的字符串,再遍历s1的时候,s2会有两种情况1.s2最后两个字符是相同的如xx,如果这时再遇到 ...
- cf C. Dima and Containers
http://codeforces.com/contest/358/problem/C 将最大的放在stack里面,第二大的放在queue中,第三大的放在deck里面.然后模拟就可以了. #inclu ...
- emacs vim IDE
原本想花点时间来学习下Vim或者emacs,结果在网上搜索到这篇文章 骂战挺多的,但是也长见识 http://bbs.csdn.net/topics/390306165 下面是windows下的ema ...
- Python闭包与函数对象
1. Python闭包是什么 在python中有函数闭包的概念,这个概念是什么意思呢,查看Wikipedia的说明如下: “ In programming languages, closures (a ...
- java算法之身份证号码验证
调用时直接 new IDCard().verify(身份证id);就可以了 实现代码如下: public class IDCard { private String _codeError; //wi ...
- 基于PCA的特征提取
图像处理方面的知识也学了一段时间了,总是光看理论的话,感觉联系不上实际,第一次把理论综合的实现出来,对这些理论的印象才感觉的更深刻,也能够为后续的学习打下良好的基础. PCA是比较老的算法,但是可靠性 ...