J - 简单dp

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.

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

* Line 1: Two space separated integers: T and W

* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.

Output

* Line 1: The maximum number of apples Bessie can catch without walking more than W times.

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

Hint

INPUT DETAILS:

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.

题目大意:有两课苹果树,编号为1和2,每一分钟都会有苹果从其中之一落下,刚开始的时候人在苹果树1的下面,这个人最多可以移动w
次,问最终这个人最多可以拿到多少苹果。
思路分析:本问题中,总共有三个变量,一个是当前是第几分钟(i),另一个是已经移动了多少次(j),除此之外,还有这个人当前处在哪一棵苹果树
下,但是我们发现,这个人当前在哪一棵苹果树是可以通过移动的次数推算出来的,也就是说现在的变量只有i,j,我们最后要求的就是dp[T][j]的最大值
同时决策就是在当前分钟要不要移动,决策就会引起状态的转移,dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]),同时对每一步要进行判断,如果当前所在的
苹果树在i分钟刚好掉落苹果,那么dp[i][j]++;
代码:
#include <iostream>
#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的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  3. 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 ...

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  6. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  7. 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 ...

  8. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

  9. poj1189 简单dp

    http://poj.org/problem?id=1189 Description 有一个三角形木板,竖直立放.上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周 ...

随机推荐

  1. mysql 查询大量数据内存溢出

    使用非buffer 的sql 查询 比如pymysql 的 pymysql.cursor.SSCursion

  2. Kafka笔记--使用ubuntu为bocker(服务器)windows做producer和comsumer(客户端)

    原文连接:http://www.cnblogs.com/davidwang456/p/4201875.html 程序仍然使用之前的一篇博文中的例子 :http://www.cnblogs.com/gn ...

  3. Codeforces 573B Bear and Blocks

    http://codeforces.com/problemset/problem/573/B  题目大意: 给出n个连续塔,每个塔有高度hi,每次取走最外层的块,问需要多少次操作能够拿光所有的块. 思 ...

  4. 在O(1) 时间删除链表节点

    struct Node { int val; Node * next; }; void deleteNode(Node ** head, Node * target) { assert(head != ...

  5. ISO7816通讯协议在工控主板EM9160中的实现方案

    在新的国家电网智能终端相关标准中,规定了通过专门的加密芯片来保证设备数据安全性的方法,而设备主控单元与加密芯片采用了广泛应用的ISO7816通讯协议.工控主板EM9160为了适应这一新的技术需求,对其 ...

  6. linux vmstat使用说明

    FIELD DESCRIPTION FOR VM MODE Procs r: The number of processes waiting for run time. 表示运行队列,就是说多少个进程 ...

  7. bzoj1178

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1178 看ppt http://wenku.baidu.com/link?url=dJv6LNm ...

  8. 黑马程序员_Java面向对象_包

    7.包 7.1包(package) 对类文件进行分类管理. 给类提供多层命名空间. 写在程序文件的第一行. 类名的全称是:包名.类名. 包也是一种封装形式. 利用命令行自动生成文件夹格式:D:\jav ...

  9. 【剑指offer】面试题42:翻转单词顺序 VS 左旋转字符串

    题目: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”abcX ...

  10. sublime text 2相关

    官网:http://www.sublimetext.com/2 安装包控制(Package Control) 打开Sublime Text 2,按快捷键 ctrl+` 或者点击 Tools → Com ...