-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest increasing subsequence.cpp
More file actions
45 lines (40 loc) · 1.18 KB
/
longest increasing subsequence.cpp
File metadata and controls
45 lines (40 loc) · 1.18 KB
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
#include <bits/stdc++.h>
using namespace std;
#define sset(x,y) memset(x,y,sizeof(x))
int sq[100];
int n;
int dp[100];
int direction[100]; // will be used for printing solution ;
int LIS (int v){
if (dp[v]!=-1) return dp[v]; // avoid calculating same state twice;
int mx = 0 , i;
for (i = v + 1; i < n; i++){ // always take next value from the starting value;
if (sq[i] > sq[v]){ // next value must be bigger than the previous;
if (mx < LIS (i)){// finding the maximum number of value
mx = LIS (i); // which satisfy LIS with the starting value;
direction[v] = i;// save printing direction in (direction) array;
}
}
}
dp[v] = 1 + mx; return dp[v]; // save and return
}
int main (){
sset(dp,-1);sset(direction,-1); // intialize (dp) and (direction) array with (-1);
cin >> n;
for (int i = 0; i < n; i++)
cin >> sq[i];
int mx_ind , mx_val = 0;
for (int i = 0; i < n; i++){
printf ("Longest subsequence of %d is %d\n" , sq[i] , LIS (i));
if (mx_val < LIS (i)){
mx_val = LIS (i);
mx_ind = i;
}
}puts("");
//cout << mx_ind << endl;
//cout << sq[mx_ind] << endl;;
while (direction[mx_ind]!=-1){
cout << sq[mx_ind] << " ";
mx_ind = direction[mx_ind];
}
}