zoukankan      html  css  js  c++  java
  • poj 2385 Apple Catching

    Apple Catching
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7108   Accepted: 3469

    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.
    ========================================================================
    题目大意:很少有人知道母牛也爱吃苹果,John农场里有一只母牛Bessie,和两棵苹果树。Bessie不吃摔烂的苹果所以只能在空中接住苹果来吃。
    两棵苹果树每分钟掉落一个苹果,一共掉落T分钟,T个苹果,两棵苹果树不远,Bessie一开始在树1下,如果另一棵树掉苹果,Bessie能赶过去接住。
    但是Bessie体力有限,在两棵之间的奔波有次数限制W。提供T、W和每分钟掉苹果的树的编号,问Bessie最多接到多少苹果。

    解题思路:就一组数据,猜也是DP了。
    既然是DP就要打状态表,横轴是跑动的次数,纵轴掉落是第多少个苹果,状态值是到掉落第n个苹果时跑动 i 次能接到的最多苹果数。
    更新行时,跑动次数为0的状态值可以按照哪棵树掉苹果直接填写,跑动次数为 j 的状态值 dp[ i ][ j ] = max( dp[ i-1 ][ j-1 ] , dp[ i-1 ][ j ] )+1。
     
     1 //dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + 1。
     2 #include <iostream>
     3 #include <cstring>
     4 #include <string>
     5 #include <algorithm>
     6 using namespace std;
     7 int dp[1005][35];
     8 int main()
     9 {
    10     int a[1005];
    11     int t, w;
    12     cin >> t >> w;
    13     int i;
    14     memset(dp, 0, sizeof(dp));
    15     memset(a, 0, sizeof(a));
    16     for (i = 1; i <= t; i++)
    17     {
    18         cin >> a[i];
    19     }
    20     int j;
    21     for (i = 1; i <= t; i++)
    22     {
    23         if (a[i] == 1)
    24         {
    25             dp[i][0] = dp[i-1][0] + 1;
    26         }
    27         else dp[i][0] = dp[i - 1][0];
    28         for (j = 1; j <= w; j++)
    29         {
    30             if ((j & 1) != (a[i] & 1))        //如果掉苹果的树编号与跑动次数奇偶性不同,则更新.比如苹果掉在1,跑了1次,没接到苹果
    31                 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + 1;
    32             else                           //如果掉苹果的树编号与跑动次数奇偶性相同,则保持上一行的状态值  
    33                 dp[i][j] = dp[i - 1][j];
    34         }
    35     }
    36     int ans = 0;
    37     for (int i = 0; i <= w; i++)        //遍历dp数组最后一行找最优状态值  
    38     {
    39         if (dp[t][i] > ans)
    40         {
    41             ans = dp[t][i];
    42         }
    43     }
    44     printf("%d
    ", ans);
    45     return 0;
    46 
    47 }

    偶数&1为0,奇数&1为1

    2&1即01&10=00,奇数的最后一位是1,偶数的最后一位是0

     
  • 相关阅读:
    MySQL存储引擎--MyISAM与InnoDB区别
    HTTP Keep-Alive模式
    php通过curl下载远程图片实例
    使用PHP QR Code生成二维码
    PHP中输出文件,怎么区别什么时候该用readfile() , fread(), file_get_contents(), fgets()
    SSDB 一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis.
    html头文件设置常用之<meta>设置缓存
    redis使用watch完成秒杀抢购功能
    Linux信号(signal) 机制分析
    php信号处理
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271257.html
Copyright © 2011-2022 走看看