-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path11456.cpp
37 lines (32 loc) · 1.04 KB
/
11456.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
/*
dynamic programming > longest increasing subsequence (LIS)
difficulty: medium
date: 01/Mar/2020
hint: find the max(lis[i]+lds[i]-1) for all i in [0 .. N-1], being i where the subsequence starts
by: @brpapa
*/
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 2002
vector<int> lis; // lis[i] = tamanho da maior lis começando em A[i]
vector<int> lds; // lds[i] = tamanho da maior lds começando em A[i]
int main() {
int T; cin >> T;
while (T--) {
int N; cin >> N; lis.assign(N, 1); lds.assign(N, 1);
vector<int> A(N); for (int &a : A) cin >> a;
for (int i = N-1; i >= 0; i--)
for (int j = i+1; j < N; j++)
if (A[i] < A[j] && lis[j]+1 > lis[i])
lis[i] = lis[j]+1;
for (int i = N-1; i >= 0; i--)
for (int j = i+1; j < N; j++)
if (A[i] > A[j] && lds[j]+1 > lds[i])
lds[i] = lds[j]+1;
int ans = 0;
for (int i = 0; i < N; i++) ans = max(ans, lis[i]+lds[i]-1);
cout << ans << endl;
}
return 0;
}