习题2.3:C. 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

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.

题意:

  奶牛爱吃苹果是鲜为人知的事实。FJ在他的田地里有两棵苹果树(编号为1和2)。Bessie不会爬树,所以她必须等着苹果掉下来。然而,她必须在空中接住它们,因为苹果落地时碰伤了(谁也不想吃碰伤了的苹果)。Bessie吃得很快,吃的时间可以忽略不计。 每分钟,两棵苹果树中的一棵会掉落一个苹果。Bessie做过很多练习,只要她站在一棵有苹果落下的树下,就能接住一个苹果。Bessie可以很快地在两棵树之间行走,时间忽略不计。她可以随时站在一棵树下。此外,奶牛没有得到大量的锻炼,所以她不愿意在树间来回地来回行走(因此可能错过了一些苹果)。 苹果下降时间T(1 < = T< = 1000)分钟。Bessie最多只愿意来回走W(1 < = W< = 30)次。考虑哪棵树每分钟会掉落一个苹果,求贝西能抓到的最大数量的苹果。从树1开始。

题解:

#include
#include
using namespace std;
#define MAX_T 1002
#define MAX_W 32
int app[MAX_T];   
int dp[MAX_T][MAX_W];
int T,W;
//用于判断第i分钟奶牛j次轮换后站的位置是否落下苹果 
bool judge(int i,int j)
{
    return(j%2+1==app[i]);
}
int max(int a,int b)
{
    int k = a=0) return dp[i][j];
    int res;
    if(i==T)
        res = 0;  //这一步设置出口要学会,特别重要
    else if(j>0&&judge(i,W-j))
        res = rec(i+1,j)+1;
    else if(j>0&&!judge(i,W-j))
        res = max(rec(i+1,j),rec(i+1,j-1)+1);
    else if(j==0&&judge(i,W-j))
        res = rec(i+1,0) + 1;
    else
        res = rec(i+1,0);
    return dp[i][j] = res;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    while(cin>>T>>W)
    {
        //输入 
        for(int t=0;t> app[t];
        cout << rec(0,W) << endl;
    }
}

与01背包问题类似,用参数逐渐增加的递归,注意设置出口,if(i==T) res = 0;这一步特别关键。

你可能感兴趣的:(习题2.3:C. Apple Catching)