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


  • 相关阅读:
    调试代码的技巧
    关联Lable和输入项
    如何更有效的利用自己的时间
    高效能人士的七个习惯>读书笔记之二
    .NET别名机制简介
    VS2005升SP1错误1718文件FileName被数字签名策略拒绝
    程序物语(三):做人、做事、生活
    程序物语(二):起手式
    SQL Server 2008如何压缩日志(log)文件?
    prototype中文参数乱码解决方案
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203979.html
Copyright © 2020-2023  润新知