• 【动态规划】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;
    }
    
  • 相关阅读:
    超参数(Hyperparameter)
    验证和交叉验证(Validation & Cross Validation)
    训练集,验证集,测试集(以及为什么要使用验证集?)(Training Set, Validation Set, Test Set)
    特征的非线性变换(Feature Non-linear Transformation)
    机器学习---三种线性算法的比较(线性回归,感知机,逻辑回归)(Machine Learning Linear Regression Perceptron Logistic Regression Comparison)
    Numpy练习题
    过拟合产生的原因(Root of Overfitting)
    超哥笔记 ---集群、负载均衡、代理(7)
    搭建前端项目(2)
    超哥笔记 --nginx入门(6)
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7423368.html
Copyright © 2020-2023  润新知