简体   繁体   English

动态编程最大序列

[英]Dynamic Programming Maximum Sequence

I work in stock trading. 我从事股票交易。 I have a two arrays representing currencies. 我有两个表示货币的数组。

I want to be able to find the maximum sequence in both the arrays. 我希望能够在两个数组中找到最大序列。

However, when I change between the two arrays I incur a fixed cost of £20.00 但是,当我在两个数组之间切换时,会产生20.00英镑的固定成本

For example: 例如:

Array 1: 12, 21, 45, 10, 42 Array 2: 52, 3, 4, 10, 35 阵列1:12,21,45,10,42阵列2:52,3,4,10,35

How do I use dynamic programming to solve this problem. 我如何使用动态编程来解决此问题。

Ie find the maximal weight of the sequence. 即找到序列的最大权重。

An obvious first attempt at a DP solution is to define an array P[i], which is the maximum weight attainable for transaction 1 through i. DP解决方案的一个明显的首次尝试是定义一个数组P [i],它是事务1到i可获得的最大权重。 The problem is that in order to update this array, we need to know where we were during the previous index since we need to know whether we need to charge the £20.00 change fare. 问题在于,为了更新此数组,我们需要知道上一个索引期间的位置,因为我们需要知道是否需要收取£20.00的更改票价。

We will encode this extra conditional information by adding an additional parameter to control for the location (Array1 or Array2) we were in the last transaction. 我们将通过添加一个附加参数来控制我们在上一个事务中所在的位置(Array1或Array2)来对这些额外的条件信息进行编码。

P [i, A1] = the max (sequence) transaction 1 through i, assuming transaction i is in A1 P [i,A1] =交易1到i的最大(顺序)交易,假设交易i在A1中

P [i, A2] = the max (sequence) transaction 1 through i, assuming transaction i is in A2. P [i,A2] =交易1到i的最大(顺序)交易,假设交易i在A2中。

For the basis case, assume we start in A1 , we incur no fixed cost, so we have P[0, A1] = 0. 对于基本情况, 假设我们从A1开始,则不产生固定成本,因此P [0,A1] = 0。

On the other hand, if we want to start in A2, we need to pay to get there, and thus, P [0, A2] = −20. 另一方面,如果我们要从A2开始,则需要付费才能到达那里,因此,P [0,A2] = -20。

Note : We can change the basis case if we start at A2 instead of A1 , but I assumed we MUST start at A1. 注意如果我们从A2而不是A1开始,我们可以更改基本情况 ,但是我假设我们必须从A1开始。 If you can start at either, you can compute both ways and find the max of the two with the following procedure. 如果可以从任何一个开始,则可以按照以下步骤计算两种方式并找到两者的最大值。

In general, for i > 0, to compute P[i, A1], we consider two possibilities, depending on where we were our last transaction. 通常,对于i> 0,要计算P [i,A1],我们考虑两种可能性,这取决于我们上次交易的位置。

If we were in A1, we don't need to incur the cost, and we obtain a value of A[i] on top of whatever value we accrued up to transaction i − 1. Thus, we 如果我们在A1中,则不需要花费成本,我们可以在交易i − 1产生的任何值的基础上获得A [i]的值。

P [i, A1] = A1[i] + P [i − 1, A1].

On the other hand, if we were in A2 last transaction, we need to pay the incur cost of 20, but we still obtain the A1 transaction and the accrued profit from the first i − 1 transactions. 另一方面,如果我们在A2的最后一笔交易中,我们需要支付20的产生成本,但是我们仍然从第一笔i-1交易中获得A1交易和应计利润。 In this case we 在这种情况下,我们

P[i, A1] = A1[i] + P [i − 1, A2] − 20.

We have two of the following recursive rule: 我们有以下两个递归规则:

P [i, A1] = A1[i] + max(P [i − 1, A1], P [i − 1, A2] − 20).
P [i, A2] = A2[i] + max(P [i − 1, A2], P [i − 1, A1] − 20).

Once we succeed in computing the values P [i, A1] and P [i, A2], for 0 ≤ i ≤ n, we return the max of P [n, A1] and P [n, A2] as the final result 一旦成功计算出值P [i,A1]和P [i,A2],对于0≤i≤n,我们将返回P [n,A1]和P [n,A2]最大值作为最终结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM