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

    一、题目

    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.

    二、思路&心得

    • 定义dp[i][j]为在第i秒,移动j次获得的最大苹果数。在零时刻时,所有的dp项均为0。
    • 当j为0时,有dp[i][j] = dp[i - 1][j]
    • 当j不为0时,有dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]),即在 i - 1 时刻时,均有两种选择,一种选择移动,一种选择不移动。
    • 注意题目并不是在移动越多次能获得越多苹果。

    三、代码

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int MAX_T = 1005;
    const int MAX_W = 35;
    
    int main() {
    	int dp[MAX_T][MAX_W];
    	int num[MAX_T];
    	int T, W;
    	scanf("%d %d", &T, &W);
    	for (int i = 1; i <= T; i ++) {
    		scanf("%d", &num[i]);
    	}
    	for (int i = 1; i <= T; i ++) {
    		for (int j = 0; j <= W, j <= i; j ++) {
    			if (j == 0) dp[i][j] = dp[i - 1][j];
    			else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]);	
    			if ((num[i] - (j & 1) == 1)) dp[i][j] ++;
    		}
    	}
    	int ans = dp[T][0];
    	for (int i = 1; i <= W; i ++) {
    		ans = max(ans, dp[T][i]);
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    
  • 相关阅读:
    深度学习三巨头Hinton,Bengio,LeCunn共摘本年度ACM图灵奖(ACM A.M. Turing Award)
    【我为之而活的三种激情】by 伯特兰·罗素
    遥感高光谱分类文献阅读:Going Deeper with Contextual CNN for Hyperspectral Image Classification
    遥感高光谱分类文献阅读:Exploring Hierarchical Convolutional Features for Hyperspectral Image Classification
    大乘百法明门论笔记
    太宰治【人间失格】
    论文笔记:Accurate Causal Inference on Discrete Data
    因果推断(Causal Inference)概要
    阿毗达摩基本概念
    我们必须知道,我们终将知道。
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7423368.html
Copyright © 2011-2022 走看看