-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_increasing_subsequence_v3.cpp
executable file
·56 lines (47 loc) · 1.23 KB
/
longest_increasing_subsequence_v3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <stdio.h>
#define MAX_ARR_SIZE 100000
int arrLength;
// Array containing the input data
int arr[MAX_ARR_SIZE];
// Array containing max
int resultArr[MAX_ARR_SIZE];
int resultLink[MAX_ARR_SIZE];
int findLongestSequence()
{
int globalMaxIndex = 0;
for(int i=arrLength-1; i>=0; --i)
{
for(int j=i+1; j<arrLength; ++j)
{
if(arr[j] > arr[i] && resultArr[i] <= resultArr[j])
{
resultArr[i] = resultArr[j] + 1;
resultLink[i] = j;
}
}
if(resultArr[globalMaxIndex] < resultArr[i])
globalMaxIndex = i;
}
return globalMaxIndex;
}
int main()
{
while( std::cin >> arrLength )
{
for(int i=0; i<arrLength; ++i)
{
scanf("%d", &arr[i]);
resultArr[i] = 1;
resultLink[i] = -1;
}
int linkIndex = findLongestSequence();
std::cout << resultArr[linkIndex] << std::endl;
while(linkIndex >= 0)
{
std::cout << linkIndex << " ";
linkIndex = resultLink[linkIndex];
}
std::cout << std::endl;
}
}