๐ฃ ๊ฐ์ฅ ๊ธด ๋ฐ์ดํ ๋ ๋ถ๋ถ ์์ด
1. ๋ฌธ์ ๋งํฌ
11054๋ฒ: ๊ฐ์ฅ ๊ธด ๋ฐ์ดํ ๋ ๋ถ๋ถ ์์ด
2. ์ฝ๋
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
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
incresed_dp = [0] * n
decresed_dp = [0] * n
# ์ข์ธก์์๋ถํฐ ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด ํ์
for i in range(n):
flag = 0
for j in range(i - 1, -1, -1):
if a[j] < a[i]:
incresed_dp[i] = incresed_dp[j] + 1
flag = max(flag, incresed_dp[j] + 1)
if flag:
incresed_dp[i] = flag
else:
incresed_dp[i] = 1
# ์ฐ์ธก์์๋ถํฐ ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด ํ์
for i in range(n - 1, -1, -1):
flag = 0
for j in range(i + 1, n):
if a[j] < a[i]:
flag = max(flag, decresed_dp[j] + 1)
if flag:
decresed_dp[i] = flag
else:
decresed_dp[i] = 1
ans = list(map(sum, list(zip(incresed_dp, decresed_dp))))
print(max(ans) - 1)
This post is licensed under CC BY 4.0 by the author.