zoukankan      html  css  js  c++  java
  • poj 2385【动态规划】

    poj 2385

    Apple Catching
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14007   Accepted: 6838

    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.
     
    题意:有树1和树2,他们在每一时刻只有一棵树落苹果,给定时间T,可在两棵树间移动的最大次数W,初始时站在树1下面。求能接到苹果数的最大值。
    题解:定义状态dp[i][j]表示在i时刻移动了j次时能得到的最大苹果数。状态的含义很重要!则dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+(移动j次后对应的树正好落苹果?1:0)
      方程想出来了,但是细节,边界处理,循环上还不会。。。显然移动次数为0时状态只能由上一个时间不移动的状态转移过来,即dp[i][0]=dp[i-1][0]+app[i]%2(初始时在树1下),而且有在第一时刻时若树1落苹果则dp[1][0]=1,否则dp[1][1]=1。由于是最多能移动W次并不是一定要移动W次,所以最后在0~W次结果中取最大值。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 int dp[1050][50];
     8 int app[1050];
     9 
    10 int main()
    11 {
    12     int T,W;
    13     scanf("%d%d",&T,&W);
    14     memset(dp,0,sizeof(dp));
    15     for(int i=1;i<=T;i++){
    16         scanf("%d",&app[i]);
    17     }
    18     if(app[1]==1) dp[1][0]=1;
    19     else dp[1][1]=1;
    20     for(int i=2;i<=T;i++){
    21         dp[i][0]=dp[i-1][0]+app[i]%2;
    22         for(int j=1;j<=W;j++){
    23             dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
    24             if(j%2+1==app[i]) dp[i][j]++;
    25         }
    26     }
    27     int ans=0;
    28     for(int i=0;i<=W;i++)
    29         ans=max(ans,dp[T][i]);
    30     printf("%d
    ",ans);
    31     return 0;
    32 }
     
  • 相关阅读:
    委托
    反射
    ADO.net五大对象
    DNS协议详解
    FTP协议详解
    .Net 多线程 异步编程 Await、Async和Task
    AsnycLocal与ThreadLocal
    Angular 6.X CLI(Angular.json) 属性详解
    Angular Npm Package.Json文件详解
    Npm常用命令整理
  • 原文地址:https://www.cnblogs.com/zxhyxiao/p/8459253.html
Copyright © 2011-2022 走看看