-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva10534.cpp
111 lines (84 loc) · 1.56 KB
/
uva10534.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
97
98
99
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
/*
vector<int> grade(vector<int> v)
{
vector<int> LIS;
LIS.resize(v.size());
LIS[0] = 1;
for(int i = 1; i < v.size(); i++)
{
LIS[i] = 1;
for(int j = 0; j < i; j++)
{
//daca se poate extinde secventa de lungime maxima
if(v[i] > v[j])
LIS[i] = max(LIS[i], 1 + LIS[j]);
}
}
return LIS;
} */
vector<int> grade(vector<int> v)
{
int index;
//int len;
vector<int>::iterator low;
vector<int> tailTable;
tailTable.push_back(v[0]);
vector<int> LIS;
LIS.resize(v.size());
LIS[0] = 1;
for(int i = 1; i < v.size(); i++)
{
if(v[i] < tailTable[0])
{
tailTable[0] = v[i];
LIS[i] = 1;
}
else if(v[i] > tailTable[tailTable.size() - 1])
{
tailTable.push_back(v[i]);
LIS[i] = tailTable.size();
}
else
{
low = lower_bound(tailTable.begin(), tailTable.end(), v[i]);
index = low - tailTable.begin(); //e ceil direct aici
tailTable[index] = v[i];
LIS[i] = index + 1;
}
}
return LIS;
}
int main()
{
vector<int> v;
int n;
int nr, wavio;
while(scanf("%d", &nr) != EOF)
{
for(int i = 0; i < nr; i++)
{
scanf("%d", &n);
v.push_back(n);
}
vector<int> LIS = grade(v);
reverse(v.begin(), v.end());
vector<int> LDS = grade(v);
reverse(LDS.begin(), LDS.end());
wavio = 0;
for(int i = 0; i < LIS.size(); i++)
{
if(min(LIS[i], LDS[i]) > wavio)
wavio = min(LIS[i], LDS[i]);
}
printf("%d\n", 2 * wavio - 1);
v.clear();
}
return 0;
}