zoukankan      html  css  js  c++  java
  • poj 2385 Apple Catching

    Apple Catching
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11144   Accepted: 5415

    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.
    题意:一头牛在规定时间内在两棵苹果树间吃苹果,每一分钟其中一棵树都会掉一个苹果,牛起始位置在第一棵树,求在规定时间内最多能吃到的苹果数
    思路:dp[i][j]//i分钟时且总共往返j次能吃到的苹果的最大数
    j>0时:dp[i][j]=max{dp[i-1][j],dp[i-1][j-1]}+当前分钟能否吃到苹果;
                  max {牛没有移动,牛移动到了新树}+...
    j=0时,dp[i][j]=dp[i-1][j];
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int T_MAX = 1000;
    const int W_MAX = 30;
    int dp[T_MAX+1][W_MAX+1];//第i分钟往返j次能吃到的苹果的最大数
    int fall_app[T_MAX+1];
    int main() {
        int T, W;
        while (cin >> T >> W) {
            for (int i = 1;i <= T;i++)//分别对应着第i分钟在哪棵树掉苹果
                scanf("%d",&fall_app[i]);
            for (int i = 0;i <= W;i++) {
                dp[0][i] = 0;//0分钟及前规定没吃到苹果
                //cout << dp[0][i] << " ";
            }
            //cout << endl;
            for (int i = 1;i <= T;i++) {
                for (int j = 0;j <= W;j++) {
                    if (j == 0)dp[i][j] = dp[i - 1][j];//没往返过时的情况
                    else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]);
                    if (j & 1) {
                        if (fall_app[i] == 2)dp[i][j] += 1;
                    }
                    else
                        if (fall_app[i] == 1)dp[i][j] += 1;
                    //cout << dp[i][j]<<" ";
                }
                //cout << endl;
            }
            int max=0;
            for (int i = 0;i <= W;i++)
                if (max < dp[T][i])max = dp[T][i];
            cout << max << endl;
        }
        return 0;
    }
  • 相关阅读:
    ora12514
    telnet到虚拟机上的red hat linux失败——解决办法
    linux下监听的配置
    本机win7系统与虚拟机中的linux系统实现通讯
    ORA01078: failure in processing system parameters LRM00109: could not open parameter file '/oradata/oracle/112/dbs/in
    Xmanager无法登录Red Hat Linux——解决方法
    几种常见算法的介绍及复杂度分析(转)
    Linux学习笔记18cal显示日历
    Linux学习笔记14架设Apache http服务器
    Installing Oracle Database 10g on Linux
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/5838848.html
Copyright © 2011-2022 走看看