-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLIS.cpp
53 lines (45 loc) · 767 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
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
using namespace std;
vector<int> L;
vector<int> cache;
int len;
int prob(int start);
int main(void){
int C;
cin >> C;
for(;C--;){
cin >> len;
L.clear();
cache.clear();
L.resize(len);
cache.resize(len);
for(int idx = 0; idx < len; idx++){
cin >> L[idx];
cache[idx] = -1;
}
int output = -1;
for(int idx = 0; idx < len; idx++){
output = max(output, prob(idx));
}
cout << output << endl;
}
}
//N^2 algorithm...
int prob(int start){
if (start == len - 1){
return 1;
}
int& ret = cache[start];
if (ret != -1){
return ret;
}
int cur = L[start];
ret = 1;
for(int idx = start + 1; idx < len; idx++){
if (cur < L[idx]){
ret = max(ret, 1 + prob(idx));
}
}
return ret;
}