-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva11456.cpp
107 lines (74 loc) · 1.14 KB
/
uva11456.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <list>
#include <stack>
using namespace std;
int N;
vector<int> v;
vector<int> lis;
vector<int> lds;
vector<int> createLIS()
{
vector<int> LIS;
LIS.resize(N);
LIS[LIS.size()-1] = 1;
for(int i = v.size()-1; i >= 0; i--)
{
LIS[i] = 1;
for(int j = v.size()-1; j > i; j--)
{
if(v[j] >= v[i])
LIS[i] = max(LIS[i], 1+LIS[j]);
}
}
return LIS;
}
vector<int> createLDS()
{
vector<int> LIS;
LIS.resize(N);
LIS[LIS.size()-1] = 1;
for(int i = v.size()-1; i >= 0; i--)
{
LIS[i] = 1;
for(int j = v.size()-1; j > i; j--)
{
if(v[j] <= v[i])
LIS[i] = max(LIS[i], 1+LIS[j]);
}
}
return LIS;
}
int main()
{
int tc;
cin >> tc;
while(tc--)
{
cin >> N;
if(N == 0)
cout << "0" << endl;
else
{
v.clear();
v.resize(N);
for(int i = 0; i < N; i++)
{
cin >> v[i];
}
lis = createLIS();
lds = createLDS();
int maxNr = -1;
for(int i = 0; i < v.size(); i++)
{
maxNr = max(maxNr, lis[i] + lds[i] - 1);
}
cout << maxNr << endl;
}
}
return 0;
}