• POJ 2385 Apple Catching


    Apple Catching

    Time Limit: 1000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 2385
    64-bit integer IO format: %lld      Java class name: Main
     
    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.
     

    Source

    解题:动态规划。。。有点像01背包。。。

     1 /*
     2 @author: Lev
     3 @date:
     4 */
     5 #include <iostream>
     6 #include <cstdio>
     7 #include <cmath>
     8 #include <cstring>
     9 #include <string>
    10 #include <cstdlib>
    11 #include <algorithm>
    12 #include <map>
    13 #include <set>
    14 #include <queue>
    15 #include <climits>
    16 #include <deque>
    17 #include <sstream>
    18 #include <fstream>
    19 #include <bitset>
    20 #include <iomanip>
    21 #define LL long long
    22 #define INF 0x3f3f3f3f
    23 
    24 using namespace std;
    25 const int maxn = 1010;
    26 int d[maxn],dp[2][40];
    27 int main() {
    28     int T,W;
    29     while(~scanf("%d %d",&T,&W)) {
    30         for(int i = 1; i <= T; ++i)
    31             scanf("%d",d+i);
    32         memset(dp,0,sizeof(dp));
    33         dp[0][W] = 1;
    34         for(int i = 1; i <= T; ++i)
    35             for(int j = W; j >= 0; --j) {
    36                 if(d[i] == 1) {
    37                     dp[0][j] = max(dp[0][j]+1,dp[1][j-1]+1);
    38                     dp[1][j] = max(dp[0][j-1],dp[1][j]);
    39                 }else{
    40                     dp[1][j] = max(dp[1][j]+1,dp[0][j-1]+1);
    41                     dp[0][j] = max(dp[0][j],dp[1][j-1]);
    42                 }
    43             }
    44         printf("%d
    ",max(dp[0][W],dp[1][W]));
    45     }
    46     return 0;
    47 }
    View Code

    噢、。。貌似会有出界的风险。。好吧。。再偏移下。。

     1 /*
     2 @author: Lev
     3 @date:
     4 */
     5 #include <iostream>
     6 #include <cstdio>
     7 #include <cmath>
     8 #include <cstring>
     9 #include <string>
    10 #include <cstdlib>
    11 #include <algorithm>
    12 #include <map>
    13 #include <set>
    14 #include <queue>
    15 #include <climits>
    16 #include <deque>
    17 #include <sstream>
    18 #include <fstream>
    19 #include <bitset>
    20 #include <iomanip>
    21 #define LL long long
    22 #define INF 0x3f3f3f3f
    23 
    24 using namespace std;
    25 const int maxn = 1010;
    26 int d[maxn],dp[2][40];
    27 int main() {
    28     int T,W;
    29     while(~scanf("%d %d",&T,&W)) {
    30         for(int i = 1; i <= T; ++i)
    31             scanf("%d",d+i);
    32         memset(dp,0,sizeof(dp));
    33         for(int i = 1; i <= T; ++i)
    34             for(int j = W+1; j > 0; --j) {
    35                 if(d[i] == 1) {
    36                     dp[0][j] = max(dp[0][j]+1,dp[1][j-1]+1);
    37                     dp[1][j] = max(dp[0][j-1],dp[1][j]);
    38                 }else{
    39                     dp[1][j] = max(dp[1][j]+1,dp[0][j-1]+1);
    40                     dp[0][j] = max(dp[0][j],dp[1][j-1]);
    41                 }
    42             }
    43         printf("%d
    ",max(dp[0][W+1],dp[1][W+1]));
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    python RabbitMQ gRPC 实践经验
    python 数据库实践经验
    python GUI相关
    angularjs中的$watch、$digest、$apply
    css选择器优先级
    angularjs基本理解
    如何编写符合web标准的XHTML文档
    跨域问题
    如何养成良好的书写代码习惯
    ie常见兼容问题
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4321651.html
Copyright © 2020-2023  润新知