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. hihoCoder挑战赛14 A,B,C题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题目1 : 不等式 时间限制:10000ms 单点时限:1000ms 内存限制:2 ...

  2. [Leetcode][020] Valid Parentheses (Java)

    题目在这里: https://leetcode.com/problems/valid-parentheses/ [标签]Stack; String [个人分析]这个题应该算是Stack的经典应用.先进 ...

  3. python 登陆一个网站

    今天想用python写一个登陆的脚本,搜了一下,网上挺多的,看了一些后写了个登陆虎扑论坛的脚本. 原理: 只要在发送http请求时,带上含有正常登陆的cookie就可以了. 1.首先我们要先了解coo ...

  4. How can I save HICON to an .ico file

    refer:http://stackoverflow.com/questions/2289894/how-can-i-save-hicon-to-an-ico-file answer1: #inclu ...

  5. dede分页

    {dede:list pagesize ='} <div class="intro clearfix"> <ul class="pic"> ...

  6. HTML&CSS基础学习笔记1.9-添加图片

    <img>标签是用来添加图片的~ <img>标签的使用方法:<img src="图片的地址"> 先来看段实例代码: <!DOCTYPE h ...

  7. .cshrc

    使用set和setenv命令可以设置shell选项或者列出shell变量 在C Shell 里, set 定义局部变量, setenv定义全局变量   1.set   set 变量名 = 内容: s2 ...

  8. Unity 3D中的菜单项

    1.File菜单:主要是包含项目和场景的创建.保存以及输出等功能.2.Edit(编辑)菜单:只要包括对场景进行一系列的编辑以及环境设置操作等命令.3.Assets(资源)菜单:掌握资源在Unity中的 ...

  9. 关于ActionBar的向下兼容

    1. Create a blank Android Project创建一个空的Android项目.a. 导入ActionBarCompat工程.ActionBarCompat的source code位 ...

  10. WPF - 使用Microsoft.Win32.OpenFileDialog打开文件,使用Microsoft.Win32.SaveFileDialog将文件另存

    1. WPF 使用这个方法打开文件,很方便,而且可以记住上次打开的路径. Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.W ...