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

  1. 7 2
  2. 2
  3. 1
  4. 1
  5. 2
  6. 2
  7. 1
  8. 1

Sample Output

  1. 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. USB OTG to PC USB API简介

    本API分为四部分:Linux或Android内核 (主要是gadget驱动).linux端API及其DEMO.Windows 驱动.Windows API及其Demo. 一.linux.Androi ...

  2. Android-transulcent-status-bar

    最近业务上看到一个设计图挺好看,所以研究了一下透明状态栏,注意不是沉浸式状态栏,在参考了网上的一些资料后,整理出了这篇博客. Github Demo 链接: StatusBarCompat 参考文章: ...

  3. js大文件分割上传

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  4. pcduino通过sd卡刷系统

    1.登录到pcduino的官网,下载相应的文件. 下载第一个kernel和后面那个ubuntu. 2.将SD卡插入到电脑上,运行下面这个软件: 那个盘符就是你的SD卡的盘符,选择卡量产,镜像文件选上面 ...

  5. 一品楼论坛www.ep6.info一品楼论坛

    一品楼论坛最新地址www.ep6.info>访问一品楼网站. 一品楼是现在比较大的信息分享平台,一品楼上网必进. 一品楼江苏版块,一品楼北京版块,一品楼怡红院,一品楼怡春院. 一品楼山东信息. ...

  6. 将主机IDS OSSEC日志文件存入MYSQL的方法

    将主机IDS OSSEC日志文件存入MYSQL的方法 http://www.freebuf.com/articles/system/6139.html http://ossec-docs.readth ...

  7. Qt多国语言QT_TR_NOOP和QT_TRANSLATE_NOOP

    文章来源:http://devbean.blog.51cto.com/448512/245063/ 在代码中,我们使用tr()将需要翻译的字符串标记出来.lupdate工具就是提取出tr()函数中的相 ...

  8. 解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)——ubuntu环境设置默认是utf-8,文件编码可使用Encodersoft批量转换

    一.Qt环境设置 文件从window上传到Ubuntu后会显示乱码,原因是因为ubuntu环境设置默认是utf-8,Windows默认都是GBK.Windows环境下,Qt Creator,菜单-&g ...

  9. Qt编程之实现属性窗口编辑器

    类似于这种: 就是右下角这个框,有属性名字和对应的value编辑. 这个Widget是作为一个QDockWidget被添加到QMainWindow中的.QMainWindow必须要有centralWi ...

  10. 关于老驱动不能在windows 8下正常安装的问题

    问题: 老驱动(WDF),能在windows 7下安装并工作,但是不能在windows 8下安装. 先了解下windows 8驱动的新东西吧: New for windows 8 http://msd ...