-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestIncreaseSubsequence.cpp
More file actions
57 lines (49 loc) · 922 Bytes
/
LongestIncreaseSubsequence.cpp
File metadata and controls
57 lines (49 loc) · 922 Bytes
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;
const int MAXN = 1e4 + 5;
int n, a[MAXN];
int mem[MAXN];
int pos = -1;
int dp (int i) {
if (mem[i] != -1) return mem[i];
int res = 1;
for (int j = 1; j < i; j++) {
if (a[j] < a[i]) res = max(res, 1 + dp(j));
}
return mem[i] = res;
}
int solve() {
int ans = 1;
for (int i = 1; i <= n; i++)
{
if (ans < dp(i)) {
ans = dp(i);
pos = i;
}
}
return ans;
}
void trace(int i) {
for (int j = 0; j < i; j++)
{
if (mem[i] == 1 + mem[j] && a[j] < a[i]) {
trace(j);
break;
}
}
cout << a[i] << ' ';
}
void interative_trace() {
}
int main() {
memset(mem, -1, sizeof(mem));
cin >> n;
for (int i = 1; i <= n; i++)
{
/* code */
cin >> a[i];
}
cout << solve() << endl;
trace(pos);
return 0;
}