-
Notifications
You must be signed in to change notification settings - Fork 54
/
longestBitonicSequence.cpp
75 lines (69 loc) · 1.37 KB
/
longestBitonicSequence.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
75
#include <iostream>
using namespace std;
int lis(int* input, int n){
int* output = new int[n];
output[0] = 1;
for(int i = 1; i < n; i++){
output[i] = 1;
for(int j = i-1; j >= 0; j--){
if(input[j] <= input[i])
continue;
int possibleAns = output[j] + 1;
if(possibleAns > output[i])
output[i] = possibleAns;
}
}
int best = 0;
for(int i = 0; i < n; i++)
if(best < output[i])
best = output[i];
delete []output;
return best;
}
int lds(int* input, int n){
int* output = new int[n];
output[0] = 1;
for(int i = 1; i < n; i++){
output[i] = 1;
for(int j = i-1; j >= 0; j--){
if(input[j] >= input[i])
continue;
int possibleAns = output[j] + 1;
if(possibleAns > output[i])
output[i] = possibleAns;
}
}
int best = 0;
for(int i = 0; i < n; i++)
if(best < output[i])
best = output[i];
delete []output;
return best;
}
int lbs(int* input, int n){
int best = 0;
int* output = new int[n];
for(int k = 0; k < n; k++){
int i = lis(input+k, n-k);
int d = lds(input+k, n-k);
output[k] = i+d-1;
}
for(int k = 0; k < n; k++){
if(best < output[k])
best = output[k];
}
delete []output;
return best;
}
int main(){
int n;
int* input = new int[n];
cin >> n;
for(int i = 0; i < n; i++)
cin >> input[i];
//cout << lis(input, n) << endl;
//cout << lds(input, n) << endl;
cout << lbs(input, n) << endl;
delete []input;
return 0;
}