-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP11456.cpp
55 lines (53 loc) · 1.09 KB
/
P11456.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
/*
Special LIS: Include the element itself!
Algorithm from
https://en.wikipedia.org/wiki/Longest_increasing_subsequence#Online_algorithms
Section "Efficient algorithms".
No need for the P-array in this assignment.
L is saved in ret.
*/
void lis(int N, int *a, int *ret) {
int *M = new int[N+1]; // M[i] = First index in a where there is increasing subsequence of length i.
int L = 0;
FORI(N) {
int lo = 1;
int hi = L;
//cerr << "L=" << L << ", mid=" << (lo+hi+1)/2 << endl;
while(lo <= hi) {
int mid = (lo+hi+1)/2;
if(a[M[mid]] < a[i])
lo = mid+1;
else
hi = mid-1;
}
int newL = lo;
ret[i] = newL;
//cerr << " " << ret[i];
M[newL] = i;
if(newL > L)
L = newL;
}
//cerr << endl;
delete[] M;
}
int main() {
int N, x, A[2009], retA[2009], B[2009], retB[2009];
FORCAS {
cin >> N;
FORI(N) {
cin >> x;
A[N-1-i] = x;
B[N-1-i] = -x;
}
lis(N, A, retA);
lis(N, B, retB);
int ret = 0;
FORI(N) {
x = retA[i]+retB[i]-1;
if(x > ret)
ret = x;
}
cout << ret << endl;
}
return 0;
}