#풀이
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] nums = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++){
nums[i] = Integer.parseInt(st.nextToken());
}
int[] inc = new int[N];
int[] dec = new int[N];
Arrays.fill(inc,1);
Arrays.fill(dec,1);
for(int i = 0; i<N; i++){ //i로 끝나는 증가수열
for(int j = 0; j<i; j++){
if(nums[j]<nums[i])inc[i] = Math.max(inc[i],inc[j]+1);
}
}
for(int i = N-1; i>=0; i--){ //i로 끝나는 감소수열
for(int j = N-1; j>=i; j--){
if(nums[j]<nums[i])dec[i] = Math.max(dec[i],dec[j]+1);
}
}
int max = 0;
for(int i = 0; i<N; i++){
max = Math.max(max,inc[i]+dec[i]-1);
}
System.out.println(max);
}
}
#성능

#정리
i로 끝나는 가장 긴 증가하는 수열과 가장 긴 감소하는 수열을 구해서 둘의 조합으로 가장 긴 바이토닉 수열을 구하면 된다.
각 수열은 DP로 가장 긴 수열 길이를 구하면 된다.
'PS' 카테고리의 다른 글
[백준] 5214번 : 환승[Java] (0) | 2025.07.22 |
---|---|
[백준] 6118번 : 숨바꼭질[Java] (1) | 2025.07.21 |
[백준] 1806번 : 부분합[Java] (2) | 2025.07.18 |
[백준] 5639번 : 이진 검색 트리[Java] (1) | 2025.07.17 |
[백준] 17270번 : 연예인은 힘들어[Java] (0) | 2025.07.15 |