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

    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

    dp[i][j]表示第i分钟时移动j次得到的最大值,则dp[i][j]=max(dp[i-1][j]+c,dp[i-1][j-1]+c),其中c表示第i分钟时此树是否有苹果落下,有c=1,没有c=0。空间还可以优化,减少一维

    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    
    int main()
    {
        int t, w, x, f[40];
        while (cin >> t >> w)
        {
            int ans = 0;
            memset(f, -INF, sizeof(f));
            f[0] = 0;
            while (t--)
            {
                cin >> x;
                x--;
                for (int i = 0; i <= w; ++i)
                {
                    f[i] = f[i] + (x == i%2);
                    if (i)
                        f[i] = max(f[i], f[i-1]+(x==i%2));
                    ans = max(ans, f[i]);
                }
            }
            cout << ans << endl;
        }
        return 0;
    }


  • 相关阅读:
    Nginx配置文件nginx.conf详解
    Nginx的内部(进程)模型
    Nginx特点
    Nginx的事件处理机制
    8 个实用的 Bootstrap 3 案例教程
    超高速前端开发工具——Emmet
    3ds MaxVRay全套家装效果图制作典型实例第2版
    Word Excel PPT 2016完全自学教程
    Unity 5.X 3D游戏开发技术详解与典型案例
    C#从入门到精通(第2版)
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203979.html
Copyright © 2011-2022 走看看