zoukankan      html  css  js  c++  java
  • Apple Catching(dp)

    Apple Catching
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9831   Accepted: 4779

    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位置落苹果,问最多移动M次可以在T分钟街道的最多苹果数目;
    dp每分钟对移动次数进行处理,移动奇数次可以接到位置1的苹果,偶数则相反,然后从1~T分钟每分钟对移动次数判断当前时间移动j次可以接到的最大苹果数;
    if(j&1)
         dp[j][i]=max(dp[j-1][i-1],dp[j][i-1])+x-1;
        else
         dp[j][i]=max(dp[j-1][i-1],dp[j][i-1])+(x&1);
    代码:
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const double PI=acos(-1.0);
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define SL(x) scanf("%lld",&x)
    #define PI(x) printf("%d",x)
    #define PL(x) printf("%lld",x)
    #define P_ printf(" ");
    #define puts puts("");
    typedef long long LL;
    const int MAXN=1010;
    int dp[35][MAXN];
    int main(){
    	int T,W;
    	while(~scanf("%d%d",&T,&W)){
    		mem(dp,0);
    		int x;
    		for(int i=1;i<=T;i++){
    			SI(x);
    			dp[0][i]=dp[0][i-1]+(x&1);
    			for(int j=1;j<=W;j++){
    				if(j&1)
    					dp[j][i]=max(dp[j-1][i-1],dp[j][i-1])+x-1;
    				else 
    					dp[j][i]=max(dp[j-1][i-1],dp[j][i-1])+(x&1);
    			}
    		}
    		int ans=0;
    		for(int i=0;i<=W;i++)ans=max(ans,dp[i][T]);
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    零知识证明入门
    Vue入门语法(二)表单,组件,路由和ajax
    Vue入门语法(一)
    okexchain整体架构分析
    写了个unsigned Tx生成二维码的web站点
    metamusk与web3相关资料
    QRCode.js:使用 JavaScript 生成二维码
    js工程安装,发布等命令
    C语言学习笔记-9.结构体
    C语言学习笔记-8.指针
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5082216.html
Copyright © 2011-2022 走看看