-
Notifications
You must be signed in to change notification settings - Fork 7
/
LIS.cpp
45 lines (42 loc) · 891 Bytes
/
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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void printLIS(vector<ll> &a, vector<ll> &prev, int pos) {
if(pos<0)
return;
printLIS(a, prev, prev[pos]);
cout<<a[pos]<<" ";
}
void LIS(vector<ll> &a) {
int i,j,maxim = 0,n = a.size();
vector<ll> LIS(n,1);
vector<ll> prev(n,-1);
for(i=0; i<n; i++) {
for(j=0; j<i; j++) {
if(a[i] > a[j] && LIS[i] < LIS[j]+1) {
LIS[i] = LIS[j] + 1;
prev[i] = j;
}
}
}
int pos = 0;
for(i=0; i<n; i++) {
if(LIS[i] > maxim) {
maxim = LIS[i];
pos = i;
}
}
cout<<"Length of LIS = "<<maxim<<endl;
printLIS(a,prev,pos);
cout<<endl;
}
int main() {
ll i,n;
cin>>n;
vector<ll> a(n);
for(i=0; i<n; i++) {
cin>>a[i];
}
LIS(a);
return 0;
}