zoukankan      html  css  js  c++  java
  • POJ-2385

    Apple Catching
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13077   Accepted: 6354

    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.

    题意:

    两棵树,t分钟内每分钟有一棵掉落一个苹果,可在两树之间交换w次,问最多可以接到多少苹果。

    设dp[i][j]代表第i分钟,交换j次时接到苹果的值,

    则dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]),若当前j%2+1==a[i],则dp[i][j]++。

    最后在所有dp[t][j]中找到最大值即可。

    AC代码:

     1 //#include<bits/stdc++.h>
     2 #include<iostream>
     3 #include<cmath>
     4 using namespace std;
     5 
     6 int a[1010],dp[1010][40];
     7 
     8 int main(){
     9     ios::sync_with_stdio(false);
    10     int t,w;
    11     while(cin>>t>>w){
    12         for(int i=1;i<=t;i++){
    13             cin>>a[i];
    14         }
    15         if(a[1]==1){
    16             dp[1][0]=1;
    17             dp[1][1]=0;
    18         }
    19         else{
    20             dp[1][0]=0;
    21             dp[1][1]=1;
    22         }
    23         for(int i=2;i<=t;i++){
    24             for(int j=0;j<=w;j++){
    25                 if(j==0){
    26                     dp[i][j]=dp[i-1][j]+a[i]%2;
    27                 }
    28                 else{
    29                     dp[i][j]=max(dp[i-1][j-1],dp[i-1][j]);
    30                     if(j%2+1==a[i])
    31                     dp[i][j]++;
    32                 }
    33             }
    34         }
    35         int MAX=0;
    36         for(int i=0;i<=w;i++){
    37             MAX=max(dp[t][i],MAX);
    38         }
    39         cout<<MAX<<endl;
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    [转]用异或交换两个整数的陷阱
    线索化二叉树
    [转]Socket编程中,阻塞与非阻塞的区别
    两个链表的归并
    [转] std::string and stl 算法
    类图
    leetcode 答案
    about raw socket
    54. Spiral Matrix【数组】
    矩阵乘法问题的实现
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/7365184.html
Copyright © 2011-2022 走看看