-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11054.cpp
70 lines (64 loc) · 1.13 KB
/
11054.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
// 11054. 가장 긴 바이토닉 부분 수열
// 2019.05.22
// 다이나믹 프로그래밍
#include<iostream>
using namespace std;
int a[1001];
int d[2][1001];
//d[0][i] : 앞에서 부터 하는 가장 긴 증가 부분 수열
//d[1][i] : 뒤에서 부터 하는 가장 긴 증가 부분 수열
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
// 앞에서 부터 하는 가장 긴 증가 부분수열
for (int i = 1; i <= n; i++)
{
int max = 0;
for (int j = 0; j < i; j++)
{
if (a[i] > a[j])
{
if (max < d[0][j])
{
max = d[0][j];
}
}
}
d[0][i] = max + 1;
}
// 뒤에서 부터 하는 가장 긴 증가 부분수열
for (int i = n; i >= 1; i--)
{
int max = 0;
for (int j = n; j > i; j--)
{
if (a[i] > a[j])
{
if (max < d[1][j])
{
max = d[1][j];
}
}
}
if (d[1][i] < max + 1)
{
d[1][i] = max + 1;
}
}
int max = 0;
for (int i = 1; i <= n; i++)
{
if (max < d[0][i] + d[1][i])
{
max = d[0][i] + d[1][i];
}
}
// 자기 자신 두번 포함하기에 -1한 결과를 출력한다.
cout << max - 1 << endl;
return 0;
}