-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLIS.cpp
57 lines (48 loc) · 1.21 KB
/
LIS.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
57
#include<bits/stdc++.h>
using namespace std;
void printoutput(int *arr, int *out, int curi);
int main()
{
int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
int *res;
int *out;
int len = sizeof(arr)/sizeof(arr[0]);
int max = len > 0 ? 1 : 0;
int n = 1;
int maxi = 0;
int i,j;
res = (int*)malloc(sizeof(int) * len);
out = (int*)malloc(sizeof(int) * len);
for (i=0; i < len; i++) {
res[i] = 1;
out[i] = -1;
}
for (i = 1; i < len; i++) {
n = res[i];
for (j = 0; j < i; j++) {
if ((arr[i] > arr[j]) && (res[j] >= n)) {
n = res[j] + 1;
out[i] = j;
}
}
res[i] = n;
if (n > max) {
maxi = i;
max = n;
}
}
printf("The LIS length is: %d\n", max);
printf("LIS value is:\n");
if (len != 0)
printoutput(arr, out, maxi);
printf("\n");
return 0;
}
void printoutput(int *arr, int *out, int curi) {
if (curi < 0)
return;
if (out[curi] != -1) {
printoutput(arr, out, out[curi]);
}
printf("%d ", arr[curi]);
}