zoukankan      html  css  js  c++  java
  • Apple Catching POJ

    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.
     
    百度翻译:牛爱苹果是鲜为人知的事实。农夫约翰在他的田里种了两棵苹果树(很方便地编号为1和2),每棵树都长满了苹果。贝西够不到树上的苹果,所以她必须等它们掉下来。然而,她必须在空中抓住它们,因为当它们落地时苹果会碰伤(没有人愿意吃碰伤的苹果)。贝西吃得很快,所以她抓到的苹果只需几秒钟就可以吃了。每分钟,两棵苹果树中的一棵会掉一个苹果。贝西经常练习,如果她站在一棵树下,可以摘到一个苹果。虽然贝西可以在两棵树之间快速行走(不到一分钟),但她在任何时候都只能站在一棵树下。此外,奶牛没有得到大量的锻炼,所以她不愿意在树之间来回走动(因此错过了一些苹果)。苹果落下(每分钟一个)持续t(1<=t<=1000)分钟。贝西最多愿意来回走W(1<=W<=30)次。考虑到哪棵树每分钟会掉一个苹果,确定贝西能接住的苹果的最大数量。贝西从一号树开始。
     
    思路:dp[i][j] 表示第i分钟移动第j次所能得到的最大苹果数。
    状态转移方程:dp[i][j] = max(dp[i-1][j], dp[i-1][j-1])。表示第i分钟能得到的苹果数量,等于在第i-1分钟时,在树1和树2下得到苹果的最大值。j为偶数则在树1下面,奇数则在树2下面。
    然后判断当前是否在第i分钟掉苹果的那颗树下,是的话,dp[i][j]++。
     
     1 #include <cstdio>
     2 #include <fstream>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <deque>
     6 #include <vector>
     7 #include <queue>
     8 #include <string>
     9 #include <cstring>
    10 #include <map>
    11 #include <stack>
    12 #include <set>
    13 #include <sstream>
    14 #include <iostream>
    15 #define mod 1000000007
    16 #define ll long long
    17 using namespace std;
    18 
    19 int m,n;
    20 int dp[1005][35];
    21 int num[1005];
    22 int main()
    23 {
    24     while(scanf("%d %d",&n,&m)!=EOF)
    25     {
    26         for(int i=1;i<=n;i++)
    27         {
    28             scanf("%d",&num[i]);
    29         }
    30         if(num[1]==1)
    31         {
    32             dp[1][0]=1;
    33             dp[1][1]=0;
    34         }
    35         else
    36         {
    37             dp[1][0]=0;
    38             dp[1][1]=1;
    39         }
    40         for(int i=2;i<=n;i++)
    41         {
    42             for(int j=0;j<=m;j++)
    43             {
    44                 if(j==0)
    45                 {
    46                     dp[i][j]=dp[i-1][j]+num[i]%2;
    47                 }
    48                 else
    49                 {
    50                     dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
    51                     if(j%2+1==num[i])
    52                     {
    53                         dp[i][j]++;
    54                     }
    55                 }
    56             }
    57         }
    58         int ans=0;
    59         for(int i=0;i<=m;i++)
    60         {
    61             ans=max(ans,dp[n][i]);
    62         }
    63         printf("%d
    ",ans);
    64     }
    65 }
  • 相关阅读:
    json格式转换
    早该知道的7个JavaScript技巧
    SPFA加上SLF时判负环的条件
    HDU 4061 A Card Game
    线性筛法求素数
    STL之deque
    POJ 3219 二项式系数
    HDU 4296 Buildings
    HDU 4292 Food (成都赛区网络赛第五题,拆点网络流)
    拆点网络流(POJ3281)
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11275352.html
Copyright © 2011-2022 走看看