• 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;
    }


  • 相关阅读:
    什么是递归神经网络
    2020年蜂窝网络连接超过110亿台设备
    伊朗Cisco路由器遭黑客攻击 全国互联网几乎瘫痪
    看了才知道!伊朗黑客组织原来这么牛
    美国知名Cloudflare网络公司遭中国顶尖黑客攻击
    如何对Web服务器进行飓风级防御
    美国的科技公司是如何使用加密的DNS
    揭秘网络黑客常见的6种必用攻击手段
    物联网是什么?您的家用电器从黑客手中安全吗
    不可不知!设置什么密码才不易被黑客破解
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203979.html
Copyright © 2020-2023  润新知