zoukankan      html  css  js  c++  java
  • poj2385 Apple Catching (线性dp)

    题目传送门

    Apple Catching 

    Apple Catching
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15444   Accepted: 7582

    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.

    Source

    题意:有两棵树,给你t分钟,每分钟有一棵树会掉落一颗苹果,再给你w个体力,每次从一棵树移动到另一棵树需要消耗1个体力,一开始你再第一棵树那里,问你t分钟后你最多能得到几个苹果?

    题解:定义dp[i][j]:前i分钟后移动j步得到的最多的苹果数

    那么这到第i分钟的状态就取决于前i-1分钟移动或者不移动得到的苹果树

    即dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);//注意边界

    还有怎么判断移动j步到哪棵树了呢,很简单,

    就是偶数步回到起点,奇数步到第二棵树

    代码:

    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #include<stdio.h>
    #include<queue>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int,int> PII;
    #define mod 1000000007
    #define pb push_back
    #define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    //head
    #define MAX 1005
    int dp[MAX][35];
    int a[MAX];
    int main()
    {
        int n,w;
        while(~scanf("%d %d",&n,&w)){
            for(int i=1;i<=n;i++)
                scanf("%d",&a[i]);
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<=w;j++)
                {
                    if(j>=1) dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
                    else dp[i][j]=dp[i-1][j];
                    if(j%2+1==a[i]) dp[i][j]++;
                }
            }
            int ans=0;
            for(int i=0;i<=w;i++)
                ans=max(ans,dp[n][i]);
            printf("%d
    ",ans);
        }
    
        return 0;
    }

    Apple Catching 

  • 相关阅读:
    将深度学习低延迟推理性能提高一倍
    常量特征
    数据库链接笔记
    html5页面调用手机打电话功能
    插件使用一表单验证一validation
    插件使用一树形插件---zTree一zTree异步加载
    插件使用一树形插件---zTree
    插件使用一进度条---nprogress
    插件使用一颜色选择器---cxColor
    插件使用一顶部消息提示---overhang
  • 原文地址:https://www.cnblogs.com/zhgyki/p/9876158.html
Copyright © 2011-2022 走看看