-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlis-dp.cpp
74 lines (57 loc) · 1.28 KB
/
lis-dp.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// LIS - Longest Increase Sequence
// http://www.lcad.icmc.usp.br/~jbatista/scc218/ProgDin2_novo.pdf
// input:
// 8
// -7 10 9 2 3 8 8 1
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <string.h> // memset
using namespace std;
#define fs first
#define sd second
#define pb push_back
#define MAX 100030 // max array
const int INF = 0x3f3f3f3f;
int n, aux, dp[MAX];
vector<int> numbers;
// O (n^2)
int lis(int num, int i) {
if(i >= n - 2)
return 0;
if(num < numbers[i + 1]) {
return max(1 + lis(numbers[i + 1], i + 1), lis(num, i + 1));
}
return lis(num, i + 1);
}
// O (n^2)
int lis_dp() {
int ans = 0;
dp[0] = 1;
for(int i = 1; i < n; ++i) {
// the size of least increase sequence is 1
dp[i] = 1;
for(int j = 0; j < i; ++j) {
if(numbers[i] > numbers[j]) {
dp[i] = max(1 + dp[j], dp[i]);
ans = max(ans, dp[i]);
}
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; ++i) {
cin >> aux;
numbers.pb(aux);
}
cout << lis_dp() << '\n';
cout << lis(-INF, -1) << '\n';
return 0;
}