I'm currently working on the following problem:
我目前正在解决以下问题:
Given an array of M positive numbers, I need to get N blocks of contiguous numbers with some given length. For example, when I have the array:
给定一个M个正数的数组,我需要得到N个连续的数字,并给出一定的长度。例如,当我有数组时:
6 9 3 2 8 1 6 9 7
6 9 3 2 8 6 9 7
When I need to find one block of length 3, the solution is [3,2,8] which has a total minimal sum of 13. When I need to find two blocks, the algorithm should give [3,2,8] and [1,6,9] since the sum of all elements in these blocks is minimal (29). It is given that the length of the sequence is always strictly larger than N times the length of a block (so there is always a solution).
当我需要找到一个长度为3的块时,解是[3,2,8],它总共有13的最小和。当我需要找到两个块时,算法应该给出[3,2,8]和[1,6,9],因为这些块中的所有元素之和都是最小的(29)。已知序列的长度总是严格大于N乘以一个块的长度(所以总有一个解)。
I think this problem is solvable by using DP but I currently can't see how. I'm struggling to find a recurrent relation between the subproblems. Could anyone give me a hand here?
我认为这个问题是可以用DP来解决的,但是我现在不知道怎么解决。我很难找到子问题之间的循环关系。有人能帮我一下吗?
Thanks in advance!
提前谢谢!
1 个解决方案
#1
6
-
Calculate the sum of each block with the given length, and record them with the initial index. This can be done by a complexity of
O(n)
. So you get a list like:用给定的长度计算每个块的和,并用初始索引记录它们。这可以通过O(n)的复杂性来实现。所以你会得到这样一个列表:
index sum 0 18 1 14 2 13 ... ...
-
Due to the objective blocks could not overlap with each other, so each difference of their indexes can not be less than the given length. So you need to apply a simple dynamic planning algorithm on the list you got.
由于目标块之间不能互相重叠,所以它们的索引的每个差异都不能小于给定的长度。所以你需要在你得到的列表上应用一个简单的动态规划算法。
if the block length is
l
, list length isn
(say the listS[n]
), and you want to findm
blocks, then the如果块长度为l,则列表长度为n(假设列表S[n]),并且要找到m个块,则
F(n,m,l) = min { F(n-i-l,m-1,l) + S[n-i] } (for i = 0 ~ n-(m-1)*l)
F(n,m,l)=分钟{ F(n-i-l,m - 1,l)+ S[n]}(i = 0 ~ n - l(m - 1)*)
The complexity of this step is O(nm)
where m
is how many blocks you want.
这个步骤的复杂性是O(nm),其中m是你想要的块数。
Finally the complexity is O(nm)
. Let me know if you need more details.
最后,复杂性是O(nm)。如果你需要更多的细节,请告诉我。
#1
6
-
Calculate the sum of each block with the given length, and record them with the initial index. This can be done by a complexity of
O(n)
. So you get a list like:用给定的长度计算每个块的和,并用初始索引记录它们。这可以通过O(n)的复杂性来实现。所以你会得到这样一个列表:
index sum 0 18 1 14 2 13 ... ...
-
Due to the objective blocks could not overlap with each other, so each difference of their indexes can not be less than the given length. So you need to apply a simple dynamic planning algorithm on the list you got.
由于目标块之间不能互相重叠,所以它们的索引的每个差异都不能小于给定的长度。所以你需要在你得到的列表上应用一个简单的动态规划算法。
if the block length is
l
, list length isn
(say the listS[n]
), and you want to findm
blocks, then the如果块长度为l,则列表长度为n(假设列表S[n]),并且要找到m个块,则
F(n,m,l) = min { F(n-i-l,m-1,l) + S[n-i] } (for i = 0 ~ n-(m-1)*l)
F(n,m,l)=分钟{ F(n-i-l,m - 1,l)+ S[n]}(i = 0 ~ n - l(m - 1)*)
The complexity of this step is O(nm)
where m
is how many blocks you want.
这个步骤的复杂性是O(nm),其中m是你想要的块数。
Finally the complexity is O(nm)
. Let me know if you need more details.
最后,复杂性是O(nm)。如果你需要更多的细节,请告诉我。