A sequence {ai } is called k- flat if for any two consecutive elements ai and ai+ 1 , the following holds true: |ai - ai+ 1 | ≦ k. For a given sequence, find the longest subsequence which is k- flat.
Input : (standard input)
+Line 1 contains two numbers n and k , 1 ≦ ( n , k ) ≦ 100000, where n denotes a number of elements of a sequence.
+Line 2 contains n positive integers - elements of a given sequence, all of them are not greater than 1000000000.
Output : (standard output)
Your program should print out k- flat subsequence with the maximum length . If there are more than one solution, print the one for which the last element has the smallest index, if there are more than one such solutions, print the one for which the next to last has the smallest index, etc.
Example:
For sample input:
2 9 7 2 5 8
a correct output format:
9 7 5 8
Hint: There are two 3-flat subsequences (k = 3) with four elements: 9 7 5 8 and 2 2 5 8. Both ends up with the same element (8), the next to last are also the same (5), but 7 is earlier (has smaller index) than 2 in the above example.
solution:
说明:数据和是正确的, 但是序列打印有问题
寻找最长k-扁平子序列

本文介绍了一种算法,用于找出给定序列中最长的k-扁平子序列。该算法通过动态规划实现,确保找到满足条件的最长子序列,并在多个解中优先选择元素索引更小的子序列。
659

被折叠的 条评论
为什么被折叠?



