forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa 10534 - Wavio Sequence.cpp
50 lines (46 loc) · 1.2 KB
/
UVa 10534 - Wavio Sequence.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// LIS[i] stores the maximum length of S's LIS ending at i.
void doLIS(const vector<size_t> &S, vector<size_t> &LIS)
{
vector<size_t> L(S.size());
size_t lisCount = 0;
for (size_t i = 0; i < S.size(); ++i)
{
size_t pos = lower_bound(L.begin(), L.begin() + lisCount, S[i])
- L.begin();
L[pos] = S[i];
if (pos == lisCount)
++lisCount;
LIS[i] = pos + 1;
}
}
int main()
{
size_t N;
while (cin >> N)
{
vector<size_t> S(N);
for (int i = 0; i < N; ++i)
cin >> S[i];
vector<size_t> LIS(N), LDS(N);
doLIS(S, LIS);
reverse(S.begin(), S.end());
doLIS(S, LDS);
reverse(LDS.begin(), LDS.end());
size_t maxLIS = 0;
for (size_t i = 0; i < LIS.size(); ++i)
{
/** Suppose S[] = 1 3 6 5
LIS[] = 1 2 3 3
LDS[] = 1 1 2 1
then we can pick 1 6 5 or 3 6 5.
*/
maxLIS = max(maxLIS, 2 * min(LIS[i], LDS[i]) - 1);
}
cout << maxLIS << endl;
}
return 0;
}