POJ 2385 Apple Catching

来源:互联网 发布:怎么样禁止软件联网 编辑:程序博客网 时间:2024/05/16 19:38

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

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.

大意

有两棵树(1,2),一头牛一开始在1树下,
给你一个N 代表有N分钟,每一分钟都有一个苹果落下
W 最多代表移动 W步
其他数代表从哪棵树落下,
根据题意,我们容易得出转移方程
f[i][j]=max(f[i-1][j],f[i-1][j-1])
f[i][j]代表前i分钟,走了j步,收了几个苹果
要特殊处理一下j==0的情况
因为 数组负下标有时会出问题

输入可以用一维数组储存苹果掉落情况
我用的二维数组a[3][N]代表第i分钟 1树或 2树是否掉落苹果
如果第i分钟 1树掉落苹果 那么a[1][i]=1;

代码

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include"algorithm"#define MAXN 1010using namespace std;int f[MAXN][31];//这里31 开成30 结果WA了千百遍(~~~~(>_<)~~~~)                //我开始怀疑自己的智商了int a[3][MAXN];int n,w;inline void read(int&x) {    x=0;char c=getchar();    while(c>'9'||c<'0') c=getchar();    while(c>='0'&&c<='9') x=10*x+c-48,c=getchar();}int main() {    while(scanf("%d%d",&n,&w)==2) {        memset(f,0,sizeof(f));        memset(a,0,sizeof(a));        int x;        for(int i=1;i<=n;i++) {            read(x);            if(x==1) a[1][i]=1;            else a[2][i]=1;        }        for(int i=1;i<=n;i++)        for(int j=0;j<=w&&j<=i;j++) {            if(j==0)  f[i][j]=f[i-1][j]+a[1][i];            else f[i][j]=max(f[i-1][j],f[i-1][j-1])+a[j%2+1][i];        }        printf("%d\n",f[n][w]);    }    return 0;}
0 0
原创粉丝点击