UVa 10134:越大越聪明吗? (动态规划和最长递增子序列)
private void findLDS() {
Integer[] array = Arrays.copyOf(elephants.iq, elephants.iq.length);
Hashtable<Integer, Integer> eq = elephants.elephantiqs;
Integer[] lds = new Integer[array.length];
Integer[] prev= new Integer[array.length];
lds[0] = 0;
prev[0] = 0;
int maxlds = 1, ending=0;
for(int i = 0; i < array.length; ++i) {
lds[i] = 1;
prev[i] = -1;
for (int j = i; j >= 0; --j) {
if(lds[j] + 1 > lds[i] && array[j] > array[i] && eq.get(array[j]) < eq.get(array[i])) {
lds[i] = lds[j]+1;
prev[i] = j;
}
}
if(lds[i] > maxlds) {
ending = i;
maxlds = lds[i];
}
}
System.out.println(maxlds);
for(int i = ending; i >= 0; --i) {
if(prev[i] != -1) {
System.out.println(eq.get(array[prev[i]]));
}
}
我将此算法基于这个SO问题。该代码试图找到最长的递减子序列而不是递增子序列。 array[] 按降序排序,我还有一个哈希表,其中大象的智商作为其权重的键。
我很难正确理解 DP,我需要一些帮助。
除了跟踪 prev[] 中所选的序列(它总是丢失一个元素)之外,我的算法似乎工作得很好。有谁知道该怎么做?
private void findLDS() {
Integer[] array = Arrays.copyOf(elephants.iq, elephants.iq.length);
Hashtable<Integer, Integer> eq = elephants.elephantiqs;
Integer[] lds = new Integer[array.length];
Integer[] prev= new Integer[array.length];
lds[0] = 0;
prev[0] = 0;
int maxlds = 1, ending=0;
for(int i = 0; i < array.length; ++i) {
lds[i] = 1;
prev[i] = -1;
for (int j = i; j >= 0; --j) {
if(lds[j] + 1 > lds[i] && array[j] > array[i] && eq.get(array[j]) < eq.get(array[i])) {
lds[i] = lds[j]+1;
prev[i] = j;
}
}
if(lds[i] > maxlds) {
ending = i;
maxlds = lds[i];
}
}
System.out.println(maxlds);
for(int i = ending; i >= 0; --i) {
if(prev[i] != -1) {
System.out.println(eq.get(array[prev[i]]));
}
}
I have based this algorithm on this SO question. This code is trying to find longest decreasing subsequence instead of increasing. array[] is sorted in descending order, and I also have a hashtable with the elephants IQ's as keys for their weights.
I'm having a hard time properly understanding DP, and I need some help.
My algorithm seems to work fine besides tracking the chosen sequence in prev[], where it always misses one element. Does anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决这个问题的几种方法:
如果你不理解最长增加子序列O(N^2)的DP,它基本上是这样的:
llis
,代表“最长递增子序列”,长度为N,即现在大象的数量。创建另一个名为last
且长度相同的数组。我假设大象的排序列表称为数组,就像您的问题陈述中一样。llis
中索引 n 的元素(这是一个不同的“n”)< N 将是array
子数组从索引 0 到 n(含)的最长递增子序列的长度。还可以说next
数组中索引 n 处的元素将是array
中最长递增子序列的下一个索引。llis
中的第 1 元素,查找实际子序列将简化为遵循next
数组中的索引。llis[k] + 1
。 (另外,请记住将next[k]
设置为 n,因为递增子序列中的下一个大象将是 n 处的大象。 )llis[n] = max(llis[n], llis[k] + 1)
,在遍历了所有 k 后,严格之前n。只需以正确的顺序(线性)处理 n ,您就应该得到正确的结果。llis
中的最大数字以获得最终结果。A few ways to approach this one:
If you don't understand the DP for longest increasing subsequence O(N^2), it's basically this:
llis
standing for "Longest Increasing Subsequence", of length N, the number of elephants there now are. Create another array calledlast
with the same length. I will assume the sorted list of elephants is calledarray
as it is in your problem statement.llis
at index n (this is a different "n") < N will be the length of the longest increasing subsequence for the sub-array ofarray
from index 0 to n, inclusive. Also say that the element in thenext
array at index n will be the next index inarray
in the longest increasing subsequence.llis
after the DP calculations, and finding the actual subsequence would simplify to following the indices in thenext
array.llis[k] + 1
. (Also, remember to setnext[k]
to be n, since the next elephant in the increasing subsequence will be the one at n.)llis[n] = max(llis[n], llis[k] + 1)
, after going through all k s that come strictly before n. Just process the n s in the right order (linearly) and you should get the correct result.llis
to get your final result.