Borab wrote: |
---|
all that stuff is weird to me |
It's never too late to learn the basics.
adjacent_find() is the C++ function that searches a sequence (in your case, an array) for the next two adjacent values that satisfy a criterion.
In your case, you're looking for the longest decreasing subsequence, so you need to find the next two adjacent values such that the first is less or equal the second
For example, let's find the end of the decreasing subsequence that begins at the first element:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
int main()
{
int a[] = {5, 3, 1, 4, 6, 4, 3, 2};
int* p = adjacent_find(begin(a), end(a), less_equal<int>());
std::cout << "The longest decreasing sequence that starts at position 0 ends at position "
<< distance(a, p) << '\n';
}
|
online demo:
http://liveworkspace.org/code/MzQyOT
(compatibility note: begin() and end() for arrays are fairly new functions in C++, for old compilers, use
adjacent_find(a, a+8, less_equal<int>());
or just use vectors, they are better than arrays anyway)
Now, once you have the end of the decreasing sequence that started at position 0, you want to run adjacent_find again to find the end of the sequence that starts at 3 (which is zero-length), and then the sequence that starts at 4 (which goes all the way to the end), and since you're looking for the longest, you'd be recording the new result each time you find a sequence that's longer than the previous. That's what vlad's code does.