• POJ 2385 Apple Catching(简单DP)


    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.
    题意:
      奶牛爱吃苹果是鲜为人知的事实。FJ在他的田地里有两棵苹果树(编号为1和2)。Bessie不会爬树,所以她必须等着苹果掉下来。然而,她必须在空中接住它们,因为苹果落地时碰伤了(谁也不想吃碰伤了的苹果)。Bessie吃得很快,吃的时间可以忽略不计。 每分钟,两棵苹果树中的一棵会掉落一个苹果。Bessie做过很多练习,只要她站在一棵有苹果落下的树下,就能接住一个苹果。Bessie可以很快地在两棵树之间行走,时间忽略不计。她可以随时站在一棵树下。此外,奶牛没有得到大量的锻炼,所以她不愿意在树间来回地来回行走(因此可能错过了一些苹果)。 苹果下降时间T(1 < = T< = 1000)分钟。Bessie最多只愿意来回走W(1 < = W< = 30)次。考虑哪棵树每分钟会掉落一个苹果,求贝西能抓到的最大数量的苹果。从树1开始。
    题解: 
      基础的动态规划。dp[i][j]表示第i分钟最多转换j次能得到苹果的最大数量。转换次数j为偶数时,肯定是在第1号树下,为奇数时,在第2号树下。
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int dp[3005][35],t[3005];
    int main()
    {
        int n,w;
        cin>>n>>w;
        for(int i=1;i<=n;i++)
            cin>>t[i],t[i]--;
        for(int i=1;i<=n;i++)
            for(int j=0;j<=w;j++)
                if(j%2)
                {
                        dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+t[i];
                }
                else
                {
                    if(j)//加判断,防止j==0时,dp[i-1][j-1]非法访问内存
                        dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+!t[i];
                    else
                        dp[i][j]=dp[i-1][j]+!t[i];
                }
        cout<<dp[n][w]<<endl;
        return 0;
    }
  • 相关阅读:
    我劝你不要记账
    富人不存钱
    1w+的心路历程
    简历这么写才对!
    Android 开发凉了吗!
    三个思维转变
    说一说赚钱
    【原】Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法(Matt Galloway著)读书笔记(二)
    【原】Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法(Matt Galloway著)读书笔记(一)
    【原】iOS开发进阶(唐巧)读书笔记(二)
  • 原文地址:https://www.cnblogs.com/orion7/p/7498127.html
Copyright © 2020-2023  润新知